Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery split array into individual rows

In BigQuery I have done a REGEXP_EXTRACT_ALL query and have results where each row contains multiple lines (an array).

How can query this table to convert each row's array into its own row so that my resulting table is individual rows of each line, instead of an array?

like image 772
d-_-b Avatar asked Aug 28 '17 15:08

d-_-b


People also ask

How do I flatten an array in BigQuery?

To flatten an entire column of ARRAY s while preserving the values of the other columns in each row, use a correlated cross join to join the table containing the ARRAY column to the UNNEST output of that ARRAY column.

How do you Unnest an array in BigQuery?

You can do this with a CROSS JOIN. A cross join will take every individual element of your unnested array and join it back to its parent row. This will create multiple rows for each element of your array but you can then filter it down.

What is array AGG in BigQuery?

ARRAY_AGG. Returns an ARRAY of expression values. To learn more about the optional arguments in this function and how to use them, see Aggregate function calls. To learn more about the OVER clause and how to use it, see Window function calls.


1 Answers

You can apply the unnest operation. As an example:

WITH data AS(
  SELECT ['string1', 'string2'] AS r UNION ALL
  SELECT ['string3', 'string4', 'string5']
)

SELECT
r
FROM data,
UNNEST(r) r
like image 162
Willian Fuks Avatar answered Oct 19 '22 04:10

Willian Fuks