Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A2A499F9CA9 for ; Sat, 25 Aug 2007 17:50:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 04271-02 for ; Sat, 25 Aug 2007 17:50:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mx1.ipf.is (mack.ipf.is [85.197.192.164]) by postgresql.org (Postfix) with ESMTP id 4BE1F9F9C73 for ; Sat, 25 Aug 2007 17:50:09 -0300 (ADT) Received: (qmail 11985 invoked by uid 1010); 25 Aug 2007 20:49:50 +0000 Received: from 88.149.106.19 by mack (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.88.4/1639. spamassassin: 3.1.4. perlscan: 1.25-st-qms. Clear:RC:1(88.149.106.19):SA:0(-2.4/4.5):. Processed in 7.132945 secs); 25 Aug 2007 20:49:50 -0000 X-Antivirus-HIVE-Mail-From: gnari@hive.is via mack X-Antivirus-HIVE: 1.25-st-qms (Clear:RC:1(88.149.106.19):SA:0(-2.4/4.5):. Processed in 7.132945 secs Process 11966) Received: from dsl-149-106-19.hive.is (HELO ?192.168.1.33?) (88.149.106.19) by mx1.ipf.is with SMTP; 25 Aug 2007 20:49:43 +0000 Subject: Re: Returning array of IDs as a sub-query with group-by From: Ragnar To: Andreas Joseph Krogh Cc: pgsql-sql@postgresql.org In-Reply-To: <200708251755.30291.andreak@officenet.no> References: <200708251710.57557.andreak@officenet.no> <200708251755.30291.andreak@officenet.no> Content-Type: text/plain Date: Sat, 25 Aug 2007 21:02:19 +0000 Message-Id: <1188075739.12268.50.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/205 X-Sequence-Number: 29143 On lau, 2007-08-25 at 17:55 +0200, Andreas Joseph Krogh wrote: > On Saturday 25 August 2007 17:10:57 Andreas Joseph Krogh wrote: [snip] > > count | item_id | price | item_id_array > > -------+---------+-------+--------------- > > 3 | 1 | 100 | {1,2,3} > > 6 | 1 | 200 | {4,5,6,7,8,9} > > 2 | 2 | 200 | {10,11} > > > > I tried this query which complains about an ungruoped column: > > > > SELECT COUNT(il.price), i.id AS item_id, il.price, > > ARRAY(SELECT a.id FROM item_log a WHERE a.id = il.id) AS item_id_array > > FROM item i, item_log il WHERE i.id = il.item_id GROUP BY il.price, i.id; > > > > ERROR: subquery uses ungrouped column "il.id" from outer query > > > > Any hints? > > I found the following CREATE AGGREGATE suggestion in the PG-docs: [aggregate solution snipped] > If someone knows of a way without introducing a new AGGREGATE I'm still > interrested. you can allways do the ARRAY(SELECT...) outside the grouping: # select *,(select ARRAY( SELECT a.id FROM item_log as a WHERE foo.item_id=a.item_id AND foo.price=a.price ) ) AS item_id_array from ( select count(*),item_id, price from item_log group by item_id, price ) as foo; count | item_id | price | item_id_array -------+---------+-------+--------------- 3 | 1 | 100 | {1,2,3} 6 | 1 | 200 | {4,5,6,7,8,9} 2 | 2 | 200 | {10,11} (3 rows) but i suspect the aggregate will perform better gnari