BigQuery SQL Exclusion NOT IN empty results

前端 未结 2 1221
情歌与酒
情歌与酒 2021-01-24 03:07

I\'m having problems with this not returning any values. There are accounts in the database that match this criteria. Somewhat confused why they aren\'t being returned. Any sugg

相关标签:
2条回答
  • 2021-01-24 03:31

    Ensure you're referencing the field you don't want to be present in the subquery result:

    select accountid from `table1` 
    where accountid not in (select accountid from `table1` where action != "Action8")
    
    0 讨论(0)
  • 2021-01-24 03:42

    Do not use not in. Semantically, it is counter-intuitive. If any values in the subquery are NULL, then no rows are returned.

    Use not exists instead;

    select t1.accountid
    from `table1` t1
    where not exists (select 1
                      from table1 tt1
                      where tt1.accountid  = t1.accountid and
                            tt1.action <> 'Action8'
                     );
    

    Or use group by and having:

    select t1.accountid
    from table1 t1
    group by t1.accountid
    having sum(case when action = 'Action8' then 1 else 0 end) = 0;
    
    0 讨论(0)
提交回复
热议问题