Skip to content
Advertisement

How to exclude users with multiple options sql

Trying to retrieve just users that don’t have a disabled campaign, where disabled = 1.

A user can have a disabled campaign and a non-disabled campaign, but if they have any disabled campaigns I want to exclude them from my final result.

Thinking I need something like

SELECT DISTINCT
   user_id,
   CASE
      WHEN
         disabled = 1 
      THEN
         'Disabled' 
      ELSE
         'Good' 
   END
   AS campaign_disabled

But this just returns two rows for each user_id, one being Good and the other campaign_disabled

Advertisement

Answer

You want the users that have disabled = 0 for all their campaigns, so the max value of the column disabled must be 0:

SELECT user_id
FROM tablename
GROUP BY user_id
HAVING MAX(disabled) = 0
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement