Reputation: 1143
I have data table giving following results:
number code
1 A
1 B
1 A
2 B
2 A
3 A
4 B
How do i find distinct number from table having code as A. The number should only have code A in table and not have B in table.
For the above table: My answer would be 3. As it has only code A.
Upvotes: 0
Views: 28
Reputation: 1269563
If you only want A
, one method is aggregation:
select t.number
from t
group by t.number
having min(code) = 'A' and max(code) = 'A';
Upvotes: 1
Reputation: 69504
SELECT DISTINCT a.number
FROM TableName a
WHERE a.Code = 'A'
AND NOT EXISTS (Select 1
FROM TableName b
WHERE a.number = b.number
AND b.Code = 'B')
Upvotes: 0