BigQuery SQL Exclusion NOT IN empty results

前端 未结 2 1220
情歌与酒
情歌与酒 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: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;
    

提交回复
热议问题