删除SQL Server中的重复数据

时间:2016-06-18 21:26:54

标签: sql-server

我通过此查询在SQL Server中发现了大量重复数据

SELECT
    [MærkeID], [Model], [Årgang], [Motor Type], [Krydsmålet],
    [Centerhul], [Bolter], [Dæk], [Fælge], [ET], [vendor], COUNT(*)
FROM
    Data.Hjuldata   
GROUP BY
    [MærkeID], [Model], [Årgang], [Motor Type], [Krydsmålet],
    [Centerhul], [Bolter], [Dæk], [Fælge], [ET], [vendor]
HAVING 
    COUNT(*) > 1

enter image description here

有没有办法删除它们所以只有1条记录而不是2条记录?

1 个答案:

答案 0 :(得分:2)

WITH X AS (
SELECT
     *
     ,ROW_NUMBER() OVER (PARTITION BY 
                           [MærkeID]
                          ,[Model]
                          ,[Årgang]
                          ,[Motor Type]
                          ,[Krydsmålet]
                          ,[Centerhul]
                          ,[Bolter]
                          ,[Dæk]
                          ,[Fælge]
                          ,[ET]
                          ,[vendor] ORDER BY (SELECT NULL)) RN
FROM Data.Hjuldata )
DELETE FROM X 
WHERE RN > 1