Amour22015
asked on
SQL Server 2014 - Percentage
Hi Experts,
I have this bit of code:
But that is wrong:
Example:
If 92 students takes a quiz and all fail: 92 then the PercentCompliant would be 0
92/92 = 100%
It should be:
92/92 = 0%
please help and thanks
I have this bit of code:
Count(*) / Cast( SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) as float) *100 as PercentCompliant
But that is wrong:
Example:
If 92 students takes a quiz and all fail: 92 then the PercentCompliant would be 0
92/92 = 100%
It should be:
92/92 = 0%
please help and thanks
This should give you the result you need.
If all failed then this SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) should return 0.
Check these-
SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END)
SUM( Case when ActionableResult = 0 THEN 1 ELSE 0 END)
if you need only INT percentage
If all failed then this SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) should return 0.
Check these-
SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END)
SUM( Case when ActionableResult = 0 THEN 1 ELSE 0 END)
SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as PercentCompliant
if you need only INT percentage
SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) * 100 / COUNT(*) as PercentCompliant
If all failed then this SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) should return 0.
Check these-
Check these-
SUM( Case when ActionableResult = 1 THEN 0 ELSE 1 END)
SUM( Case when ActionableResult = 0 THEN 1 ELSE 0 END)
SUM( Case when ActionableResult = 1 THEN 0 ELSE 1 END) * 100.0 / COUNT(*) as PercentCompliant
ASKER
I tried all and so far still comes up with 100%
If 10 Students take a test and all fail then the class would get a 0% grade. Not 100% That is the issue...
If 10 Students take a test and all fail then the class would get a 0% grade. Not 100% That is the issue...
Does ActionableResult mean they passed, or they failed?
»bp
»bp
ASKER
Maybe I need a case when statement:
Looks like this would work?
Case When Count(*) = Cast( SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) as float) Then 0
Else Count(*) / Cast( SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) as float) *100 End as PerCompliant
Looks like this would work?
Perhaps you need:
»bp
(Count(*) / Cast( SUM( Case when ActionableResult = 1 THEN 0 ELSE 1 END) as float)) * 100 as PercentCompliant
»bp
ASKER
ActionableResult = 1 means they failed...
Okay, then try my last code.
»bp
»bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I ended up doing:
thanks
Cast( Count(*) - SUM( Case when ActionableResult = 1 THEN 1 ELSE 0 END) as float) /Count(*) as PercentCompliant
thanks
ASKER
thank you all for helping
This will get you the same result without an extra subtration arithmetic...
»bp
Cast((SUM(Case when ActionableResult = 1 THEN 0 ELSE 1 END) as float) / Count(*) as PercentCompliant
»bp
Open in new window
»bp