Aggregate Arrays In Postgres
array_agg
is a great aggregate function in Postgres but it gets weird when aggregating other arrays.
First let's look at what array_agg
does on rows with integer columns:
It puts each value into an array. What if are values are arrays?
Put this doesn't work when the arrays have different numbers of elements:
If you are trying to accumulate elements to process in your code, you can use jsonb_agg
.
The advantage of using Postgres arrays however is being able to unnest those arrays downstream:
Tweet