当Count为0时,SQL显示月份

时间:2015-06-01 13:24:50

标签: sql date count sql-server-2008-r2 datepart

我想知道是否有人可以帮助我......

我有以下SQL查询(已将其缩短为大型联合查询)

  SELECT [  Month   ],
sum(total) 
 from
(select datename(month,Resolved1Date) as '  Month   ',
COUNT(case when 
fileDescription not like 'test%' 
and Issue1Description ='Escalated' then 0 else 1 end)   as 'total'   
FROM         complaint_1 WITH (nolock) INNER JOIN
             Case WITH (nolock) ON Case.ref = complaint_1.ref 
WHERE     
Resolved1Date >=DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
Resolved1Date  <= dateadd(mm,datediff(mm,0,getdate()),0)
group by datename(month,Resolved1Date), datepart(month, Resolved1Date)
)x                     
group by  [  Month   ]
order by  [  Month   ] desc

查询计算所有解决日期在当前年份的第1天和当前月份之间的情况。我的问题是如果一个月没有结果它排除了月份,我希望我的结果返回类似: -

jan   5
feb   10
march 7
apr   0
may   2

任何人都能引导我朝着正确的方向前进吗?

2 个答案:

答案 0 :(得分:2)

创建一组Months作为from子句中的第一个表,并将您的查询加入到此子句中。然后你会得到每个月的结果。 我在财务报告方面遇到类似的问题,我需要所有月份和财政年度的结果。 我使用了DATENAME函数来确保查询的结果一致。 如果您想要按月订单(1月 - 2月 - 3月)的数据,您可能不希望按月订购,因为这将按字母顺序排列,您需要包含排序字段。

SELECT M.[  Month  ] AS [  Month  ]
      ,SUM(ISNULL(x.total,0)) AS [Total] -- x.total will be null for months with no transactions.
  FROM -- Set of Months (need one record for each month)
     (SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
                          ,(DATENAME(month,'2015-02-01'),2)
                          ,(DATENAME(month,'2015-03-01'),3)
                          ,(DATENAME(month,'2015-04-01'),4)
                          ,(DATENAME(month,'2015-05-01'),5)
                          ,(DATENAME(month,'2015-06-01'),6)
                          ,(DATENAME(month,'2015-07-01'),7)
                          ,(DATENAME(month,'2015-08-01'),8)
                          ,(DATENAME(month,'2015-09-01'),9)
                          ,(DATENAME(month,'2015-10-01'),10)
                          ,(DATENAME(month,'2015-11-01'),11)
                          ,(DATENAME(month,'2015-12-01'),12)) AS Mnth("  Month  ",MnthSort)) AS M
LEFT OUTER JOIN  -- Your from clause goes here.
     (SELECT * 
        FROM (VALUES (DATENAME(month,'2015-01-01'),5)
                    ,(DATENAME(month,'2015-02-01'),4)
                    ,(DATENAME(month,'2015-02-01'),6)
                    ,(DATENAME(month,'2015-03-01'),7)
                    ,(DATENAME(month,'2015-04-01'),0)
                    ,(DATENAME(month,'2015-05-01'),1)
                    ,(DATENAME(month,'2015-05-01'),1)
              ) AS data("  Month  ","total")) x ON x.[  Month  ] = M.[  Month  ]
GROUP BY M.[  Month  ], M.MnthSort
ORDER BY M.MnthSort

我在SQL Server 2008上运行了这个 - R1

查询中from子句的第一部分以表格格式定义月份集合,每个月返回一行(运行此项以查看结果):

SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
                     ,(DATENAME(month,'2015-02-01'),2)
                     ,(DATENAME(month,'2015-03-01'),3)
                     ,(DATENAME(month,'2015-04-01'),4)
                     ,(DATENAME(month,'2015-05-01'),5)
                     ,(DATENAME(month,'2015-06-01'),6)
                     ,(DATENAME(month,'2015-07-01'),7)
                     ,(DATENAME(month,'2015-08-01'),8)
                     ,(DATENAME(month,'2015-09-01'),9)
                     ,(DATENAME(month,'2015-10-01'),10)
                     ,(DATENAME(month,'2015-11-01'),11)
                     ,(DATENAME(month,'2015-12-01'),12)) AS Mnth("  Month  ",MnthSort)

LEVER OUTER JOIN是将查询结果链接到每个月,因此每个月都会得到一个总数。使用外部联接是因为每个月没有总计。

使用上面的sql查询将是:

SELECT M.[  Month  ] AS [  Month  ]
      ,SUM(ISNULL(x.total,0)) AS [Total] -- x.total will be null for months with no transactions.
  FROM -- Set of Months (January - December), ensures one record for each month
     (SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
                          ,(DATENAME(month,'2015-02-01'),2)
                          ,(DATENAME(month,'2015-03-01'),3)
                          ,(DATENAME(month,'2015-04-01'),4)
                          ,(DATENAME(month,'2015-05-01'),5)
                          ,(DATENAME(month,'2015-06-01'),6)
                          ,(DATENAME(month,'2015-07-01'),7)
                          ,(DATENAME(month,'2015-08-01'),8)
                          ,(DATENAME(month,'2015-09-01'),9)
                          ,(DATENAME(month,'2015-10-01'),10)
                          ,(DATENAME(month,'2015-11-01'),11)
                          ,(DATENAME(month,'2015-12-01'),12)) AS Mnth("  Month  ",MnthSort)) AS M
LEFT OUTER JOIN  -- Your Query included from here...
    (SELECT datename(month,Resolved1Date) as '  Month   ',
            COUNT(CASE WHEN fileDescription NOT LIKE 'test%' 
                        AND Issue1Description ='Escalated' THEN 0 ELSE 1
                  END) as 'total'   
       FROM complaint_1 WITH (nolock)
            INNER JOIN Case WITH (nolock) ON Case.ref = complaint_1.ref 
      WHERE     
            Resolved1Date >=DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
            Resolved1Date  <= dateadd(mm,datediff(mm,0,getdate()),0)
      group by datename(month,Resolved1Date), datepart(month, Resolved1Date)
     ) x on x.[  Month  ] = M.[  Month  ]

GROUP BY M.[  Month  ], M.MnthSort
ORDER BY M.MnthSort

答案 1 :(得分:1)

您可以从中创建临时表和左连接。

这样的事情:

DECLARE @Helper TABLE 
(
  TheDate datetime
)

DECLARE @StartDate datetime
SELECT @StartDate = '01.01.2015'

WHILE @StartDate < DATEADD(day,7,GETDATE())
BEGIN
  INSERT INTO @Helper (Thedate) VALUES (@StartDate)

  SELECT @StartDate = DATEADD(MONTH, 1, @StartDate)

END

我希望它有所帮助。