【问题标题】:SQL display months when Count is 0 [duplicate]Count 为 0 时 SQL 显示月份 [重复]
【发布时间】:2015-08-14 23:40:38
【问题描述】:

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

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

谁能指引我正确的方向?

【问题讨论】:

  • 你能不能只为 Resolved1Date = 0 添加另一个 WHERE 子句?
  • 您应该格式化您的查询。您还应该修复逻辑。例如,count(case) 中的 case 语句没有做任何事情。您还应该使用您正在使用的数据库标记问题。
  • 删除 case 语句对我所追求的没有帮助,我认为问题在于计数,因为我正在计算日期字段,然后说明状态字段在两个值之间的位置,我如果没有匹配项,则需要将月份显示为 0。

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


【解决方案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

希望对你有帮助。

【讨论】:

    【解决方案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
    

    【讨论】:

    • 感谢您的帖子@nxb,您能否解释一下您所说的“创建一组月份作为 from 子句中的第一个表”我对 SQL 相当陌生,这开始让我发疯!
    • 编辑帖子回答问题,希望更清楚。
    • 啊,我已经注释掉了“左外连接”并将我的查询放入,并且收到了不正确的语法消息。我不明白它是用来将我的查询结果链接到几个月,现在有道理了。谢谢你的帮助@nxb :-) 真的很感激
    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多