Skip to content
Advertisement

How do I exclude a user with a specific criteria where every row has the criteria for that user id?

So I have a list of users who every time they have a feature created, a new row with their ID will be created. I would like to exclude all users that have feature B enabled.

USER   FEATURE
100    A
100    B
200    A
200    C
200    D

I can’t do select USER from TABLE where FEATURE =! 'B' because that will still show USER 100. Any other solution?

Advertisement

Answer

Assuming all users are in this table, you can use aggregation:

select user
from t
group by user
having sum(case when feature = 'B' then 1 else 0 end) = 0;

If users are actually in another table, I would suggest:

select u.*
from users u
where not exists (select 1
                  from user_features uf
                  where uf.user = u.user and uf.feature = 'B'
                 );
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement