X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id EA8919F9ECD for ; Tue, 12 Sep 2006 15:19:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 75240-05 for ; Tue, 12 Sep 2006 15:18:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id ECC259FA432 for ; Tue, 12 Sep 2006 15:18:10 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.8/8.13.8) with ESMTP id k8CII0UQ062169 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 12 Sep 2006 12:18:03 -0600 (MDT) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.8/8.13.8) with ESMTP id k8CII0Ex077185; Tue, 12 Sep 2006 12:18:00 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.8/8.13.8/Submit) id k8CII097077184; Tue, 12 Sep 2006 12:18:00 -0600 (MDT) (envelope-from mfuhr) Date: Tue, 12 Sep 2006 12:17:59 -0600 From: Michael Fuhr To: Steven Murdoch Cc: pgsql-sql@postgresql.org Subject: Re: Sorting items in aggregate function Message-ID: <20060912181759.GA66966@winnie.fuhr.org> References: <20060912154628.GB4991@cl.cam.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060912154628.GB4991@cl.cam.ac.uk> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.168 tagged_above=0 required=5 tests=AWL, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/81 X-Sequence-Number: 26292 On Tue, Sep 12, 2006 at 04:46:28PM +0100, Steven Murdoch wrote: > Here is an example of a setup and what I could like to achieve. Does > anyone have suggestions on what is the best way to get the desired > result? Use the aggregate over an ordered subquery: SELECT name, trim(concat(code || ' ')) AS codes FROM ( SELECT a.name, b.code FROM a LEFT JOIN ab ON a.id = ab.a_id LEFT JOIN b ON ab.b_id = b.id ORDER BY b.code ) AS s GROUP BY name ORDER BY name; Here's a comment from Tom Lane, one of the core developers, on feeding the aggregate based on the subquery's order: http://archives.postgresql.org/pgsql-general/2005-09/msg00047.php -- Michael Fuhr