Concatenate multiple rows in an array with SQL on PostgreSQL

后端 未结 4 1932
挽巷
挽巷 2020-12-13 17:45

I have a table constructed like this :

oid | identifier | value
1   | 10         | 101
2   | 10         | 102
3   | 20         | 201
4   | 20         | 202
5         


        
4条回答
  •  执念已碎
    2020-12-13 18:18

    Here is the code for the requested output.

    select identifier, array_agg(value)
    from (
      values
        (1   , 10         , 101),
        (2   , 10         , 102),
        (3   , 20         , 201),
        (4   , 20         , 202),
        (5   , 20         , 203)
      ) as tab (oid, identifier, value)
    group by identifier
    order by identifier;
    

提交回复
热议问题