1

我有一个查询:

   SELECT DTH.PointPerson AS Person
          ,PDT.[Name] AS Project
          ,(CASE WHEN TSK.Name IS NULL THEN NULL
            WHEN SPT.[Name]+' - '+SPT.[Description] IS NULL THEN 'KanBan'
            ELSE SPT.[Name]+' - '+SPT.[Description]
            END) AS Sprint
          ,COALESCE(STY.[Number], NSS.IncidentNumber) AS Story
          ,TSK.[Name] AS Task
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 1 THEN DTH.[Hours] ELSE 0 END) AS Monday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 2 THEN DTH.[Hours] ELSE 0 END) AS Tuesday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 3 THEN DTH.[Hours] ELSE 0 END) AS Wednesday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 4 THEN DTH.[Hours] ELSE 0 END) AS Thursday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 5 THEN DTH.[Hours] ELSE 0 END) AS Friday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 6 THEN DTH.[Hours] ELSE 0 END) AS Saturday
          ,SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 7 THEN DTH.[Hours] ELSE 0 END) AS Sunday
          ,SUM(DTH.[Hours]) AS Total
          FROM DailyTaskHours DTH
     LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task 
     LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story 
     LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory 
     LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint 
     LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
     GROUP BY  DTH.PointPerson, PDT.[Name], SPT.[Name]
               ,SPT.[Description]
               ,STY.[Number]
               ,NSS.IncidentNumber
               ,TSK.[Name]

如果: SUM(DTH.[Hours]) AS Total = 0 我希望不返回该行

我相信这种方法是使用 CASE WHEN 但我很难弄清楚它到底在哪里。

4

2 回答 2

15

在 之后添加一个having子句group by

HAVING SUM(DTH.[Hours]) <> 0 
于 2013-09-18T16:13:34.417 回答
7

只需添加:

HAVING SUM(DTH.[Hours]) > 0

在您的查询结束时。

于 2013-09-18T16:13:25.137 回答