X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 5D762474E4F for ; Fri, 7 Feb 2003 09:33:44 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id C0149474E42 for ; Fri, 7 Feb 2003 09:33:43 -0500 (EST) Received: from klaster.net (pe105.krakow.cvx.ppp.tpnet.pl [213.76.40.105]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 114442B152; Fri, 7 Feb 2003 15:26:42 +0100 (CET) Message-ID: <3E43C503.6010508@klaster.net> Date: Fri, 07 Feb 2003 15:38:59 +0100 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; PL; rv:1.1) Gecko/20020826 X-Accept-Language: pl, en-us, en MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: efficient count/join query References: <200302071007.28697.gary.stainburn@ringways.co.uk> <200302071209.55330.gary.stainburn@ringways.co.uk> <3E43B402.1090301@klaster.net> <200302071421.14672.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/158 X-Sequence-Number: 11556 Gary Stainburn wrote: > On Friday 07 Feb 2003 1:26 pm, Tomasz Myrta wrote: > >>Gary Stainburn wrote: >> > > Many appologoes Tomasz. No problem > > Because your select *LOOKED* wrong to me, I didn't even try it. Upon looking > at it again I can see what you're doing. When I tried, it complained about > the counts and grouping, so I moved the count(*) to a sub-select and changed > the coalesce and it's working. There were 2 bugs in this query: - in group by - add field hcount - counting when any roster doesn't exist for some history. Here is (I suppose) final query: create view some_view as select coalesce(hjid,rjid) as jid, coalesce(hsid,rsid) as sid, coalesce(hcount,0)+ sum(case when rjid is not null then 1 else 0 end) as hcount from history full outer join roster on (hjid=rjid and hsid=rosid) group by hjid,rjid,hsid,rosid,hcount; Tomasz Myrta