gpt4 book ai didi

sql - Google BigQuery,使用 'unnest' 函数时丢失了空行

转载 作者:行者123 更新时间:2023-12-02 10:18:14 24 4
gpt4 key购买 nike

#StandardSQL
WITH tableA AS (
SELECT ["T001", "T002", "T003"] AS T_id, [1, 5] AS L_id
UNION ALL
SELECT ["T008", "T009"] AS T_id, NULL AS L_id
)

SELECT * FROM tableA, UNNEST(L_id) AS unnest

当我执行这段代码时,我期望得到如下结果。

RowNumber  T-id            L-id  unnest
1 T001,T002,T003 1,5 1
2 T001,T002,T003 1,5 5
3 T004,T005 NULL NULL

但我得到了这个结果:

RowNumber  T-id            L-id  unnest
1 T001,T002,T003 1,5 1
2 T001,T002,T003 1,5 5

我失去了第三排。然后,我看到Google官方文档,里面是这么说的:

UNNEST treats NULL as follows.
・NULL and empty ARRAY generate zero rows.
・An ARRAY containing NULL generates a row containing a NULL value.

但我不想丢失空行。

如何保留空行?

请告诉我解决方案...

最佳答案

使用 LEFT JOIN 代替 CROSS JOIN。这将为空数组返回一个包含 null 的行。您可能还对 working with arrays topic 感兴趣来自文档。

#StandardSQL
WITH tableA AS (
SELECT ["T001", "T002", "T003"] AS T_id, [1, 5] AS L_id
UNION ALL
SELECT ["T008", "T009"] AS T_id, NULL AS L_id
)
SELECT * FROM tableA
LEFT JOIN UNNEST(L_id) AS value;

关于sql - Google BigQuery,使用 'unnest' 函数时丢失了空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44918108/

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