Interestingly enough, I have only now have found this declaration in the User-Defined Aggregates related Postgres documentation chapter:
CREATE AGGREGATE array_accum (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);This array aggregate function is very useful when working with arrays in PostgreSQL and it is not included to the default installation. It can be used as a reverse to the
ARRAY(query) construct and sometimes together with generate_series() result set generation function.Another, sometimes quite important, aggregate function to aggregate text is
CREATE AGGREGATE text_accum (text)
(
sfunc = textcat,
stype = text,
initcond = ''
);but as it does not allow to insert delimiters in the accumulated text it's usage is quite limited.
To accumulate texts using say a comma as a delimiter
array_to_string(array_accum(TEXT_COLUMN_TO_AGGREGATE), ', ') construct can be used.To concatenate several arrays in aggregate another, very simple aggregate can be used
CREATE AGGREGATE public.array_accum_cat(anyarray) (
SFUNC=array_cat,
STYPE=anyarray,
INITCOND='{}'
);This makes it possible to merge several arrays together in one one-dimensional array.
1 comments:
thanks for the explication, very useful :)
Post a Comment