我需要按部门分组按月计算活动数量

时间:2017-04-27 13:07:50

标签: sql sql-server-express

我试图按月计算一系列活动,并按部门名称对它们进行分组。该报告基于我输入查询" @ rptDate"的日期参数。我下面的内容有效,但我猜测有更好的方法可以做同样的事情,并希望有人可以解决问题。

Select Count(*) As Month1Count, 0 As Month2Count, 0 As Month3Count,
0 As Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(@rptDate) and Year(BeginDate) = Year(@rptDate)
Group By DepartmentName, ActivityDescription

UNION

Select 0 As Month1Count, COUNT(*) As Month2Count, 0 as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(@rptDate) + 1  and Year(BeginDate) = Year(DateAdd(month,1,@rptDate))
Group By DepartmentName, ActivityDescription

UNION

Select 0 As Month1Count, 0 As Month2Count, Count(*) as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName, ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(@rptDate) + 2  and Year(BeginDate) = Year(DateAdd(month,2,@rptDate))
Group By DepartmentName, ActivityDescription

1 个答案:

答案 0 :(得分:0)

我没有对以下内容进行测试,因此可能存在一些拼写错误或轻微错误,但我认为以下是您想要的:

SELECT
    sum(case when Month(BeginDate) = Month(dateadd(month,  0, @rptDate)) then 1 else 0 end) as Month1Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  1, @rptDate)) then 1 else 0 end) as Month2Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  2, @rptDate)) then 1 else 0 end) as Month3Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  3, @rptDate)) then 1 else 0 end) as Month4Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  4, @rptDate)) then 1 else 0 end) as Month5Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  5, @rptDate)) then 1 else 0 end) as Month6Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  6, @rptDate)) then 1 else 0 end) as Month7Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  7, @rptDate)) then 1 else 0 end) as Month8Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  8, @rptDate)) then 1 else 0 end) as Month9Count,
    sum(case when Month(BeginDate) = Month(dateadd(month,  9, @rptDate)) then 1 else 0 end) as Month10Count,
    sum(case when Month(BeginDate) = Month(dateadd(month, 10, @rptDate)) then 1 else 0 end) as Month11Count,
    sum(case when Month(BeginDate) = Month(dateadd(month, 11, @rptDate)) then 1 else 0 end) as Month12Count
    DepartmentName,
    ActivityDescription
FROM
    reports.WorkOrders
WHERE
    Year(BeginDate) + right('0'+Month(BeginDate),2)
         between Year(@rptDate) + right('0'+Month(@rptDate),2)
             and Year(dateadd(month,11,@rptDate)) + right('0'+Month(dateadd(month,11,@rptDate)),2)
GROUP BY
    DepartmentName,
    ActivityDescription

请告诉我你是如何继续这样做的。如果我犯了拼写错误并且您无法纠正错误,请报告错误消息和/或结果的任何问题。