简体   繁体   English

如何使用 BigQuery 过滤多个值

[英]How to filter multiple values using BigQuery

I have a BigQuery table with numbers and values.我有一个包含数字和值的 BigQuery 表。 Each number has several values.每个数字都有几个值。 I want to get list of numbers where no one negative value.我想获得没有负值的数字列表。

SELECT 1 as id, 10 as number, 0.2 as value
UNION ALL 
SELECT 1, 10, 0.3
UNION ALL 
SELECT 1, 10, 0.4
UNION ALL 
SELECT 1, 10, 0.4
UNION ALL 
SELECT 1, 11, 0.3
UNION ALL 
SELECT 1, 11, -0.3
UNION ALL 
SELECT 1, 11, 0.1
UNION ALL 
SELECT 1, 12, 0.83
UNION ALL 
SELECT 1, 12, 0.16
UNION ALL 
SELECT 1, 12, 2.3
UNION ALL 
SELECT 1, 12, 0.3

I this case I need to get numbers 10 and 12 only because one value of number 11 is negative.在这种情况下,我只需要获得数字 10 和 12,因为数字 11 的一个值为负数。 I know how to make it using EXCEPT statement.我知道如何使用EXCEPT语句来实现它。 Maybe there is some more effective way, because original table has more than 70 millions rows.也许有一些更有效的方法,因为原始表有超过 7000 万行。

You could use an aggregation approach here:您可以在此处使用聚合方法:

SELECT number
FROM yourTable
GROUP BY number
HAVING COUNT(CASE WHEN value < 0 THEN 1 END) = 0;

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

#standardSQL
SELECT number
FROM `project.dataset.table`
GROUP BY number
HAVING COUNTIF(value < 0) = 0   

If to apply to sample data from your question - output is如果适用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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