1

I have this table (very simplified):

 id | product
------------
  1 | a
  2 | b
  3 | b
  3 | c
  4 | c
  5 | b
  5 | c
  5 | d
  6 | d
  6 | c
  7 | b
  8 | b
  9 | c
 10 | d

I would like to get all ids which have only produkt 'd' or 'c'. For example '5' should not be selected, because it also has product 'b'. The result should be: 4, 6, 9 and 10.

0

1 Answer 1

4

One easy way of approaching such problems is counting the number of forbidden products an ID has:

SELECT   id
FROM     mytable
GROUP BY id
HAVING   COUNT(CASE WHEN product NOT IN ('c', 'd') THEN 1 END) = 0
Sign up to request clarification or add additional context in comments.

2 Comments

I like your solution. +1. I would have done subquery in the Where clause.
Thank you so much! Works great!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.