How to only SELECT rows with non-zero and non-null columns efficiently in Big Query?

天大地大妈咪最大 提交于 2020-12-13 09:28:07

问题


I am having a table with large number of columns in Big Query.

The table has lot of rows with some column values as 0/0.0 and null.

For example

Row A      B    C     D     E   F
1   "abc"  0   null  "xyz"  0   0.0
2   "bcd"  1    5    "wed"  4   65.5

I need to select only those rows which have non zero Integer, Float and non NULL values. Basically, I need just Row 2 in the above table

I know I can do this by using this query for each of the columns

SELECT * FROM table WHERE (B IS NOT NULL AND B is !=0) AND
.
.
.

But I have lot of columns and writing query like this for each of the columns would be difficult. Is there any better approach to handle this?


回答1:


Below example for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT "abc" a, 0 b, NULL c, "xyz" d, 0 e, 0.0 f UNION ALL
  SELECT "bcd", 1, 5, "wed", 4, 65.5 
)
SELECT *
FROM `project.dataset.table` t
WHERE NOT REGEXP_CONTAINS(TO_JSON_STRING(t), r':0[,}]|null[,}]')  

with output

Row a   b   c   d   e   f    
1   bcd 1   5   wed 4   65.5    


来源:https://stackoverflow.com/questions/57822709/how-to-only-select-rows-with-non-zero-and-non-null-columns-efficiently-in-big-qu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!