我从这个链接有这个代码:
How can I remove duplicate rows?
;WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id,date_work,hours ORDER BY ( SELECT 0)) RN FROM work_hours) DELETE FROM cte WHERE RN > 1
是否可以删除第一个输入的重复行或者我应该有一个额外的列date_of_entry?我想这样做,如果我输入相同的date_work和不同的小时PARTITION BY person_id,date_work它会随机删除重复.
如果不可能,我如何删除更高时间的重复项?
解决方法
按小时desc添加订单
;WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id,date_work ORDER BY hours DESC) RN FROM work_hours) DELETE FROM cte WHERE RN > 1