2016-03-30 51 views
-1

ile Concat sonuçları PostgreSQL kullanıyorum. Sütunları olan bir tablom var: id, şehir, ülke. Her ülke için birkaç şehrim var. ÖrneğinPostgreSQL

: Ben

Bunu nasıl yapabilirim
Country  Cities 
Brazil  (Rio de Janeiro) 
Argentina (Buenos Aires, Bariloche) 

gibi bir şey döndüren bir SEÇ istiyorum

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

Ve?

+1

Şimdiye kadar ne denediniz? http://www.postgresql.org/docs/9.4/static/functions-aggregate.html –

+1

Olası http://stackoverflow.com/questions/29557563/whats-the-equivalent-for-listagg-in-postgres kopyası – leqid

+0

geliştirilmiş biçimlendirme – showdev

cevap

0

Sonuç kümesinde ne almak istediğinize bağlı olarak - array_agg() veya string_agg() işlevlerini kullanabilirsiniz.

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;