小编典典

Google BigQuery,使用“ unnest”功能时我丢失了空行

sql

#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.
 銉籒ULL and empty ARRAY generate zero rows.
 銉籄n ARRAY containing NULL generates a row containing a NULL value.

但是我不想丢失我的空行。

如何保留空行?

请告诉我解决方案…


阅读 230

收藏
2021-04-22

共1个答案

小编典典

代替CROSS JOIN,使用LEFT
JOIN。这将为空数组返回一行包含null的行。您可能也对文档中的使用数组主题感兴趣。

#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;
2021-04-22