Re: [SQL] aggregate question

2003-06-24 Thread Hubert Lubaczewski
On Mon, 23 Jun 2003 13:59:52 -0500 (CDT)
"Ryan" <[EMAIL PROTECTED]> wrote:

>  package_id  | integer | not null default
> I must be a total space case today because I can't hammer out the sql to
> get a listing of all the packages with a count() of the package_log by
> package_id.

if you would just package_id and count it would be simple:
select
p.package_id,
count(*) as count
from
packages p
left outer join package_log l on p.package_id = l.package_id
;

but, if you want all fields from packages it get trickier.
you can do it in two ways:

select
p.*,
(select count(*) from package_log l where l.package_id = p.package_id)
from
packages p

or:

select
p.*,
coalesce(c.count,0)
from
packages p
left outer join (select l.package_id, count(*) as count from package_log l) c 
on p.package_id = c.package_id


should work - but i just wrote it "by hand", and didn't test it. anyway - it should 
give either working code or idea on how to achieve it.

depesz

---(end of broadcast)---
TIP 9: the planner will ignore your desire to choose an index scan if your
  joining column's datatypes do not match


Re: [SQL] Bitwise operation

2003-06-30 Thread Hubert Lubaczewski
On Mon, 30 Jun 2003 22:28:15 +1000
"Rudi Starcevic" <[EMAIL PROTECTED]> wrote:

> SELECT  & 111 = 71
> I was expecting the second example to be 
> SELECT  & 111 = 111

hmm ..:
let's see:
dec() is bin(10001010111)
dec(111)  is bin(110)

so, when we'll "AND" them:
 :  10001010111 
111  :  110 
---  
1000111 : dec: 71 

quite reasonable, isn't it?

depesz

---(end of broadcast)---
TIP 8: explain analyze is your friend