简体   繁体   English

对SQL Server中列的分组计数求和

[英]Sum on a grouped count of a column in SQL Server

I have details of the count of users and the month in the 2nd column and the teams name in the 3rd columns as below. 我在第二列中有用户数量和月份的详细信息,在第三列中有团队名称,如下所示。 I want the o/p to give the sum of the users column for 7th, 8th and 9th month grouped wrt the month. 我希望o / p提供第7、8和9个月的用户列总和,并将该月份分组。

 Count_of_Users Month   Team
 ----------------------------
       1          7       a
       1          8       a
       1          9       a
       1          7       a
       1          8       a
       1          9       a
       1          7       a
       1          8       a
       1          9       a

Query: 查询:

SELECT  
    (COUNT(DISTINCT([username]))) AS A,
    MONTH([Date]) AS B,
    Team
FROM
    myTable
GROUP BY
    [UserName], MONTH([Date]), Team
ORDER BY 
    [UserName], MONTH([Date]), Team

This is the query i had for the above o/p. 这是我对以上o / p的查询。 I am not sure if i should be using UNION ALL to proceed. 我不确定是否应该使用UNION ALL继续进行。 Any inputs are appreciated. 任何输入表示赞赏。

I'm not sure what you want, but your current version doesn't seem very useful. 我不确定您想要什么,但是您当前的版本似乎不太有用。 Is this what you intend? 这是你打算的吗?

SELECT COUNT(DISTINCT [username]) AS A,
       MONTH([Date]) AS B,
       Team
FROM myTable
GROUP BY MONTH([Date]), Team
ORDER BY MONTH([Date]), Team

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM