Search code examples
sqlpostgresqlstring-aggregation

Concatenate strings in a column in PostgreSQL


I am trying to transform the table below:

id  another_id_1 another_id_2  remarks
1   34.         151.         good
2.  34.         151.         okay
3.  34.         152.         bad
4.  34.         153.         very good
5.  34          153          okay
6.  34          154          good
7.  34          155          bad

Into

another_id_1 another_id_2 remarks
34.          151.         good, okay
34.          152.         bad
34.          153          very good, okay
34.          154          good
34           155          bad

the table below using postgresql statement:

Is there a way this can be achieved, Nothing I have tried seemed to work


Solution

  • Although not consistent with your data, I think you want aggregation:

    select another_id_1, another_id_2, 
           string_agg(remarks, ', ' order by id) as remarks
    from t
    group by another_id_1, another_id_2;