简体   繁体   中英

Returning only non-null key/value in bigQuery

I have an object in bigQuery that stores all possible parameters in the system. 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.

How can I write the query so that it returns 1 column, with only the non null key/value pair?

ie

instead of returning:

param.phone , param.lob , param.destination , param.id , param.1 , param.2 etc with null values

在此处输入图像描述

i want to see one column with value {"e_line_of_business":"inte.net"} or any other non-null key/values. 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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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