gpt4 book ai didi

arrays - Bigquery - 如何将两个数组压缩为一个?

转载 作者:行者123 更新时间:2023-12-01 11:20:28 26 4
gpt4 key购买 nike

如果我知道 BigQuery 中有两个大小相等的数组。如何将它们压缩到一个结构数组或两个元素数组或类似数组中?

以下查询为我提供了 x 和 y 的所有可能组合,这不是我想要的。

WITH test AS (
SELECT
['a', 'b', 'c'] as xs,
[1, 2, 3] as ys
)
SELECT struct(x, y) as pairs
FROM test, unnest(xs) as x, unnest(ys) as y

我想得到这样的东西:
+--------+--------+
| pair.x | pair.y |
+--------+--------+
| a | 1 |
| b | 2 |
| c | 3 |
+--------+--------+

最佳答案

使用 WITH OFFSET和括号运算符:

WITH test AS (
SELECT
['a', 'b', 'c'] as xs,
[1, 2, 3] as ys
)
SELECT struct(x, ys[OFFSET(off)] as y) as pairs
FROM test, unnest(xs) as x WITH OFFSET off;

关于arrays - Bigquery - 如何将两个数组压缩为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com