Re: combine multiple row values in to one row - Mailing list pgsql-general

From David Fetter
Subject Re: combine multiple row values in to one row
Date
Msg-id 20090707154304.GJ32215@fetter.org
Whole thread Raw
In response to Re: combine multiple row values in to one row  (David Fetter <david@fetter.org>)
List pgsql-general
On Tue, Jul 07, 2009 at 08:40:06AM -0700, David Fetter wrote:
> On Tue, Jul 07, 2009 at 01:59:35AM +0430, Lee Harr wrote:
> >
> > Is there a generic way to do this? An aggregate maybe?
>
> The aggregate is called array_agg() and it's in 8.4.  You can then
> wrap array_to_string() around it and get pretty formatting, as in:
>
> SELECT idn, array_to_string(array_agg(code),', ') AS codes FROM tbl;

Oops.  That should read:

SELECT
    idn,
    array_to_string(array_agg(code),', ') AS codes
FROM tbl
GROUP BY idn; /* gotta group by :) */

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

pgsql-general by date:

Previous
From: Ransika de Silva
Date:
Subject: Re: Table Partitioning : Having child tables in multiple database servers
Next
From: Dimitri Fontaine
Date:
Subject: Re: Table Partitioning : Having child tables in multiple database servers