2017-06-02 41 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

相關問題