Search code examples
google-bigquerylogical-operatorsconditional-operator

Filter out rows only when two columns meet two different conditions


Assuming I have a huge table with a bunch of combinations and it looks like this

Row Location Toy Date
1 China A day1
2 China B day1
3 USA A day1
4 USA B day1
5 France B day2
6 France A day1

When pulling data, I need to filter out some of the "pairs" in two columns (Location and Toy). How can I query the table to exclude rows where the values in the columns BOTH meet particular values? I've tried getting multiple ands together but it doesn't work. This is what I tried:

Select * from  TABLE 
where Date = day1 
and ((Location != 'China' and Toy != 'B')) 
and ((Location != 'USA' and Toy != 'A'))
and ((Location != 'France' and Toy != 'A'))

So in this case, I would expect it to return rows 1 and 4, but it does not.

*Note, the script/exceptions are generated from a different code that is scheduled to run multiple times, so they are all dynamic and continuously change. I know I can easily do this in python straight in the dataframe, but I would rather pull the data clean to begin with.


Solution

  • You might try below:

    SELECT * 
      FROM sample_table 
     WHERE Date = 'day1'
       AND (Location, Toy) NOT IN (('China', 'B'), ('USA', 'A'), ('France', 'A'));
    
    +----------+-----+------+
    | Location | Toy | Date |
    +----------+-----+------+
    | China    | A   | day1 |
    | USA      | B   | day1 |
    +----------+-----+------+