每个不同的总和的SQL服务器

时间:2015-07-21 12:25:15

标签: sql sql-server

我想得到一份清单,其中包含不同国家的名称,每个国家的员工人数明显不同。我无法在SQL服务器中为每个循环使用a。有没有办法实现我想要的?我可以使用country ='input country',但我不想为每个国家都这样做。

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;

4 个答案:

答案 0 :(得分: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 :(得分: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;

答案 2 :(得分: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

答案 3 :(得分: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