提问人:Simon Breton 提问时间:11/8/2023 更新时间:11/8/2023 访问量:26
BigQuery 中没有联接键的 PIVOT 嵌套数组
PIVOT nested array in BigQuery without join key
问:
我看到一些类似的问题,但找不到适合我问题的答案。我认为我正在努力解决的是我想要取消嵌套的数组没有任何连接键(或者我可能不明白我面临的问题)。
所以我有这个数据集:
with raw as (
select
['a','b','c','d'] as keys,
[1,2,3,4] values
union all
select
['a','b','c','d'] ,
[5,6,7,8]
)
select
*
from
raw
我希望每个键都有一列,它们的相应值作为行。
到目前为止,我有这样的东西,但我有点卡住了。我想我需要在某个时候使用偏移量,但我不确定如何以及在哪里使用......
with raw as (
select
['a','b','c','d'] as keys,
[1,2,3,4] values
union all
select
['a','b','c','d'] ,
[5,6,7,8]
)
select
*
from (
select
*
from raw,
unnest(keys) as un_keys,
unnest(values) as un_values
)
pivot(max(un_values) for un_keys in ('a','b'))
答:
1赞
Mikhail Berlyant
11/8/2023
#1
以下应该会有所帮助
WITH raw AS (
SELECT ['a','b','c','d'] AS keys, [1,2,3,4] VALUES UNION ALL
SELECT ['a','b','c','d'], [5,6,7,8]
)
SELECT * EXCEPT(line) FROM (
SELECT line, key, VALUE FROM (
SELECT *, ROW_NUMBER() OVER() AS line FROM raw
), UNNEST(keys) AS key WITH OFFSET
JOIN UNNEST(VALUES) AS VALUE WITH OFFSET
USING (OFFSET))
PIVOT(ANY_VALUE(VALUE) FOR key IN ('a', 'b', 'c', 'd'))
带输出
评论