Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux