views:

34

answers:

2

I have a table with the following fields. My requirement is to delete the records from the table when 1. The Flag is set to 'No' 2. For a title, in one record if the flag is set to 'No' and in another record if it is set to 'Yes'. Delete both the records. For example TH-123, In 1st record the flag is set to 'No' and in 4th record flag is set 'Yes' Delete both the records. 3. For a title, in one record if the flag is set to 'No' and in another record if it is set to ''(space). Delete both the records. For example TH-344. Delete both the records.

   Title                        Flag     

    TH-123                        No
    TH-344                        
    TH-543                        No
    TH-123                        Yes
    Th-344                        No
    Th-098                        Yes
    Th-876                        Yes
    Th-098                        Yes
    Th-512                        No

so now i am expecting a result, with the following records.

    Th-098                        Yes
    Th-876                        Yes
    Th-098                        Yes

I have done this with 2 queries.

update temp1 set [flag] = 'No' WHERE Title IN 
(SELECT Title FROM temp1 where [flag] = 'No')

delete from temp1 where [flag] = 'No'

Is it possible to do with a single query. If so please let me know or any other better procedure..

+4  A: 
delete from temp1 
where 
    Title IN 
        (SELECT Title FROM temp1 where [flag] = 'No')
Alexander
Thanku. query is smart.
lucky
+1  A: 

This should works:

delete from t1
from temp1 t1
  inner join temp2 t2 on t1.Title = t2.Title
where t2.[flag] = 'No'
outcoldman