1
|Rownumber  |OldIdassigned  |commoncode  |
------------------------------------------
| 1         |FLEX           |Y2573F102   |
------------------------------------------
| 2         |RCL            |Y2573F102   |
------------------------------------------
| 3         |FLEX           |Y2573F102   |
------------------------------------------
| 4         |QGEN           |N72482123   |
------------------------------------------
| 5         |QGEN           |N72482123   |
------------------------------------------
| 6         |QGEN           |N72482123   |
------------------------------------------
| 7         |RACE           |N72482123   |
------------------------------------------
| 8         |CLB            |N22717107   |
------------------------------------------
| 9         |CLB            |N22717107   |
------------------------------------------
<b>| 10     |CLB            |N22717107   |


I need to delete the duplicate records based on Common code and a condition that - if oldidassigned is same then delete else don't delete.

For example Y2573F102 has 3 duplicate records rows 1,2,3 .... 1,2 need not to be deleted , only 3rd row has to be deleted.

5 Answers 5

5

I like updatable CTEs and window functions for this purpose:

with todelete as (
      select t.*,
             row_number() over (partition by commoncode order by rownumber) as seqnum
      from t
     )
delete todelete
    where seqnum > 1;
Sign up to request clarification or add additional context in comments.

1 Comment

@chadalavadaharish . . . I misread the question. You state the condition is on "two columns". But actually, you simply want the oldest record for the third column. This is fixed by removing the second column form the order by.
2

Use ROW_NUMBER() :

DELETE t
FROM (SELECT t.*, ROW_NUMBER() OVER (PARTITION BY OldIdassigned, commoncode ORDER BY rownumber) AS Seq
      FROM table t
     ) t
WHERE t.seq > 1;

EDIT : If you want to check the duplication based on commoncode only then remove OldIdassigned from PARTITION clause :

DELETE t
FROM (SELECT t.*, ROW_NUMBER() OVER (PARTITION BY commoncode ORDER BY rownumber DESC) AS Seq
      FROM table t
     ) t
WHERE t.seq > 1; 

1 Comment

@chadalavadaharish. . . I think you just need commoncode in partition clause.
2

use window function row_number, according to your description and comments it seems you need change in partition clause

delete t
from
(select t1.*,row_number() over(partition by commoncode order by  Rownumber) rn from table t1
)t where rn<>1

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=eacc0688efb534a0addee68678f323fe

2 Comments

@chadalavadaharish dbfiddle.uk/… check this link
@chadalavadaharish does this your output Rownumber OldIdassigned commoncode 1 FLEX Y2573F102 4 QGEN N72482123 8 CLB N22717107
2

Use Row_Number()

delete t from 
(select *, row_number() over(partition by commoncode order by 
 rownumber) as rn) t 
 where rn<>1

Comments

1

Since all answers are similar (and correct), I will post one alternative way:

DELETE FROM TableA
WHERE EXISTS ( SELECT * FROM TableA AS A2
               WHERE A2.commoncode = TableA.commoncode
                 AND A2.OldIdassigned = TableA.OldIdassigned
                 AND A2.Rownumber < TableA.Rownumber )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.