繁体   English   中英

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

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

我有一个包含 6 个可能电话号码的示例,我需要创建一个数组或 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 中不起作用。 知道为什么会这样吗?

谢谢!

知道为什么会这样吗?

当你做 to_json_string - 所有null s 变成字符串'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'         

与 output

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM