2

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

我有以下 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

查询计算解决日期在当年第一天到小于当月的所有情况。我的问题是,如果一个月没有结果,则不包括该月,我希望我的结果返回如下内容:-

jan   5
feb   10
march 7
apr   0
may   2

谁能指导我正确的方向?

4

2 回答 2

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)

之后的 LEFT 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
于 2015-06-02T08:19:08.530 回答
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

我希望它有所帮助。

于 2015-06-01T14:00:54.413 回答