[英]Returning only non-null key/value in bigQuery
I have an object in bigQuery that stores all possible parameters in the system.我在 bigQuery 中有一个 object,它存储了系统中所有可能的参数。 Therefore somewhere under the hood the 'parameter' object, has a lot of keys, and when I build a query to
SELECT param
it returns a lot of columns with null, and maybe only 1 with a value, which makes it impossible to analyze as the output table is incredibly wide.因此,在引擎盖下的某个地方,“参数”object 有很多键,当我构建对
SELECT param
的查询时,它会返回很多带有 null 的列,并且可能只有 1 个具有值,这使得无法分析为output 表非常宽。
How can I write the query so that it returns 1 column, with only the non null key/value pair?我如何编写查询以使其返回 1 列,只有非 null 键/值对?
ie IE
instead of returning:而不是返回:
param.phone
, param.lob
, param.destination
, param.id
, param.1
, param.2
etc with null
values param.phone
、 param.lob
、 param.destination
、 param.id
、 param.1
、 param.2
等具有null
值
i want to see one column with value {"e_line_of_business":"inte.net"}
or any other non-null key/values.我想看到一个值为
{"e_line_of_business":"inte.net"}
或任何其他非空键/值的列。 It's ok to be stringified.可以被字符串化。
You might consider below approach.您可能会考虑以下方法。
WITH sample_data AS (
SELECT STRUCT(STRING(null) AS phone, STRING(null) AS lob, STRING(null) AS destination, 'internet' AS e_line_of_business, STRING(null) AS param1) params
UNION ALL
SELECT STRUCT(STRING(null) AS phone, STRING(null) AS lob, STRING(null) AS destination, 'internet' AS e_line_of_business, 'value_1' AS param1)
UNION ALL
SELECT STRUCT('01012345678' AS phone, 'web' AS lob, STRING(null) AS destination, null AS e_line_of_business, null AS param1)
)
SELECT params, REPLACE(REGEXP_REPLACE(TO_JSON_STRING(params), r'"[^,{]+"\:null,?', ''), ',}', '}') non_nulls
FROM sample_data;
Query results查询结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.