X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3B1FB474E44 for ; Fri, 7 Feb 2003 08:28:49 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id 99078474E42 for ; Fri, 7 Feb 2003 08:28:48 -0500 (EST) Received: from klaster.net (pe231.krakow.cvx.ppp.tpnet.pl [213.76.40.231]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id D7F3B2B152; Fri, 7 Feb 2003 14:21:41 +0100 (CET) Message-ID: <3E43B402.1090301@klaster.net> Date: Fri, 07 Feb 2003 14:26:26 +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> <3E438F0F.5000301@klaster.net> <200302071209.55330.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/154 X-Sequence-Number: 11552 Gary Stainburn wrote: > On Friday 07 Feb 2003 10:48 am, Tomasz Myrta wrote: > > Hi Tomasz, > > I don't think you understand what I mean. > > The history table could be thought of as the following SQL statement if the > data had actually existed. This table actually represents a manually input > summary of the pre-computerised data. > > insert into history > select rosid, rojid, count(*) from roster_staff group by rssid, rsgsid; > > If I have a history of > > hsid | hjid | hcount > ------+------+-------- > 1 | 2 | 3 > 1 | 3 | 1 > 5 | 5 | 4 > 6 | 5 | 3 > 9 | 4 | 4 > 14 | 5 | 4 > > and I have a roster of > > rodate | rogid | rojid | rosid > -----------+-------+-------+------- > 2003-02-15 | 1 | 2 | 1 > 2003-02-15 | 1 | 5 | 5 > 2003-02-16 | 1 | 5 | 1 > > I want my view to show > > hsid | hjid | hcount > ------+------+-------- > 1 | 2 | 4 > 1 | 3 | 1 > 1 | 5 | 1 > 5 | 5 | 5 > 6 | 5 | 3 > 9 | 4 | 4 > 14 | 5 | 4 > I understood your problem well and I just thought some idea will be enough to continue work. Here is detailed query for your problem: create view some_view as select coalesce(hjid,rjid) as jid, coalesce(hsid,rsid) as sid, coalesce(hcount,1)+count(*)-1 as hcount from history full outer join roster on (hjid=rjid and hsid=rosid) group by hjid,rjid,hsid,rosid; Regards, Tomasz Myrta