Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten multiple same-sized array columns in BigQuery table

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:

input table

I would like to get to:

output table

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.

like image 487
matt525252 Avatar asked Sep 15 '25 07:09

matt525252


2 Answers

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)
like image 113
Mikhail Berlyant Avatar answered Sep 17 '25 22:09

Mikhail Berlyant


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
like image 26
Gordon Linoff Avatar answered Sep 17 '25 21:09

Gordon Linoff