UNNEST
UNNEST is a useful utility to expand an ARRAY or a MAP into individual rows of data. The resulting output column will contain the individual data in the array mapped to existing non-ARRAY columns. UNNEST is typically used with a JOIN and can work in single and multiple column instances
UNNEST Example
Suppose we have a
SELECT *
FROM boardgamepoints
player | points -------+--------------- Lato | {4,5,1} Mario | {9,1,5} Kalie | {7,2,3} Leo | {5,2,3,9} Jeny | {5,2,0,1,2,4} Pro | {1,2,0,1,4} (6 rows)
UNNESTING the Points Column
One simple implementation of unnest can be to simply pass the column of arrays into the
SELECT UNNEST(points) as points
FROM boardgamepoints;
unnest -------- 4 5 1 9 1 5 7 2 3 5 2 3 9 5 2 0 1 2 4 1 2 0 1 4 (24 rows)
While this is often useful, it is not easy to then map these points to the individuals. To solve for this, we can do a CROSS JOIN UNNEST
CROSS JOIN UNNEST
The
SELECT player, score
FROM boardgamepoints
CROSS JOIN UNNEST(points) AS t(score);
player | score -------+------- Lato | 4 Lato | 5 Lato | 1 Mario | 9 Mario | 1 Mario | 5 Kalie | 7 Kalie | 2 Kalie | 3 Leo | 5 Leo | 2 Leo | 3 Leo | 9 Jeny | 5 Jeny | 2 Jeny | 0 Jeny | 1 Jeny | 2 Jeny | 4 Pro | 1 Pro | 2 Pro | 0 Pro | 1 Pro | 4 (24 rows)