Reputation: 586
i have a table like this,
i want to select all doc no having status 1.
select * from tbl where doc_status=1
will select 101,102,101. But in for doc_no 101 there is doc-status 0 also so it wont select.How can i do that, desired output will be
103 only
Upvotes: 2
Views: 269
Reputation: 1443
Try
SELECT * FROM test GROUP BY doc_no HAVING MIN(doc_status) = 1;
Upvotes: 1
Reputation: 13519
You can use NOT EXISTS
SELECT
DISTINCT T.doc_no
FROM tbl T
WHERE NOT EXISTS
(
SELECT
1
FROM tbl TT
WHERE TT.doc_status = 0
AND TT.doc_no = T.doc_no
)
Note: I guess doc_status
can hold value either 0/1
. If this is the case then no need to filter with doc_status = 1
outside.
OR you can use NOT IN
SELECT
DISTINCT T.doc_no
FROM tbl T
WHERE T.doc_no NOT IN
(
SELECT
TT.doc_no
FROM tbl TT
WHERE TT.doc_status = 0
)
Upvotes: 2