Home > Blockchain >  Struct values to columns in big query
Struct values to columns in big query

Time:03-29

I have a column "device" which have struct values in every row

device

(row 1)

brand_name brand_id country
huawei 34j0 china

(row 2)

brand_name brand_id country
sony ds5g japan

we can create column from these structs by doing this

SELECT
device.brand_name 
device.brand_id 
device.country 
from table

In this case struct only have three values ( brand_name,brand_id, country) but what if the struct have n number of values , so what i want to do is instead of accessing every value in struct by devicedevice.brand_name,device.brand_id....etc , I want to loop all the values inside it and make it as a column, is there a way to do it? , thank you

CodePudding user response:

You can just use the wildcard symbol *. More specifically:

WITH devices as (
SELECT STRUCT("sony" as brand_name, "ds5g" as brand_id, "japan" as country) as device UNION ALL
SELECT STRUCT("huawei" as brand_name, "34j0" as brand_id, "china" as country) as device
)

SELECT device.* FROM devices
  • Related