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

前端 未结 14 1602
北荒
北荒 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:08

    Following yet again on the use of a custom aggregate function of string concatenation: you need to remember that the select statement will place rows in any order, so you will need to do a sub select in the from statement with an order by clause, and then an outer select with a group by clause to aggregate the strings, thus:

    SELECT custom_aggregate(MY.special_strings)
    FROM (SELECT special_strings, grouping_column 
            FROM a_table 
            ORDER BY ordering_column) MY
    GROUP BY MY.grouping_column
    

提交回复
热议问题