1

并将它们显示为每个月的总数。我使用以下脚本

SELECT datepart(yy,j.CompletedAt) as 'YY',  
       str(datepart(MM, j.CompletedAt),2) as 'MM', 
       Datename(MM,j.CompletedAt) as 'Month', 
       count('MM') as 'PMInMonth'
FROM facts_reactive.dbo.jobs j 
WHERE (
       (J.Reactive = -1)     
         AND datepart(yy,j.CompletedAt) = ' 2013'
         AND j.jobstatus = 6
       )
GROUP BY datepart(yy,j.CompletedAt),  
         str(datepart(MM, j.CompletedAt),2), 
         Datename(MM,j.CompletedAt)
ORDER BY datepart(yy,j.CompletedAt) ASC,  
         str(datepart(MM, j.CompletedAt),2) ASC;

但是,如果该月没有取消任何工作,则不会返回任何内容;而我希望月份和 0 作为取消工作的数量返回。

这可能吗?

提前谢谢了。

4

1 回答 1

1
;WITH cMONTHS AS
(
    SELECT
        CAST('20130101' AS date) AS TheMonthStart,
        CAST('20130201' AS date) AS TheMonthEnd
    UNION ALL
    SELECT
        DATEADD(mm, 1, TheMonthStart),
        DATEADD(mm, 1, TheMonthEnd)
    FROM cMONTHS
    WHERE TheMonthStart < '20140101'
)
SELECT
   datepart(yy,c.TheMonthStart) as 'YY',  
   str(datepart(MM, c.TheMonthStart),2) as 'MM', 
   Datename(MM,c.TheMonthStart) as 'Month', 
   count(j.CompletedAt) as 'PMInMonth'
FROM
     cMONTHS c
     LEFT JOIN
     facts_reactive.dbo.jobs j ON j.CompletedAt >= c.TheMonthStart AND j.CompletedAt < c.TheMonthEnd
                   AND
                   J.Reactive = -1   
                   AND
                   j.jobstatus = 6
GROUP BY
   datepart(yy,c.TheMonthStart),  
   str(datepart(MM, c.TheMonthStart),2), 
   Datename(MM,c.TheMonthStart)
ORDER BY
   datepart(yy,c.TheMonthStart),  
   str(datepart(MM, c.TheMonthStart),2);
于 2013-06-17T15:38:46.607 回答