Re: aggregate question - Mailing list pgsql-sql

From Hubert Lubaczewski
Subject Re: aggregate question
Date
Msg-id 20030624093322.2051cbb5.hubert.lubaczewski@eo.pl
Whole thread Raw
In response to aggregate question  ("Ryan" <pgsql-sql@seahat.com>)
List pgsql-sql
On Mon, 23 Jun 2003 13:59:52 -0500 (CDT)
"Ryan" <pgsql-sql@seahat.com> 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:
selectp.package_id,count(*) as count
frompackages pleft 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:

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

or:

selectp.*,coalesce(c.count,0)
frompackages pleft 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
howto achieve it.
 

depesz


pgsql-sql by date:

Previous
From: "Nicolas JOUANIN"
Date:
Subject: Re: TR: Like and =
Next
From: Tomasz Myrta
Date:
Subject: Re: aggregate question