2017-06-02 88 views
0

我对300万行数据库有一个惊人的问题。SQL Server:如何从数据库中选择双城市

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

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

+0

你只是想用重复的条目?如果这是正确的,然后使用EXISTS基于长期和经济 – maSTAShuFu

+0

检查我的答案 –

+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 
总之
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 
0

集团的经度和纬度和城市,使用具有> 1,并将结果作为子查询从表中筛选正确的行。

Select * from world 
where city IN 
     (Select City From World Group by longitude, latitude, city       
     having count(*) > 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; 
+0

谢谢,但我想逐行列出,我会决定删除什么 –

+0

请你详细说明一下?你是否试过这段代码? – ViKiNG

+0

明白了。但我想你已经得到了你的答案,对吧?如果不是,则更改选择*选择w。*,然后休息一切正常。 – ViKiNG

相关问题