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 8B3C430F6F2 for ; Mon, 23 Jun 2003 22:04:16 +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 52494-06 for ; Mon, 23 Jun 2003 19:04:06 -0300 (ADT) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by svr1.postgresql.org (Postfix) with ESMTP id A61C530F774 for ; Mon, 23 Jun 2003 19:04:05 -0300 (ADT) Received: from klaster.net (tv126.internetdsl.tpnet.pl [80.55.125.126]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 22CC22B3DD; Tue, 24 Jun 2003 00:04:03 +0200 (CEST) Message-ID: <3EF7797A.4040003@klaster.net> Date: Tue, 24 Jun 2003 00:04:42 +0200 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.4b) Gecko/20030507 X-Accept-Language: pl, en-us, en MIME-Version: 1.0 To: Ryan Cc: pgsql-sql@postgresql.org Subject: Re: aggregate question References: <54717.65.102.128.233.1056394792.squirrel@fordparts.com> In-Reply-To: <54717.65.102.128.233.1056394792.squirrel@fordparts.com> Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 8bit X-Archive-Number: 200306/276 X-Sequence-Number: 14034 Dnia 2003-06-23 20:59, Użytkownik Ryan napisał: > I know this one is simple enough. > > I have two tables: packages and package_log. > 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. > > Thanks, > Ryan select package_name, count(*) as n_packages from packages join package_log using (package_id); This query is the simplest one, but doesn't display packages without any logs. If you need this, try this one: select p.package_name, (select count(*) from package_log pl where pl.package_id=p.package_id) as n_packages from packages p; And one more: select package_name, sum(case when package_log.package_id is not null then 1 else 0 end) as n_packages from packages left join package_log using (package_id); Regards, Tomasz Myrta