Reputation: 13
I'm using Sql Server 2008 and trying to achieve as below:
Existing Table:
+--------+-------------+--------+
| ID | COUNTRY | STATUS |
+--------+-------------+--------+
| 1 | UK | 0 |
| 2 | UK | 1 |
| 3 | USA | 0 |
| 4 | Greece | 0 |
| 5 | Italy | 1 |
+-------------------------------+
I'm trying to query Where STATUS =0 and at the same time exclude all those rows if same Country has two different STATUS (Exclude UK in above table in my case)
Expected Result:
+--------+-------------+--------+
| ID | COUNTRY | STATUS |
+--------+-------------+--------+
| 3 | USA | 0 |
| 4 | Greece | 0 |
+-------------------------------+
Thanks in advance !
Upvotes: 0
Views: 763
Reputation: 28403
Try this
SELECT *
FROM Table1
WHERE STATUS = 0 AND
Country NOT IN (SELECT Country FROM Table1 WHERE Status = 1)
Outpit:
+--------+-------------+--------+
| ID | COUNTRY | STATUS |
+--------+-------------+--------+
| 3 | USA | 0 |
| 4 | Greece | 0 |
+-------------------------------+
Upvotes: 0
Reputation: 18629
Please try:
select * From tbl x
where STATUS=0 and
(select COUNT(*) from tbl y
where y.COUNTRY=x.COUNTRY and y.STATUS=1)=0
Upvotes: 1