Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced UNNEST Across Multiple Array Columns in BigQuery

question about UNNEST. I have this table:enter image description here

I want to unnest based on the status and locale arrays but the result table still needs to have 7 rows, NOT 14. I want to unnest those "array pairs", not the 2 arrays one by one (that would return 14 rows).

Also, I don't the know the count of items in the arrays. It can be 1 but it also can be 20 but every array in the "group" does have the same number of items.

Any ideas of how to achieve that?

(keep in mind that I have around 10 columns as arrays but my sample image is showing only 2 of them)

like image 830
Daniel Zrůst Avatar asked Jan 01 '23 08:01

Daniel Zrůst


1 Answers

Below is for BigQuery Standard SQL

#standardSQL
SELECT * EXCEPT(status, locale, pos1, pos2), status, locale  
FROM `project.dataset.table`,
UNNEST(status) status WITH OFFSET pos1,
UNNEST(locale) locale WITH OFFSET pos2
WHERE pos1 = pos2
like image 126
Mikhail Berlyant Avatar answered Apr 27 '23 18:04

Mikhail Berlyant