複雑なクエリの一部として、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
ご覧のとおり、現在、クエリは期待どおりに実行されています。
ただし、時間 > 0 の場合にのみ UserDifference を合計する必要があります。
私はこのようなことを試しました:
,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20) WHERE DTH.Hours > 0) AS DifferentUsers
しかし、構文がオフで、エラーが発生しています。目的の機能を実現するにはどうすればよいですか?