如何解决如何仅选择具有不包括 BigQuery 中值为 0 的列的值的行
我有包含 user_id、photo_taken、photo_uploaded 和 photo_upload_error 列的数据集。对于每个用户,photo_taken、photo_uploaded 和 photo_upload_error 都有计数。喜欢Picture of data
--------------------------------------------------------------
user_id| photo_taken|phot_uploaded|photo_upload_erro|
-------------------------------------------------------------
34645654645| 6 | 7 | 9 |
65543545435| 0 | 0 | 0 |
65455545435| 0 | 0 | 0 |
44553535435| 1 | 1 | 1 |
--------------------------------------------------------------
我想取具有值的列,我想排除具有值 0 的列。
user_id| photo_taken|phot_uploaded|photo_upload_erro|
-------------------------------------------------------------
34645654645| 6 | 7 | 9 |
44553535435| 1 | 1 | 1 |
--------------------------------------------------------------
解决方法
您似乎想排除 (rows ,其中所有值都是 0
。如果是这种情况:
select t.*
from t
where photo_taken > 0 or phot_uploaded > 0 or photo_upload_erro > 0;
实际上,不清楚是否要过滤掉all值为0
或any值的行。以上没有任何值。对于所有值,将 or
更改为 and
。
如何只选择具有值的行...
考虑以下选项 - 它不需要明确提及要检查的所有列,如果您有多个列,这会非常方便
select *
from `project.dataset.table` t
where translate(format('%t',(select as struct * except(user_id) from unnest([t]))),'0,','')!='()'
如果应用于您问题中的样本数据 - 输出为
,我想排除所有行中值为 0 [ ... 的列 ...]
考虑以下
execute immediate (
select 'select ' || string_agg(col,' order by offset) || ' from `project.dataset.table`'
from (
select distinct offset,col,logical_and(val = '0') over(partition by col) all_zeroes
from `project.dataset.table` t,unnest(split(translate(to_json_string(t),'{}"',''))) kv with offset,unnest([struct(split(kv,':')[offset(0)] as col,split(kv,':')[offset(1)] as val)])
)
where not all_zeroes
)
如果适用于以下数据
输出将是
如您所见 - 列 photo_taken
被排除,因为它全为零