作为复杂查询的一部分,我正在尝试将DifferentUsers字段求和:
这是实际的表,我使用的查询是:
SELECT '3' AS RowType
,DTH.PointPerson AS Person
,COALESCE(PDT.[Name], APP.AppName) AS Project
,(
CASE WHEN (
STY.KanBanProductId IS NOT NULL
AND STY.SprintId IS NULL
) THEN 'KanBan' WHEN (
STY.KanBanProductId IS NULL
AND STY.SprintId IS NOT NULL
) THEN 'Sprint' ELSE SCY.Catagory END
) AS ProjectType
,COALESCE(STY.[Number], NSS.IncidentNumber) AS StoryNumber
,COALESCE(STY.Title, NSS.[Description]) AS StoryTitle
,CONVERT(VARCHAR(20), STY.Effort) AS Effort
,COALESCE(TSK.[Name], '') AS Task
,CONVERT(VARCHAR(20), TSK.OriginalEstimateHours) AS OriginalEstimateHours
,SCY.Catagory AS Category
,NSS.IncidentNumber AS IncidentNumber
,APP.AppName AS ApplicationName
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 2 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS MondayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 3 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS TuesdayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 4 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS WednesdayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 5 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS ThursdayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 6 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS FridayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 7 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS SaturdayHours
,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 1 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS SundayHours
,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20)) AS DifferentUsers
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 SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
LEFT JOIN [Application] APP ON NSS.ApplicationId = APP.PK_Application
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
LEFT JOIN [User] USR ON DTH.PointPerson = USR.DisplayName
WHERE DTH.PointPerson LIKE @userParam
AND ActivityDate >= @startDateParam
AND ActivityDate <= @endDateParam
AND PDT.PK_Product LIKE @productId
AND (
(
@orgTeamPK = '%'
AND (
USR.[OrganizationalTeamId] LIKE @orgTeamPK
OR USR.[OrganizationalTeamId] IS NULL
)
)
OR (
@orgTeamPK <> '%'
AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
)
AND (
(
STY.Number LIKE @search
OR STY.Number IS NULL
)
OR (
STY.Title LIKE @search
OR STY.Number IS NULL
)
OR (
TSK.NAME LIKE @search
OR STY.Number IS NULL
)
)
)
GROUP BY DTH.PointPerson
,PDT.[Name]
,SPT.[Name]
,SPT.[Description]
,STY.[Number]
,STY.Title
,TSK.[Name]
,SCY.Catagory
,NSS.IncidentNumber
,APP.AppName
,STY.KanBanProductId
,STY.SprintId
,NSS.[Description]
,TSK.OriginalEstimateHours
,STY.Effort
HAVING SUM(DTH.[Hours]) > 0
我想要更改的查询部分是:
,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20)) AS DifferentUsers
如您所见,目前查询正在按预期执行:
但是,我只希望UserDifference总和如果小时&gt; 0
我试过这样的事情:
,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20) WHERE DTH.Hours > 0) AS DifferentUsers
但语法已关闭,我收到错误。如何实现所需的功能?
答案 0 :(得分:1)
您想要一个条件SUM()
并且可以使用CASE
语句:
,CAST(SUM(CASE WHEN DTH.Hours > 0 THEN DTH.[UserDifference] END) AS VARCHAR(20)) AS DifferentUsers
注意:有些人更喜欢设置ELSE
,但是当NULL
个条件未捕获任何值时,默认值为CASE
,因为汇总排除了NULL
您不需要它的值,上面和下面将返回相同的结果:
,CAST(SUM(CASE WHEN DTH.Hours > 0 THEN DTH.[UserDifference] ELSE 0 END) AS VARCHAR(20)) AS DifferentUsers