Re: How-To: Aggregate data from multiple rows into a delimited list.

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Dimitri Fontaine" <dfontaine(at)hi-media(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How-To: Aggregate data from multiple rows into a delimited list.
Date: 2007-07-03 09:03:55
Message-ID: 162867790707030203t41d3f35g28f32887eee512aa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> The query would become
> SELECT a.id, a.name, array_acc(b.name) as b_names
> FROM a LEFT JOIN b USING(id)
> GROUP BY a.id, a.name;
>

All variants are possible. Variant with array_to_string is faster and
doesn't need own aggregate function. And you can easy remove
duplicities.

SELECT a.id, a.name,
ARRAY_TO_STRING(ARRAY(
SELECT DISTINCT b.name
FROM b
WHERE b.id = a.id
ORDER BY b.name ASC
), ',') AS b_names
FROM a
ORDER BY a.id ASC;

regards
Pavel

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Poul Møller Hansen 2007-07-03 11:09:00 Re: Invalid page header
Previous Message Richard Huxton 2007-07-03 08:57:29 Re: General search problem - challenge