tags:

views:

576

answers:

3

Using SQL, I have 5 columns: ssn, lastname, firstname, RF and a flag field. I need to go through this table and where the 4 columns are equal to another row and the value of the flag field in that row is equal to 2050, then delete that 2050 record.

+2  A: 
delete from MyTable m
where flag = 2050
and exists (
    select 1 from MyTable where
    MyTable.ssn = m.ssn 
        and MyTable.lastname=m.lastname 
        and MyTable.firstname=m.firstname 
        and MyTable.RF=m.RF 
        and MyTable.flag <> 2050
)
RedFilter
+1  A: 
delete from TableName as tn
where tn.flag = 2050 and
exists (select * from TableName as tn2 where tn.ssn = tn2.ssn
        and tn.lastname = tn2.lastname and tn.firstname = tn2.firstname
        and tn.rf = tn2.rf and tn2.flag <> 2050)
Yaakov Ellis
+1  A: 
DELETE
     T1
FROM
     My_Table T1
INNER JOIN My_Table T2 ON
     T2.ssn = T1.ssn AND
     T2.last_name = T1.last_name AND
     T2.first_name = T1.first_name AND
     T2.RF_name = T1.RF_name AND
     T2.flag <> T1.flag
WHERE
     T1.flag = 2050
Tom H.