Makarand
Makarand

Reputation: 1

Use record rows as columns BigQuery

Suppose I have a table with rows like this table, where each row of the table has records in an array form.

Using Bigquery, Is there a way I can turn this into a table that has 'a', 'b', 'c' as its own columns, with 100, 46, 29 as the values in the rows?

I.e. turn

Row | abc_keys | abc_intValues
------------------------------
1   | a        | 100
    | b        | 46
    | c        | 29
2   | a        | 101
    | b        | 47
    | c        | 30

into

Row | a   | b  | c
-------------------
1   | 100 | 46 | 29
2   | 101 | 47 | 30

Upvotes: 0

Views: 710

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172994

Consider below approach

select * except(row_hash) from (
  select keys, intValues, to_json_string(t) row_hash
  from your_table t, unnest(abc)
)
pivot (any_value(intValues) for keys in ('a', 'b', 'c'))    

if applied to sample data in your question - output is

enter image description here

Upvotes: 2

Related Questions