简体   繁体   中英

Struct values to columns in big query

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM