December 18, 2013 at 1:17 pm
I need to count all the records that meet the conditions in the case statement.
Following is the select after the CTE1 to display the count if each of the condition is met.
Can anyone tell me whats wrong with it??
Select
Count(Case when CTE1.Status = 'No' Then 1 Else Null
when CTE1.Agent_Status]= 'Aged' Then 1 Else Null
when CTE1.Platform] = Production Then 1 Else Null
When CTE1.Critical = 'Yes' Then 1 Else Null
End as CriticalData)
From CTE1
December 18, 2013 at 1:25 pm
sharonsql2013 (12/18/2013)
I need to count all the records that meet the conditions in the case statement.Following is the select after the CTE1 to display the count if each of the condition is met.
Can anyone tell me whats wrong with it??
Select
Count(Case when CTE1.Status = 'No' Then 1 Else Null
when CTE1.Agent_Status]= 'Aged' Then 1 Else Null
when CTE1.Platform] = Production Then 1 Else Null
When CTE1.Critical = 'Yes' Then 1 Else Null
End as CriticalData)
From CTE1
Will also get rid of any "Null ignored by aggregate" warnings but since I do not have your data, recognize that I've not tested it.
Select
SUM(Case when CTE1.Status = 'No' Then 1
when CTE1.Agent_Status]= 'Aged' Then 1
when CTE1.Platform] = Production Then 1
When CTE1.Critical = 'Yes' Then 1
ELSE 0
End as CriticalData)
From CTE1
--Jeff Moden
Change is inevitable... Change for the better is not.
December 18, 2013 at 1:34 pm
I get this error : 'SUM' is not a recognized built-in function name.
December 18, 2013 at 1:48 pm
sharonsql2013 (12/18/2013)
I get this error : 'SUM' is not a recognized built-in function name.
You'll have to show all the code you ran. And verify this is SQL Server.
SUM() is definitely a valid construct in SQL Server.
December 19, 2013 at 5:14 am
I think it's the column alias causing the error.
Try it like this:
Select
SUM(Case when CTE1.Status = 'No' Then 1
when CTE1.Agent_Status= 'Aged' Then 1
when CTE1.Platform = Production Then 1
When CTE1.Critical = 'Yes' Then 1
ELSE 0
End)
From CTE1
December 20, 2013 at 1:12 pm
CK2 (12/19/2013)
I think it's the column alias causing the error.Try it like this:
Select
SUM(Case when CTE1.Status = 'No' Then 1
when CTE1.Agent_Status= 'Aged' Then 1
when CTE1.Platform = Production Then 1
When CTE1.Critical = 'Yes' Then 1
ELSE 0
End)
From CTE1
^^^ That code will get you the count of rows where ANY of those conditions is true. To get the count of rows where ALL of those conditions are true, you need this:
SELECT SUM(CASE WHEN CTE1.Status = 'No' AND CTE1.Agent_Status= 'Aged' AND CTE1.Platform = 'Production' AND CTE1.Critical = 'Yes' THEN 1
ELSE 0
END)
FROM CTE1
Jason Wolfkill
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply