How to concatenate strings of a string field in a PostgreSQL 'group by' query?

前端 未结 14 1420
北荒
北荒 2020-11-22 02:37

I am looking for a way to concatenate the strings of a field within a group by query. So for example, I have a table:

ID   COMPANY_ID   EMPLOYEE
1    1               


        
14条回答
  •  长发绾君心
    2020-11-22 03:07

    Following up on Kev's answer, using the Postgres docs:

    First, create an array of the elements, then use the built-in array_to_string function.

    CREATE AGGREGATE array_accum (anyelement)
    (
     sfunc = array_append,
     stype = anyarray,
     initcond = '{}'
    );
    
    select array_to_string(array_accum(name),'|') from table group by id;
    

提交回复
热议问题