X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 4553B30F7F9 for ; Tue, 24 Jun 2003 07:33:44 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 56075-03 for ; Tue, 24 Jun 2003 04:33:33 -0300 (ADT) Received: from boa.eo.pl (boa.eo.pl [217.17.46.250]) by svr1.postgresql.org (Postfix) with SMTP id E476E30F7A0 for ; Tue, 24 Jun 2003 04:33:32 -0300 (ADT) Received: (qmail 12870 invoked from network); 24 Jun 2003 07:33:31 -0000 Received: from unknown (HELO depeszws) (depesz@eo.pl@62.233.139.194) by boa.eo.pl with SMTP; 24 Jun 2003 07:33:31 -0000 Date: Tue, 24 Jun 2003 09:33:22 +0200 From: Hubert Lubaczewski To: "Ryan" Cc: Subject: Re: aggregate question Message-Id: <20030624093322.2051cbb5.hubert.lubaczewski@eo.pl> In-Reply-To: <54717.65.102.128.233.1056394792.squirrel@fordparts.com> References: <54717.65.102.128.233.1056394792.squirrel@fordparts.com> Organization: EO Networks X-Mailer: Sylpheed version 0.8.11claws161 (GTK+ 1.2.10; i386-debian-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Archive-Number: 200306/280 X-Sequence-Number: 14038 On Mon, 23 Jun 2003 13:59:52 -0500 (CDT) "Ryan" 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