Concatenate multiple rows in an array with SQL on PostgreSQL

后端 未结 4 1945
挽巷
挽巷 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:02

    You have to create an aggregate function, e.g.

    CREATE AGGREGATE array_accum (anyelement)
    (
    sfunc = array_append,
    stype = anyarray,
    initcond = '{}'
    );
    

    then

    SELECT identifier, array_accum(value) AS values FROM table GROUP BY identifier;
    

    HTH

提交回复
热议问题