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

Concat righe in Postgres

Usa GROUP BY e la funzione aggregata string_agg() :

SELECT nummer
      ,string_agg(vorname, E'\n') AS vorname
      ,string_agg(name, E'\n') AS name
      ,cash
FROM   mytable
GROUP  BY nummer, cash
ORDER  BY nummer, cash;

Ho aggiunto cash al GROUP BY per arrivare al valore originale e salvaguardarsi dal caso in cui sarebbe diverso per lo stesso nummer .

Quanto al tuo commento:

SELECT nummer
      ,string_agg(vorname, E'\n') AS vorname
      ,string_agg(name, E'\n') AS name
      ,cash
FROM  (
   SELECT DISTINCT
          nummer, vorname, name, cash
   FROM   mytable
   ) AS m
GROUP  BY nummer, cash
ORDER  BY nummer, cash;