从 Bigquery 中不包括 NULL 值的列创建 array/json

Create an array/json from columns excluding NULL values in Bigquery

我有一个包含 6 个可能 phone 数字的示例,我需要创建一个数组或 json 包含所有这些数字,不包括重复项和 NULL。

我的样本是这样的:

WITH material as(
SELECT 619883407 as phone_1,
CAST(null AS INT64) as phone_2,
CAST(null AS INT64)  as phone_3,
CAST(null AS INT64) as phone_4,
69883407 as phone_5,
688234 as phone_6)
SELECT ARRAY_AGG(a IGNORE NULLS) as phones
 FROM material CROSS JOIN UNNEST(JSON_EXTRACT_ARRAY(TO_JSON_STRING([phone_1,phone_2,phone_3,phone_4,phone_5,phone_6]))) a

我对我的结果很满意,但我需要排除 NULL 值。出于某种原因,将 'IGNORE NULLS' 添加到 array_agg 中不起作用。知道为什么会这样吗?

谢谢!

Any idea why would this happen?

当你做 to_json_string - 所有 nulls 变成字符串 'null's

改用下面的方法

select array_agg(a) as phones
from material, 
unnest(json_extract_array(to_json_string([phone_1,phone_2,phone_3,phone_4,phone_5,phone_6]))) a
where a != 'null'         

有输出