Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All
Need your help.

I have one table below..

PK id status
1 1 01
2 1 02
3 2 01
4 3 01
5 3 02

I want to select thoese records where status equal to 01 but status !=02 whose id is same.
the expected output will be..

Pk id status
3 2 01
Posted
Comments
Maciej Los 18-May-15 10:42am    
What have you tried? Where are you stuck?
SELECT t1.PK, t1.id, t1.status, t2.status status2
FROM TableName t1 INNER JOIN TableName t2 ON t1.id = t2.Id AND t1.PK<t2.PK
WHERE status='01' and status2!='02'

try this:
SQL
SELECT  *
FROM    Table1 t1
WHERE   status = '01'
    AND NOT EXISTS
    (
    SELECT  1
    FROM    table1 t2
    WHERE   status = '02'
        AND t1.id = t2.id
    )
 
Share this answer
 
Hi,

Check this...

SQL
Select * from table_1 Where status = '01' and id not in (SELECT distinct id from table_1 Where status = '02')


Hope this will help you.

Cheers
 
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