Search code examples
sqlpostgresqlaggregate-functions

Concatenate date values in Postgres


I have a table that contains duplicate values in the columns c1, c2, c3, c4, c5.
I want to list all the dates in the row that contain the values generated the group by function.

datum          c1    c2    c3    c4    c5    number
2001-04-22     1     2     3     4     5     2
1999-08-24     2     3     4     5     6     2
2005-11-08     2     4     5     6     7     2
1998-03-20     1     2     3     4     5     2
2009-07-02     2     3     4     5     6     2
1996-05-21     2     4     5     6     7     2

Result should look like this:

datum                      c1    c2    c3    c4    c5    number
1998-03-20 , 2001-04-22    1     2     3     4     5     2
1999-08-24 , 2009-07-02    2     3     4     5     6     2
1996-05-21 , 2005-11 08    2     4     5     6     7     2

in the source table can be duplicate values ​​in all columns, what will be the solution?


Solution

  • select string_agg(to_char(datum, 'yyyy-mm-dd'), ','),
           c1, 
           c2,
           c3, 
           c4, 
           c5, 
           number
    from some_table
    group by c1, c2,c3, c4, c5, number