从表中排除一组数据

时间:2019-10-24 14:27:28

标签: sql google-bigquery

我正在尝试从“ Fruit_Table”表中过滤出某个值:

    Product     Product Type 

     Apple         Fruit 
     Apple         Fruit 
     Apple         Fruit_2
     Apple         Fruit_2 
     Banana        Fruit 
     Banana        Fruit
     Pear          Fruit
     Pear          Fruit 
     Pear          Fruit_2 
     Pear          Fruit_2

所以我想删除Apple和Pear = Fruit的所有行。

理想情况下,我的表将如下所示:

    Product     Product Type 


     Apple         Fruit_2
     Apple         Fruit_2 
     Banana        Fruit 
     Banana        Fruit 
     Pear          Fruit_2 
     Pear          Fruit_2

我尝试了Where NOT IN子句:

Select *
from Fruit_Table
WHERE Product NOT IN ( 'Apple' , 'Pear') AND Product_Type = "Fruit" 

但是,这消除了我表中所有的Apple和Pear值,即我只剩下“香蕉,水果”了。

我尝试了IF语句,但是不太确定这将如何工作。在理想的世界里,我会写;

IF Product = Apple or Pear AND Product_Type = Fruit then EXCLUDE 

^不要以为存在,而是类似的东西!

如果有人能帮上忙,那就太好了。

谢谢, 曼尼莎

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
WITH `project.dataset.Fruit_Table` AS (
  SELECT 'Apple' Product, 'Fruit' Product_Type UNION ALL 
  SELECT 'Apple', 'Fruit' UNION ALL 
  SELECT 'Apple', 'Fruit_2' UNION ALL
  SELECT 'Apple', 'Fruit_2' UNION ALL 
  SELECT 'Banana', 'Fruit' UNION ALL 
  SELECT 'Banana', 'Fruit' UNION ALL
  SELECT 'Pear', 'Fruit' UNION ALL
  SELECT 'Pear', 'Fruit' UNION ALL 
  SELECT 'Pear', 'Fruit_2' UNION ALL 
  SELECT 'Pear', 'Fruit_2' 
)
SELECT *
FROM `project.dataset.Fruit_Table`
WHERE NOT (Product IN ('Apple', 'Pear') AND Product_Type = 'Fruit')    

有结果

Row Product Product_Type     
1   Apple   Fruit_2  
2   Apple   Fruit_2  
3   Banana  Fruit    
4   Banana  Fruit    
5   Pear    Fruit_2  
6   Pear    Fruit_2