Reputation: 93
I have a big table that has duplicates as such:
number
(primary key), group
(primary key), Division
(primary key), dateChange
.Example:
1,2,3,20121015
1,2,3,20120101
1,2,3,20110101
2,2,2,20121010
2,2,2,20120101
result should be:
1,2,3,20121015
2,2,2,20121010
I have tried many combinations including group by the primary key with minimum "changeDate" but nothing seems to work perfectly.
I want to have something like this:
delete from table where (number,group.devision,changeDate) not in
(select from table(number,group,devision,Max(changeDate))
group by (number,group.devision)
But I dont think it is a valid MS-SQL syntax.
Your help will be very appreciated!!
Upvotes: 2
Views: 2111
Reputation: 23364
The following should work.
delete table from
table inner join (select
number, group, division, changeDate, row_number() over
(partition by number, group, division order by changeDate desc) as ranker
from table) Z
on table.number = Z.number and table.group = Z.group and
table.changeDate = Z.changeDate and Z.ranker != 1
Upvotes: 0
Reputation: 453298
To delete all rows except for the latest for a number, group, Division
combination.
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY number, group, Division
ORDER BY dateChange DESC) RN
FROM YourTable)
DELETE FROM cte
WHERE RN > 1
Upvotes: 4