Search code examples
sqlarraysgoogle-bigqueryunnestlateral-join

Split multiple columns into rows by delimiter comma


I have one table (Bigquery), which has ', ' separated values in two columns:

  id    tag_id    tag_name
    1   1, 2, 4   a, b, d
    2   3         c
    3   1, 4      a, d

For each tag_id is 1:1 matched with a tag_name. I want to split the comma separated values and convert them into rows. The ideal output is like:

id    tag_id    tag_name
1          1    a
1          2    b
1          4    d
2          3    c
3          1    a
3          4    d

My working progress SQL:

SELECT * EXCEPT(t, tn) REPLACE(t AS tag_id, tn AS tags_name)
FROM `table`,
UNNEST(SPLIT(tag_id, ', ')) t, UNNEST(SPLIT(tags_name, ', ')) tn

But it seems that tag_id is not 1:1 matched with tags_name... I'd like to use the EXCEPT in the select query as there are many other columns which I don't want to list them all out manually.


Solution

  • One option is to retaining the offset of each element in each list, so we can use that information to join the relevant rows once they are unnested:

    select t.id, elt_id, elt_name
    from mytable t
    cross join unnest( split(t.tag_id,   ', ') ) elt_id   with offset as rn_id
    cross join unnest( split(t.tag_name, ', ') ) elt_name with offset as rn_name
    where rn_id = rn_name