From: Hubert Lubaczewski <hubert.lubaczewski@eo.pl>
To: Ryan <pgsql-sql@seahat.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: aggregate question
Date: Tue, 24 Jun 2003 09:33:22 +0200
Message-ID: <20030624093322.2051cbb5.hubert.lubaczewski@eo.pl> (raw)
In-Reply-To: <54717.65.102.128.233.1056394792.squirrel@fordparts.com>
References: <54717.65.102.128.233.1056394792.squirrel@fordparts.com>
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:
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: hubert.lubaczewski@eo.pl, pgsql-sql@seahat.com
Subject: Re: aggregate question
In-Reply-To: <20030624093322.2051cbb5.hubert.lubaczewski@eo.pl>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox