#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
我输了第三排。 然后,我看到了谷歌的官方文档,其中指出了以下内容:
UNNEST treats NULL as follows.
・NULL and empty ARRAY generate zero rows.
・An ARRAY containing NULL generates a row containing a NULL value.
但我不想丢失我的空行。
如何保留空行?
请告诉我解决方案...
不要使用CROSS JOIN,而是使用 LEFT JOIN。这将返回空数组的空值行。您可能还对文档中的使用数组主题感兴趣。
#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;