PostgreSQL: joining arrays within group by clause
We have a problem grouping arrays into a single array. We want to join the values from two colums into one single array and aggregate these arrays of multiple rows. Given the following input: | id | name | col_1 | col_2 | | 1 | a | 1 | 2 | | 2 | a | 3 | 4 | | 4 | b | 7 | 8 | | 3 | b | 5 | 6 | We want the following output: | a | { 1, 2, 3, 4 } | | b | { 5, 6, 7, 8 } | The order of the elements is important and should correlate with the id of the aggregated rows. We tried the array_agg function: SELECT array_agg(ARRAY[col_1, col_2]) FROM mytable GROUP BY name; Unfortunately, this statement