SQL:删除带有子句的重复行

时间:2017-06-03 05:01:48

标签: sql sql-server

我有一个包含世界上所有城市的数据库。

如果我运行this查询,我可以看到有双打:

enter image description here

我已经使用此查询删除所有重复的行:

WITH CTE AS
(
SELECT *,ROW_NUMBER() 
OVER (PARTITION BY Country, City, AccentCity, Region, Population, latitude, Longitude 
ORDER BY Country, City, AccentCity, Region, Population, latitude, Longitude) AS RN
FROM ExperimentWorld
)

DELETE FROM CTE WHERE RN<>1

我现在再次运行它并删除latitudeLongitude相同的所有列,如果两行相同,如上图所示,我想保留{{1}有一些价值并删除population&#39; 0&#39;

的值

1 个答案:

答案 0 :(得分:2)

尝试此查询 -

;WITH CTE
AS (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY Country
            ,City
            ,AccentCity
            ,Region
            ,Population
            ,latitude
            ,Longitude ORDER BY Population DESC
            ) AS RowNum
    FROM ExperimentWorld
    )
DELETE
FROM CTE
WHERE RowNum > 1