给定一个类似于这个的表,称为 VehicleUser:

车辆用户 ID |车辆编号 |用户身份
1 | 1001 | 2
2 | 1001 | 2
3 | 1001 | 2
4 | 1001 | 3
5 | 1001 | 3
6 | 1001 | 3

如何编写可以删除重复项的查询?第 2 行和第 3 行与第 1 行相同,但 VehicleUserId 不同,第 5 行和第 6 行与第 4 行相同,但 VehicleUserId 不同。

最佳答案

;with cte as (
select row_number() over
    (partition by VehicleId, UserId order by VehicleUserId) as rn
from VehicleUser)
delete from cte
where rn > 1;

关于sql - 如何编写查询以删除表中的重复项?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4796023/

10-09 23:55