SQL Server:如何从数据库中选择双城市

时间:2017-06-02 03:38:50

标签: sql-server

我在300万行数据库上遇到了一个惊人的问题。

数据库存储了世界上所有城市,但有双打:

enter image description here

我想SELECT所有具有相同纬度和经度的行。 如何选择整行,而不仅仅是纬度和经度?

我想列出这样的结果:

enter image description here

不想列出结果如下:

SELECT *,ROW_NUMBER() 
OVER (PARTITION BY latitude, Longitude 
ORDER BY latitude, Longitude) AS RN
FROM World

enter image description here

4 个答案:

答案 0 :(得分:3)

select country, city, accentcity, region, population, region, population, latitude, longitude
from (
      select country
            ,city
            ,accentcity
            ,region
            ,population
            ,latitude
            ,longitude
            ,count(*) over(partition by latitude, longitude) as dup
      from world
     ) as Temp
where dup > 1

答案 1 :(得分:1)

试试这个

;WITH CTE AS(
SELECT Latitude,Longitude,COUNT(Latitude) As NumOfRec
FROM World
GROUP BY Latitude,Longitude
)

SELECT w.* FROM WORLD w 
JOIN CTE c ON w.Latitude=c.Latitude AND w.Longitude=c.Longitude
WHERE c.NumOfRec>1;

答案 2 :(得分:0)

嗨,这会对你有帮助,

 select * from World where exists( 
        select * from
            (
            select WorldLatCount = Count(*) 
            from World as WorldLat where World.Longitutde = WorldLat.Longitutde and WorldLat.Latitude = World.Latitude
        ) 
     as WorldCounts where WorldCounts.WorldLatCount> 1

   )

你会得到这样的结果

Country Longitutde  Latitude
ad  42.30   45.50
ad  42.30   45.50
ef  42.78   45.59
ef  42.78   45.59

答案 3 :(得分:0)

按经度和纬度和城市分组,使用> 1,并使用结果作为子查询从表中过滤正确的行。

Select * from world 
 where city IN 
      (Select City From World Group by longitude, latitude, city                        
      having count(* ) > 1 )