pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Steven Murdoch <psql+Steven.Murdoch@cl.cam.ac.uk>
Cc: pgsql-sql@postgresql.org
Subject: Re: Sorting items in aggregate function
Date: Tue, 12 Sep 2006 12:17:59 -0600
Message-ID: <20060912181759.GA66966@winnie.fuhr.org> (raw)
In-Reply-To: <20060912154628.GB4991@cl.cam.ac.uk>
References: <20060912154628.GB4991@cl.cam.ac.uk>

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



view thread (5+ messages)  latest in thread

Message-ID: <20060912181759.GA66966@winnie.fuhr.org>
Permalink:  ../20060912181759.GA66966@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20060912181759.GA66966@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, psql+Steven.Murdoch@cl.cam.ac.uk
  Subject: Re: Sorting items in aggregate function
  In-Reply-To: <20060912181759.GA66966@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox