David Tunnell
David Tunnell

Reputation: 7542

SELECT SUMmed column during certain circumstances

As part of a complex query, I am trying sum the DifferentUsers field:

enter image description here

This is the actual table, the query I am using is:

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

The part of the query that I want to change is:

,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20)) AS DifferentUsers

As you can see, currently the query is performing as expected:

enter image description here

However, I only want the UserDifference to sum if Hours > 0.

I tried something like this:

 ,CAST(SUM(DTH.[UserDifference]) AS VARCHAR(20) WHERE DTH.Hours > 0) AS DifferentUsers

But the syntax is off and I am getting errors. How can I achieve the desired functionality?

Upvotes: 0

Views: 42

Answers (1)

Hart CO
Hart CO

Reputation: 34784

You want a conditional SUM() and can use a CASE statement:

,CAST(SUM(CASE WHEN DTH.Hours > 0 THEN DTH.[UserDifference] END) AS VARCHAR(20)) AS DifferentUsers

Note: Some prefer to set an ELSE, but the default is NULL when a value is not captured by any of the CASE criteria, and since aggregates exclude NULL values you don't need it, the above and below will return identical results:

,CAST(SUM(CASE WHEN DTH.Hours > 0 THEN DTH.[UserDifference] ELSE 0 END) AS VARCHAR(20)) AS DifferentUsers

Upvotes: 1

Related Questions