Sum of Counts

  • I am trying to get a sum of all the counts if both the conditions are met in the case case statement.

    Here is the CTE.. I am expecting a single digit output. However , when I try to Sum the counts I get an error stating that its not allowed. Please advise

    With CTE1 As

    (SELECT [Device_Name]

    , Case when [Platform] Like 'Production%' Then 'Production'

    When [Platform] Like 'Development%' Then 'Dev-Non Production'

    When [Platform] Like 'Test' Then 'Non Production'

    End as [Platform_Display]

    ,[Group_Owner]

    , Case when [Exclusion_Status] IS Null Then 'No'

    When [Exclusion_Status]= 'No' Then 'No'

    When [Exclusion_Status]= 'Yes' Then 'Yes'

    End as [Exclusion_Status_Now]

    FROM ProductionData

    )

    Select Case when CTE1.[Exclusion_Status_now] = 'No'

    and CTE1.[Platform_Display] = 'Production'

    Then (COUNT(CTE1.[Exclusion_Status_now]) + COUNT([Platform_Display])

    Else Null

    End

    from CTE1 where CTE1.[Group_Owner] LIKE 'Prod'

    group by [Exclusion_Status_now],[Platform_Display]

  • No worried .. I figured it out:)

    Its

    Sum (case .... then 1 else 0)

  • sharonsql2013 (12/24/2013)


    No worried .. I figured it out:)

    Its

    Sum (case .... then 1 else 0)

    I think you meant:

    Sum (case .... then 1 else 0 end)

    If you're counting, it might be more intuitive this way:

    COUNT(case .... then 1 end)

    Merry Christmas!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply