0

I have a database with all the cities in the world.

and if I run this query I can see there are doubles:

enter image description here

I already used this query to delete all duplicate rows:

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

I would now run it again and delete all columns where latitude and Longitude are the same and if two rows are the same like in the picture above I want to keep the one where population has some value and delete the one with population '0'

1
  • Add a where clause. Delete from cte where ... population < 1 and.... Commented Jun 3, 2017 at 5:04

1 Answer 1

2

Try this query --

;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
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it worked, I had edit and select just 'latitude,Longitude'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.