Re: Sorting items in aggregate function

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Steven Murdoch <psql+Steven(dot)Murdoch(at)cl(dot)cam(dot)ac(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Sorting items in aggregate function
Date: 2006-09-12 18:17:59
Message-ID: 20060912181759.GA66966@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Aaron Bono 2006-09-12 18:21:35 Re: on connect/on disconnect
Previous Message Daryl Richter 2006-09-12 18:09:53 Re: Joint a table back on itself?