Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a table that looks like this


CodeID                   Status
         
         FO1                      FREE
         
         FO2                      NOT FREE
        
         FO1                      FREE
        
         FO1                      FREE
        
         FO1                      FREE

What i want to achieve is check if in the above above all FO1 have status of Free. If are free, i want to carry out and action. I'm using asp.net web form for the application by way. Kindly help me out with how to structure my query to be able to tell if my all records for a particular value are the same

What I have tried:

select count(distinct Status) from TableName where CodeID = 'FO2' and Status ='FREE'



I'm testing with this query in SQL server just to be sure if what i'm doing is right. The above query returns 1 but if i change one of the FO1 to NOT FREE the above code stills returns 1. I want to make it return o.
Posted
Updated 25-Jul-17 4:30am
v3

1 solution

The way I'd do it is to use GROUP BY:
SQL
SELECT COUNT(Status) FROM TableName GROUP BY CodeID, Status HAVING CodeID = 'F01' AND Status != 'FREE'
A value of 0 indicates all F01 are FREE (or there are no F01 codes) any other number is a NOT FREE F01.
 
Share this answer
 

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