Postgresql merge text []

Can I find out how to combine arrays according to identifier:

    Column1    Column2
--------------------------
    1          {"a","b"}
    1          {"c"}
    2          {"d"}
    2          {"w"}

output:

    Column1    Column2
--------------------------
    1          {"a","b","c"}
    2          {"d","w"}

I tried array_agg, but this is not the appropriate function to merge the array. using version 8.4 at the moment.

+3
source share
1 answer
CREATE AGGREGATE array_concat(TEXT[]) (
    SFUNC = array_cat,
    STYPE = TEXT[],
    INITCOND = '{}'
);

And if your table is "a" and the affected columns are "n" and "t":

 SELECT array_concat(t) FROM a GROUP BY n;
+1
source

Source: https://habr.com/ru/post/1774285/


All Articles