There is an easy way to delete duplicate rows.
In a first step, we will sort the records and add a rownumber.
Second step will be deleting rows with rownumber > 1.
WITH CTE AS
(
SELECT *
,ROW_NUMBER() OVER
(PARTITION BY id, updatetime
ORDER BY id, updatetime, UpdateMillisec ASC
) AS RowNum
FROM yourtable
)
SELECT * FROM CTE -- for checking the result before deleting
-- DELETE FROM CTE WHERE RowNum > 1 -- uncomment this row for the final DELETE
Attention:
To identify, which is the first record and which is a following (second, third,..) record, we have to sort the data.
Before deleting them, always check the resultset with a SELECT * FROM CTE first
In your case i checked the resultset of the above query, which is:
id lastprice updatetime UpdateMillisec RowNum
211709 51370 09:30:00.0000000 500 1
211709 51380 09:30:01.0000000 0 1
211709 51370 09:30:01.0000000 500 2
211709 51370 09:30:02.0000000 0 1
211709 51370 09:30:02.0000000 500 2
211709 51370 09:30:03.0000000 0 1
211709 51370 09:30:04.0000000 0 1
211709 51370 09:30:04.0000000 500 2
As we can see, exactly those records, which you want to delete, have RowNum = 2. So finally we can change the SELECT * to a DELETE and execute the query again.
top()to limit the delete operation to a limited number of records each time you apply thedeleteclause. A proper primary key would help a lot.