I'm trying to aggregate integer arrays that have different lengths. This answer pads with nulls and I am trying to incorporate it into my query.
Here is my data:
id | col
-----+---------
1 | {5,3}
1 | {6,4}
2 | {3}
2 | {2,3}
Here is my desired result
id | col
-----+---------
1 | {{5,3},{6,4}}
2 | {{3,NULL},{2,3}}
See DBFiddle
This is my current query, which aggregrates by id but it outputs duplicates. This is my first time using LATERAL function so don't know if this is right approach.
SELECT
array_agg(
array_cat(
col,
array_fill(NULL::smallint, ARRAY[lat-COALESCE(array_length(col, 1),0)])
)
) AS result
,tab.id
FROM tab
,LATERAL (SELECT id,MAX(array_length(col,1)) AS lat
FROM tab GROUP BY id) s
GROUP BY tab.id