Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 3905A637745 for ; Wed, 22 Jul 2009 16:01:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 13811-01 for ; Wed, 22 Jul 2009 16:01:40 -0300 (ADT) X-Greylist: delayed 00:29:17.076379 by SQLgrey-1.7.6 Received: from relais2.altitudetelecom.fr (relais2.altitudetelecom.fr [195.7.112.10]) by mail.postgresql.org (Postfix) with ESMTP id B6B9C63F755 for ; Wed, 22 Jul 2009 15:16:11 -0300 (ADT) Received: from [192.168.63.25] (unknown [91.151.125.192]) by relais2.altitudetelecom.fr (Postfix) with ESMTP id E578A65C395; Wed, 22 Jul 2009 19:46:45 +0200 (CEST) Message-ID: <4A675056.9070501@free.fr> Date: Wed, 22 Jul 2009 19:45:58 +0200 From: nha Organization: Lyon, Rhone, France User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: David Weilers CC: pgsql-sql Subject: Re: Double aggregate problem References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=-10 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/100 X-Sequence-Number: 32841 Hello, Le 22/07/09 18:16, David Weilers a écrit : > Dear anyone, > > I have the following query: > > select v.id, array_to_string(array_accum(s.name),', ') as sector , > array_to_string(array_accum(p.name),', ') as provincie from tblvacature > v, tblaccount a , tblvacaturesector vs, tblsector s , > tblvacatureprovincie vp, tblprovincie p where v.id = 11 and v.account = > a.id and vs.vacature = v.id and s.id = vs.sector and vp.vacature = v.id > and p.id = vp.provincie group by v.id, v.inserted order by v.inserted > desc > [...] > If i leave out one aggregate, the result is as i expect (if I leave out > 'provincie', sector gives): > [...] > I would like both array_accum returning only what they should and not > doubles. > [...] According to your observation, the following query may match your need: SELECT t1.id, t1.sector, t2.provincie FROM ( SELECT v.id, v.inserted, array_to_string(array_accum(s.name),', ') AS sector FROM tblvacature v, tblaccount a, tblvacaturesector vs, tblsector s, tblvacatureprovincie vp, tblprovincie p WHERE v.account = a.id and vs.vacature = v.id and s.id = vs.sector and vp.vacature = v.id and p.id = vp.provincie GROUP BY v.id, v.inserted ) AS t1 INNER JOIN ( SELECT v2.id, array_to_string(array_accum(p2.name),', ') AS provincie FROM tblvacature v2, tblaccount a2, tblvacaturesector vs2, tblsector s2, tblvacatureprovincie vp2, tblprovincie p2 WHERE v2.account = a2.id and vs2.vacature = v2.id and s2.id = vs2.sector and vp2.vacature = v2.id and p2.id = vp2.provincie GROUP BY v2.id, v2.inserted ) AS t2 ON t1.id = t2.id WHERE t1.id = 11 ORDER BY t1.inserted DESC The query has been rewritten as an inner join between two subqueries issued from the original one. WHERE and SORT clauses have been pulled off so that subqueries are not too enough broken down. Other optimization may be applied (eg. by using explicit joins between the different tables and by checking appropriate indexes are set up). Regards. -- nha / Lyon / France.