I have a table with several columns where some of them are arrays of the same length. I would like to unnest them to get a result with values from arrays in separate rows.
So having table like this one:
I would like to get to:
This is how it works for one of those array columns:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1
FROM data,
UNNEST(array_1) as a1
Is there some elegant way how to unnest both arrays at once? I would like to avoid unnesting each column separately and then joining everything together.
Below is for BigQuery Standard SQL
#standardSQL
SELECT id, a1, a2
FROM data, UNNEST(array_1) AS a1 WITH OFFSET
JOIN UNNEST(array_2) AS a2 WITH OFFSET
USING(OFFSET)
You can use with offset
and a join
:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1, a2
FROM data cross join
UNNEST(array_1) as a1 with offset n1 JOIN
UNNEST(array_2) as a2 with offset n2
on n1 = n2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With