X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 4E6B09DC863 for ; Mon, 27 Mar 2006 11:12:49 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 84851-09 for ; Mon, 27 Mar 2006 11:12:53 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from peufeu.com (boutiquenumerique.com [82.67.9.10]) by postgresql.org (Postfix) with ESMTP id 18A439DC874 for ; Mon, 27 Mar 2006 11:12:44 -0400 (AST) Received: (qmail 19711 invoked from network); 27 Mar 2006 17:12:51 +0200 Received: from boutiquenumerique.com (HELO apollo13) (82.67.9.10) by boutiquenumerique.com with SMTP; 27 Mar 2006 17:12:51 +0200 Date: Mon, 27 Mar 2006 17:12:49 +0200 To: "Jim C. Nasby" Subject: Re: Expressing a result set as an array (and vice versa)? Cc: pgsql-sql@postgresql.org References: <20060327134831.GJ80726@pervasive.com> From: PFC Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID: In-Reply-To: <20060327134831.GJ80726@pervasive.com> User-Agent: Opera M2/8.51 (Linux, build 1462) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.112 required=5 tests=[AWL=0.112] X-Spam-Score: 0.112 X-Spam-Level: X-Archive-Number: 200603/430 X-Sequence-Number: 24776 >> SELECT array_accum( DISTINCT list_id ) FROM bookmarks; >> array_accum >> --------------- >> {1,2,3,4,5,7} > > Couldn't you just use array()? Yes, you can do this : SELECT ARRAY( SELECT something with one column ); However, array_accum() as an aggregate is more interesting because you can use GROUP BY. For instance : SELECT parent, array_accum( child ) FROM table GROUP BY parent; I have another question. Suppose I have these tables : CREATE TABLE items ( id SERIAL PRIMARY KEY, category INTEGER NOT NULL, name TEXT NOT NULL, ); CREATE TABLE comments ( item_id INTEGER NOT NULL REFERENCES items(id), id SERIAL PRIMARY KEY, comment TEXT NOT NULL, added TIMESTAMP NOT NULL DEFAULT now() ) Say I want to display some items and the associated comments : SELECT * FROM items WHERE category = ... Then, I gather the item ids which were returned by this query, and do : SELECT * FROM comments WHERE item_id IN ( the ids ) ORDER BY item_id, added; Is there a more elegant and efficient way which would avoid making a big IN() query ? I could join comments with items, but in my case the search condition on items is quite complicated and slow ; hence I only want to do the search once. And I have several different tables in the same style of the "comments" table, and so I make several queries using the same IN (...) term. It isn't very elegant... is there a better way ? Use a temporary table ? How do you do it ?