Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sum of Counts


Sum of Counts

Author
Message
sharonsql2013
sharonsql2013
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 1220
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]
sharonsql2013
sharonsql2013
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 1220
No worried .. I figured it outSmile
Its
Sum (case .... then 1 else 0)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4237 Visits: 6431
sharonsql2013 (12/24/2013)
No worried .. I figured it outSmile
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search