I have below mentioned table:
ID Value
KA-1 A
KA-1 B
KA-1 C
KA-1 D
KA-2 A
KA-2 C
KA-2 C
KA-2 D
KA-3 C
KA-3 B
I want to fetch those ID
where there is at least one value corresponding to it is D
But the same ID
don't have value B
.
Required Output:
ID
KA-2
An easy solution would be:
select distinct id from table
where value='D' and id not in (select distinct id from table where value='B')
SELECT ID
FROM YOUR_TABLE
GROUP BY ID
HAVING SUM(VALUE='D')>=1 AND SUM(VALUE='B')=0;
See a working DEMO on SQL Fiddle .
You can try with exists and not exists:
select id
from table1 as a
where exists (select *
from table1 b
where b.value='D' and b.id=a.id)
and not exists (select *
from table1 c
where c.value='B' and c.id=a.id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.