Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id A5A1D9F9830 for ; Sat, 25 Aug 2007 12:55:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 19311-04 for ; Sat, 25 Aug 2007 12:55:37 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ns1.officenet.no (vpn.officenet.no [193.212.174.13]) by postgresql.org (Postfix) with ESMTP id 612C39F9826 for ; Sat, 25 Aug 2007 12:55:37 -0300 (ADT) Received: from [84.48.119.79] (helo=andreak-laptop.local) by ns1.officenet.no with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IOxz0-0000Q9-4r for pgsql-sql@postgresql.org; Sat, 25 Aug 2007 17:55:35 +0200 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Returning array of IDs as a sub-query with group-by Date: Sat, 25 Aug 2007 17:55:30 +0200 User-Agent: KMail/1.9.6 References: <200708251710.57557.andreak@officenet.no> In-Reply-To: <200708251710.57557.andreak@officenet.no> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200708251755.30291.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/204 X-Sequence-Number: 29142 On Saturday 25 August 2007 17:10:57 Andreas Joseph Krogh wrote: > Hi all. > I have the following schema: > > create table item( > id serial primary key > ); > > > create table item_log( > id serial primary key, > item_id integer not null references item(id), > price numeric NOT NULL > ); > > > insert into item(id) values(1); > insert into item(id) values(2); > insert into item(id) values(3); > insert into item(id) values(4); > > insert into item_log(item_id, price) values(1, 100); > insert into item_log(item_id, price) values(1, 100); > insert into item_log(item_id, price) values(1, 100); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(1, 200); > insert into item_log(item_id, price) values(2, 200); > insert into item_log(item_id, price) values(2, 200); > > Now, to get out all log-entries grouped on price with count the following > query gives me what I want > > SELECT COUNT(il.price), i.id AS item_id, il.price FROM item i, item_log il > WHERE i.id =3D il.item_id GROUP BY il.price, i.id; > > count | item_id | price > -------+---------+------- > 3 | 1 | 100 > 6 | 1 | 200 > 2 | 2 | 200 > (3 rows) > > Now - I would like to return an ARRAY of item_log.id for each of the two > rows. The result I'm looking for would look like this: > > 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 =3D il.id) AS item_id_array > FROM item i, item_log il WHERE i.id =3D il.item_id GROUP BY il.price, i= =2Eid; > > ERROR: subquery uses ungrouped column "il.id" from outer query > > Any hints? I found the following CREATE AGGREGATE suggestion in the PG-docs: CREATE AGGREGATE array_accum (anyelement) ( sfunc =3D array_append, stype =3D anyarray, initcond =3D '{}' ); With this I can easily issue: SELECT COUNT(il.price), i.id AS item_id, il.price, array_accum(il.id) AS item_id_array FROM item i, item_log il WHERE i.id =3D il.item_id GROUP BY il.price, i.i= d; Which does what I want: count | item_id | price | item_id_array =2D------+---------+-------+--------------- 3 | 1 | 100 | {1,2,3} 6 | 1 | 200 | {4,5,6,7,8,9} 2 | 2 | 200 | {10,11} (3 rows) If someone knows of a way without introducing a new AGGREGATE I'm still=20 interrested. =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=E9 11 | know how to do a thing and to watch | PO. Box 529 Sk=F8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+