xxlali
xxlali

Reputation: 1046

How to concat strings in different row on Postgres

Let's say I have a table Foo and has a column name. I want to concatenate all names in Foo. For example

Table Foo

  Name
---------
  name1
  name2
  name3

I want to write a query that returns name1name2name3 or if possible name1,name2,name3.

I have done some googling and see concat function but it only concats columns of same row. I couldn't find a function or a way to accomplish this.

Upvotes: 0

Views: 60

Answers (1)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 32021

use string_agg

SELECT string_agg(Foo, ', ') AS col
FROM   tbl

Upvotes: 3

Related Questions