在 BigQuery 中展平多个相同大小的数组列 table
Flatten multiple same-sized array columns in BigQuery table
我有一个 table 有几个列,其中一些是相同长度的数组。我想解除它们的嵌套以获得结果,其中数组的值位于不同的行中。
所以有 table 喜欢这个:
我想去:
这是其中一个数组列的工作方式:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1
FROM data,
UNNEST(array_1) as a1
有什么优雅的方法可以同时解除两个数组的嵌套吗?我想避免分别取消嵌套每一列然后将所有内容连接在一起。
您可以使用 with offset
和 join
:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1, a2
FROM data cross join
UNNEST(array_1) as a1 with offset n1 JOIN
UNNEST(array_2) as a2 with offset n2
on n1 = n2
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT id, a1, a2
FROM data, UNNEST(array_1) AS a1 WITH OFFSET
JOIN UNNEST(array_2) AS a2 WITH OFFSET
USING(OFFSET)
所以我自己做了一些关于在 SQL 中取消嵌套的研究,并提出了这个解决方案:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1, array_2[OFFSET(off)] AS a2
FROM data
CROSS JOIN UNNEST(array_1) AS a1 WITH OFFSET off
优点是不需要取消嵌套所有数组,只需要取消嵌套一个即可。
我有一个 table 有几个列,其中一些是相同长度的数组。我想解除它们的嵌套以获得结果,其中数组的值位于不同的行中。
所以有 table 喜欢这个:
我想去:
这是其中一个数组列的工作方式:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1
FROM data,
UNNEST(array_1) as a1
有什么优雅的方法可以同时解除两个数组的嵌套吗?我想避免分别取消嵌套每一列然后将所有内容连接在一起。
您可以使用 with offset
和 join
:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1, a2
FROM data cross join
UNNEST(array_1) as a1 with offset n1 JOIN
UNNEST(array_2) as a2 with offset n2
on n1 = n2
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT id, a1, a2
FROM data, UNNEST(array_1) AS a1 WITH OFFSET
JOIN UNNEST(array_2) AS a2 WITH OFFSET
USING(OFFSET)
所以我自己做了一些关于在 SQL 中取消嵌套的研究,并提出了这个解决方案:
WITH data AS
(
SELECT 1001 as id, ['a', 'b', 'c'] as array_1, [1, 2, 3] as array_2
UNION ALL
SELECT 1002 as id, ['d', 'e', 'f', 'g'] as array_1, [4, 5, 6, 7] as array_2
UNION ALL
SELECT 1003 as id, ['h', 'i'] as array_1, [8, 9] as array_2
)
SELECT id, a1, array_2[OFFSET(off)] AS a2
FROM data
CROSS JOIN UNNEST(array_1) AS a1 WITH OFFSET off
优点是不需要取消嵌套所有数组,只需要取消嵌套一个即可。