2015-07-21 45 views
-3

我想獲得一個列表,其中包含不同國家的名稱,明確不同國家的僱員人數。我無法在SQL服務器中使用每個循環。有什麼方法可以實現我想要的嗎?我可以使用哪個國家='輸入國家',但我不想爲每個國家都這樣做。SQL服務器爲每個不同的金額

Select distinct Country, 
    SUM(Test.[Number of Manager]) over() AS 'Number of Parts Staff/Manager', 
    Count(Test.[Name]) over() AS 'Number of Test' 

From Test; 
+2

你需要'GROUP BY Country' – DavidG

+0

你爲什麼要投下來的問題很多簡單的方法?這背後的邏輯是什麼? – el94

+0

我沒有downvote它,但我看到很多'rtfm'問題得到downvoted,也許這是原因... –

回答

2

不要使用DISTINCT,相反,你應該GROUP BY

Select Country, 
     SUM([Number of Manager]) AS 'Number of Parts Staff/Manager', 
     Count([Name]) AS 'Number of Test' 
From Test 
GROUP BY Country 
+1

錯誤消息可能來自上面顯示的SQL。請確保你改變了你的'SUM'和'COUNT'行來匹配。所以刪除'OVER()'部分。 – DavidG

0

我認爲你正在尋找group by

SELECT Country, 
     SUM([Number of Manager]) AS 'Number of Parts Staff/Manager', 
     Count([Name]) AS 'Number of Test' 
FROM Test 
GROUP BY Country; 
0
SELECT Country, 
    SUM(Test.[Number of Manager]) OVER() AS 'Number of Parts Staff/Manager', 
    COUNT(Test.[Name]) OVER() AS 'Number of Test' 
FROM Test 
GROUP BY Country 
0

您可以通過使用做到這一點PATITION BY

Select Country, 
    SUM(Test.[Number of Manager]) over (partition by Country order by Country) AS 'Number of Parts Staff/Manager', 
    Count(distinct Test.[Name]) over (partition by Country Country) AS 'Number of Test' 

From Test; 

或使用Group By COUNTRY

select country,Count(distinct Test.[Name]) AS 'Number of Test', SUM(Test.[Number of Manager]) AS 'Number of Parts Staff/Manager' from test 
group by country