[英]Create an array/json from columns excluding NULL values in Bigquery
I have a sample with 6 possible phone numbers and I need to create an array or json that has them all, excluding duplicates and NULLs.我有一个包含 6 个可能电话号码的示例,我需要创建一个数组或 json 来包含所有电话号码,不包括重复项和 NULL。
My sample is something like this:我的样本是这样的:
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
I am happy with my result, but I would need to exclude NULL values.我对我的结果很满意,但我需要排除 NULL 个值。 For some reason, adding 'IGNORE NULLS' into the array_agg is not working.
出于某种原因,将“IGNORE NULLS”添加到 array_agg 中不起作用。 Any idea why would this happen?
知道为什么会这样吗?
Thank you!谢谢!
Any idea why would this happen?
知道为什么会这样吗?
when you do to_json_string - all null
s becomes strings 'null'
s当你做 to_json_string - 所有
null
s 变成字符串'null'
s
Use below instead改用下面
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'
with output与 output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.