Google BigQuery,我在使用 'unnest' 函数时丢失了空行
Google BigQuery, I lost null row when using 'unnest' function
#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.
但我不想丢失空行。
如何保留空行?
请告诉我解决方法...
不要使用 CROSS JOIN,而是使用 LEFT JOIN。这将为空数组 return 一行包含空值。您可能还对文档中的 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;
#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.
但我不想丢失空行。
如何保留空行?
请告诉我解决方法...
不要使用 CROSS JOIN,而是使用 LEFT JOIN。这将为空数组 return 一行包含空值。您可能还对文档中的 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;