PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

come raccogliere più valori come una singola stringa in postgres?

Puoi unirti ai tavoli e usare array_agg per combinare i valori separati da una virgola

SELECT a.id, array_agg(b.name) assignments
FROM    Project a
        INNER JOIN assignment b
          ON a.id = b.project_ID
GROUP BY a.id

Demo SQLFiddle

o utilizzando STRING_AGG

SELECT a.id, STRING_AGG(b.name, ', ' ORDER BY b.name) assignments
FROM    Project a
        INNER JOIN assignment b
          ON a.id = b.project_ID
GROUP BY a.id

Demo SQLFiddle