如何从 ARRAY 中排除 NULL 以便查询不会失败 [英] How to exclude NULLs from ARRAY so query won't fail
本文介绍了如何从 ARRAY 中排除 NULL 以便查询不会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ARRAY_AGG 聚合函数包括NULLs
在它构建的数组中.当此类数组是查询结果的一部分时,查询将失败并显示错误:
ARRAY_AGG aggregate function includes NULLs
in the arrays it builds. When such arrays are part of query result, query fails with error:
数组不能有空元素;写入字段错误
Array cannot have a null element; error in writing field
即以下查询演示了这一点:
i.e. the following query demonstrates it:
#standardSQL
SELECT ARRAY_AGG(x) FROM UNNEST([1,NULL,2,3]) x
我们该如何解决?
推荐答案
很高兴您的提问!BigQuery 在一些聚合函数中支持 IGNORE NULLS
和 RESPECT NULLS
修饰符,包括 ARRAY_AGG
,所以你的查询变成了
Glad you asked! BigQuery supports IGNORE NULLS
and RESPECT NULLS
modifiers in some of the aggregate functions, including ARRAY_AGG
, so your query becomes
#standardSQL
SELECT ARRAY_AGG(x IGNORE NULLS) FROM UNNEST([1,NULL,2,3]) x
并且它通过生成[1,2,3]
.更多细节在 文档.
and it passes producing [1,2,3]
. More details are in the documentation.
这篇关于如何从 ARRAY 中排除 NULL 以便查询不会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文