SELECT *,
RN = ROW_NUMBER() OVER (PARTITION BY Code ORDER BY CreatedDate Desc)
FROM dbo.dw_emp
)
DELETE FROM CTE WHERE RN > 1
Table dw_emp Structure
------------------------------------------------------------------------------------------------
Code | Name | Address | CreatedDate |
10001 | Mark | Tai Seng Street | 1/12/2012 |
10002 | Jack | Jurong East St 11 | 12/12/2011 |
10001 | Mark | Tai Seng Street | 12/5/2013 |
If need delete the duplicated one, and remain the one whose created date is biggest, you can use the script above.
No comments:
Post a Comment