[英]Extract nested values as columns Google BigQuery?
我想获取值,将键作为没有多个交叉连接的列。 IE
SELECT
owner_id,
owner_type,
domain,
metafields.value AS name,
metafields.value AS image,
metafields.value AS location,
metafields.value AS draw
FROM
example_table
显然,以上内容不适用于此,但需要以下 output:
在实际表中,每个 owner_id 有数百个元字段,还有数百个 owner_id 和 owner_types。 owner_types 与其他表的多次连接很好,但对于相同的所有者类型,我不想多次连接。
基本上,我需要能够 select 列对应的键,并显示该列的相关值。 没有,必须显示每个可用的元字段。
有什么办法吗?
您可以使用子查询和SAFE_offset
语句并从特定位置的数组中获取值。 此外,您需要使用STRING_AGG
,它返回通过连接非空值获得的值(STRING 或 BYTES)。
根据您分享的信息,您可以使用下面的查询。
使用此代码,您将获得用逗号分隔的所有列:
WITH sequences AS
(
SELECT 1 as ID,"product" AS owner_type,"beta.com" AS domain,["name","image","lcation","draw"] AS metalfields_key, ["big","pic.png","utha","1"] AS metalfields_value
),
Val as(
SELECT distinct id, owner_type,domain, value FROM sequences, sequences.metalfields_value as value, sequences.metalfields_key
), text as(
SELECT
id, owner_type, domain,
STRING_AGG(value ORDER BY value) AS Text
FROM Val
GROUP BY owner_type, domain, id
)
在此代码中,您将获取以逗号分隔的每个元素并按列返回它们。
SELECT DISTINCT t1.id, t1.owner_type,domain,
split(t1.text, ',')[SAFE_offset(1)] as name,
split(t1.text, ',')[SAFE_offset(2)] as image,
split(t1.text, ',')[SAFE_offset(3)] as location,
split(t1.text, ',')[SAFE_offset(0)] as draw
from text as t1
你可以看到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.