Click here to Skip to main content
15,894,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all , I am new to Sql .Could someone help me in the below problem .
I need to select Batchno whose all status field value is 'Finished' ,if any of the status field value is 'pending' against the Batchno it should not be selected.Any help will be appreciated.

SQL
BatchNo  SID   Status 
2101     123    Finished
2101     457    Pending
2101     478    Finished
2101     8745   Finished
2102     4574   Finished
2102     4578   Finished
2102     4789   Finished
2103     6214   Finished
2103     4236   Pending
2103     7895   Pending                       


The output should be '2102'

What I have tried:

SELECT Batchno FROM Product where Status='Finished' GROUP BY(Batchno) -This selects all the batchno whose status is pending also.
Posted
Updated 10-Jul-17 20:40pm
Comments
Michael_Davies 5-Jul-17 8:20am    
Please show the results of the SQL, for instance 2101 has Finished and Pending so will show.

Try this:

select distinct BatchNo from TABLE as x
where 
((select count(*) Status from TABLE where BatchNo = x.BatchNo) = 
(select count(*) Status from TABLE where BatchNo= x.BatchNo and Status = x.Status ))
 
Share this answer
 
Below is the query..
select distinct BatchNo from groupdata
where BatchNo not in (select BatchNo from groupdata where status = 'Pending')
 
Share this answer
 
SQL

select BatchNo from [UDB_JAX].[dbo].[test] as x
where BatchNo not in
(select BatchNo from [UDB_JAX].[dbo].[test] where BatchNo= x.BatchNo and Status != 'finished' )
group by BatchNo
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900