3

I have a table with names and a bit column indicating whether the name is delted (1) or not (0). I'm trying to write a query that returns all names that are deleted, unless the name is also not deleted (a name may show up more than once in the table). Hope that makes sense!

Here is some sample data:

+------+-------+
|Name  |Deleted|
+------+-------+
|Bob   |   1   |
+------+-------+
|Joe   |   1   |
+------+-------+
|Joe   |   0   |
+------+-------+
|Bob   |   1   |
+------+-------+
|Sam   |   1   |
+------+-------+

So the result would be Bob and Sam:
Both 'Bob' entries are '1'.
Single Sam entry is '1'.

Joe would not be in the results because he is both '1' and '0'.

Thanks for any help!

2 Answers 2

3

One method uses aggregation:

select name
from t
group by name
having min(deleted) = 1;
Sign up to request clarification or add additional context in comments.

1 Comment

Clever! I like it
1
SELECT T.Name
  FROM Table T
 WHERE T.Deleted = 1
   AND NOT EXISTS (SELECT *
                     FROM Table T2
                    WHERE T2.Name = T.Name
                      AND T2.Deleted = 0)

Comments

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.