Using UNNEST Command in Google BigQuery

Sample Table:

\n\n
ID | startDate | endDate | info1 | info2 | info 3  \n----------------------------------------------------\n1  | 11-12-2000 | 11-12-2010 | Blue   | Circle   | A\n                                      | Triangle | B\n                                      | Square   | \n----------------------------------------------------\n2  | 11-12-2001 | 11-12-2011 | Yellow |    <*>   | C  \n----------------------------------------------------\n3  | 11-12-2007 | 11-12-2008 | Brown  |  Circle  | D\n                                      | Triangle | B    \n----------------------------------------------------\n
\n\n

Solution:

To expand the array columns, you can use the UNNEST Command

\n\n \n\n
#standardSQL\nWITH `project.dataset.MY_DB` AS (\n  SELECT 1 id, '11-12-2000' startDate, '11-12-2010' endDate, 'Blue' info1, ['Circle','Triangle', 'Square'] info2, ['A', 'B'] info3 UNION ALL\n  SELECT 2, '11-12-2001', '11-12-2011', 'Yellow', ['<*>'], ['C'] UNION ALL  \n  SELECT 3, '11-12-2007', '11-12-2008', 'Brown', ['Circle','Triangle'], ['D', 'B'] \n)\nSELECT id, startDate, endDate, info1, info2, info3\nFROM `project.dataset.MY_DB`, UNNEST(info2) info2, UNNEST(info3) info3\nWHERE info2 != '<*>' AND info3 = 'B'\nORDER BY id\n
\n\n

with the result as below

\n\n
Row id  startDate   endDate     info1   info2       info3    \n1   1   11-12-2000  11-12-2010  Blue    Circle      B    \n2   1   11-12-2000  11-12-2010  Blue    Triangle    B    \n3   1   11-12-2000  11-12-2010  Blue    Square      B    \n4   3   11-12-2007  11-12-2008  Brown   Circle      B    \n5   3   11-12-2007  11-12-2008  Brown   Triangle    B    \n

victor

posted on

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds



 Comments are Locked for this post