谷歌BigQuery,我在使用' unfst'时丢失了空行。功能

时间:2017-07-05 06:03:57

标签: sql null google-bigquery unnest

#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.

但我不想丢失我的空行。

如何保存空行?

请告诉我解决方案......

1 个答案:

答案 0 :(得分:8)

使用LEFT JOIN代替CROSS JOIN。这将为空数组返回一个带空值的行。您可能还对文档中的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;