Search code examples
sqlsplitgoogle-bigquerygoogle-query-language

Split and create an array with Google BigQuery


Starting point:

id  levels
    
11   A,B,C      
12   A
13   B,C

How to split and create an array, expected outcome:

id  levels
    
11   A
11   B
11   C      
12   A
13   B
13   C

Solution

  • You can use split:

    select id,split(levels,',') as levels yourtable
    

    You can also use UNNEST

    SELECT id, levels
    FROM yourtable
    CROSS JOIN UNNEST(levels) AS levels;