2016-03-30 13 views
-1

Ich benutze PostgreSQL. Ich habe einen Tisch mit Spalten: ID, Stadt, Land. Ich habe mehrere Städte für jedes Land.Concat Ergebnisse mit PostgreSQL

Zum Beispiel:

ID  Country  City 
1  Brazil  Rio de Janeiro 
2  Argentina Buenos Aires 
3  Argentina Bariloche 

Und ich eine SELECT wollen, dass, wie so etwas wie

Country  Cities 
Brazil  (Rio de Janeiro) 
Argentina (Buenos Aires, Bariloche) 

zurückgibt, kann ich es tun?

+1

Was haben Sie bisher versucht? http://www.postgresql.org/docs/9.4/static/functions-aggregate.html –

+1

Mögliches Duplikat von http://stackoverflow.com/questions/29557563/whats-the-äquivalent-for-listagg-in-postgres – leqid

+0

verbesserte Formatierung – showdev

Antwort

0

Je nachdem, was Sie im Resultset erhalten möchten, können Sie die Funktionen array_agg() oder string_agg() verwenden.

WITH t(id,country,city) AS (VALUES 
    (1,'Brazil','Rio de Janeiro'), 
    (2,'Argentina','Buenos Aires'), 
    (3,'Argentina','Bariloche') 
) 
SELECT country,'(' || string_agg(city,',') || ')' FROM t 
GROUP BY country;