2

i have a table like this,

enter image description here

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

2
  • plz try this. select * from tbl where doc_status='1' what is data type of doc_status Commented Aug 16, 2016 at 6:07
  • Select x.* from my_table x left join my_table y on y.doc_no = x.doc_no and y.status <> 1 where x.status = 1 and y.id is null Commented Aug 16, 2016 at 6:59

2 Answers 2

2

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
)

Working Demo

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
)
Sign up to request clarification or add additional context in comments.

Comments

1

Try

SELECT * FROM test GROUP BY doc_no HAVING MIN(doc_status) = 1;

SQL Demo

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.