表格中每个组的每月计数

时间:2017-12-29 11:24:15

标签: sql sql-server

表“RAW_DATA”的结构如下所示给定列并给出一组样本数据

        ID  CompletedDate   TypeID  AgentID
        1   2015-09-22      51      301
        2   2015-08-6       50      301
        3   2015-07-12      50      305
        4   2017-04-21      51      305
        5   2015-05-5       51      305
        6   2016-09-8       50      301
        7   2015-02-17      50      301
        8   2015-01-12      51      311
        9   2015-10-4       50      311
        10  2015-12-2       51      311

使用以下查询,我可以从此表中获取一个月的总计数

     SELECT FORMAT(CompletedDate,'MMMM yyyy') as Date,datepart(year,CompletedDate) YearNr,datepart(month,CompletedDate) MonthNr,
         COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
         COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2"
         FROM RAW_DATA
         group by  datepart(year,CompletedDate),datepart(month,CompletedDate),  FORMAT(CompletedDate,'MMMM yyyy')

我得到的样本结果看起来像是

        Date            YearNr  MonthNr Type1   Type2
        September 2016  2016    9       12282   224
        October 2015    2015    10      54      0
        November 2017   2017    11      5535    65
        March 2017      2017    3       1669    5641
        December 2016   2016    12      772     39      

现在我必须每月对“AgentID”列进行额外的分组。因此结果集应该类似于每个代理,基于上述条件的每个月的计数

我尝试在上面的查询中按选择和分组添加列“AgentID”,如下所示

        SELECT AgentID,FORMAT(CompletedDate,'MMMM yyyy') as Date,datepart(year,CompletedDate) YearNr,datepart(month,CompletedDate) MonthNr,
         COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
         COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2"
         FROM RAW_DATA
         group by  AgentID, datepart(year,CompletedDate), datepart(month,CompletedDate), FORMAT(CompletedDate,'MMMM yyyy')

但生成的输出就像

        Date        AgentID YearNr  MonthNr Type1   Type2
        April 2016      3642    2016    4   21  39
        November 2016   2887    2016    11  1   0
        March 2017      2309    2017    3   0   24
        May 2017        2784    2017    5   1   24
        December 2017   3355    2017    12  2   0

这不是预期的结果。例如,对于每个代理,结果集中的所有月份都必须有一个条目

Agent1  April2016 
Agent1 Nov 2017
Agent1 Dec 2016 

现在为Agent2

Agent2  April2016 
Agent2 Nov 2017
Agent2 Dec 2016 

那么我怎样才能得到每个特工的月数?

2 个答案:

答案 0 :(得分:2)

您只需要按agentId和month_year分组:

SELECT 
  AgentID,
  FORMAT(CompletedDate,'MMMM yyyy') as MonthYear,
  COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
  COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2",
  COUNT(*) AS Total
FROM RAW_DATA
group by  AgentID, FORMAT(CompletedDate,'MMMM yyyy');

SQL Fiddle

答案 1 :(得分:0)

只需将Count(*)添加为" monthelyAgentcount"在您的查询即

 SELECT AgentID,FORMAT(CompletedDate,'MMMM yyyy') as Date,datepart(year,CompletedDate) YearNr,datepart(month,CompletedDate) MonthNr,
     COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
     COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2" ,
     Count(*) as "monthelyAgentcount"
     FROM RAW_DATA
     group by  AgentID,datepart(year,CompletedDate),datepart(month,CompletedDate),  FORMAT(CompletedDate,'MMMM yyyy')