我想選擇只有最大地圖名稱按國家。當我提到「最大地圖名稱」時,我指的是該地圖出現次數最多的「地圖名稱」的值。我正在使用Ms Sql Server。只選擇最大地圖名稱按國家 - sql server
這是tblsurvey表結構:
[country] [mapname]
data:
BR MapQuest
CA OpenStreetMap
CN ArcGIS
DE Google Maps
GR Bing Maps
IT Google Maps
US Google Maps
US Bing Maps
US SAS.Planet
US Google Maps
查詢:
select country, mapname as mapnamemax
from tblsurvey s1
group by country, mapname
having count(mapname) = (select max(cnt)
from
(
select count(mapname) as cnt
from tblsurvey s2
where s1.country = s2.country and
s1.mapname = s2.mapname
group by mapname
)rc
)
這是我目前得到(這是當前和不正確的查詢輸出)
current output:
CN ArcGIS
GR Bing Maps
US Bing Maps
DE Google Maps
IT Google Maps
US Google Maps
BR MapQuest
CA OpenStreetMap
US SAS.Planet
這就是我想要得到的。 (這是所需的查詢輸出)
desired output:
BR MapQuest
CA OpenStreetMap
CN ArcGIS
DE Google Maps
GR Bing Maps
IT Google Maps
US Google Maps
注意:如果出現平局,那麼就返回一行那個國家。第一個發現
任何幫助,將不勝感激。在此之前謝謝。
你想告訴我們關於'tblsurvey'的結構和內容是什麼?你的問題是相當不完整的,因爲我們不知道數據是什麼。 – LittleBobbyTables
'我試圖只選擇最大國家......最大是什麼? – Lamak
請參閱上面編輯的問題,我回答了Lamak和LittleBobbyTables要求的兩個問題。 – user3657279