X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id BCC9CD1DD2C for ; Tue, 17 Feb 2004 23:33:57 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 93381-08 for ; Tue, 17 Feb 2004 19:33:58 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id E4A39D1DD59 for ; Tue, 17 Feb 2004 19:33:55 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i1HNXsFA018789; Tue, 17 Feb 2004 18:33:54 -0500 (EST) To: Richard Huxton Cc: terry@ashtonwoodshomes.com, "Postgresql Sql Group (E-mail)" Subject: Re: Return relation table data in a single value CSV In-reply-to: <200402172104.38307.dev@archonet.com> References: <007701c3f591$5acc3020$2766f30a@development.greatgulfhomes.com> <200402172104.38307.dev@archonet.com> Comments: In-reply-to Richard Huxton message dated "Tue, 17 Feb 2004 21:04:38 +0000" Date: Tue, 17 Feb 2004 18:33:54 -0500 Message-ID: <18788.1077060834@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/232 X-Sequence-Number: 16922 Richard Huxton writes: > 2. Write a custom aggregate function (like sum()) to do the concatenation. > This is easy to do, but the order your ABC get processed in is undefined. Actually, as of 7.4 it is possible to control the order of inputs to a custom aggregate. You do something like this: SELECT foo, myagg(bar) FROM (SELECT foo, bar FROM table ORDER BY foo, baz) AS ss GROUP BY foo The inner sub-select must order by the columns that the outer will group on; it can then order by additional columns that determine the sort order within each group. Here, myagg() will see its input ordered by increasing values of baz. Before 7.4 this method didn't work because the planner was too stupid to avoid re-sorting the subquery output. You could only make it work in cases where you weren't doing grouping ... regards, tom lane