This statement below gets me some data I need.
select TankName, COUNT(*) as Tanks, sum(Battles) as Battles,
SUM(victories) as Victories
from MemberTanks
where Tier = 9 and Class = 'Medium'
group by TankName
order by Tanks desc
I would like to show the percentage of the Victories in the resultset but I’m not sure how to do it. I tried…
select TankName, COUNT(*) as Tanks, sum(Battles) as Battles,
SUM(victories) as Victories, SUM(Victories)/SUM(Battles) as Percentage
from MemberTanks
where Tier = 9 and Class = 'Medium'
group by TankName
order by Tanks desc
… but percentage came back as “0” for every row.
How can I best get this value?
When you are calculating the percentage column since SUM(X) is returning a INT value, the result is getting floored and hence returning 0 as floored result
Multiply the SUM(victories) with a decimal value like 1.0 (so as to convert one operand to decimal/numeric) and then you would get correct result.
Try this:
Also, if you are using SQL Server 2005 or above you can reduce the calculations.computations using a CTE like below: