I’ve been tasked with an interesting problem. Basically, I have to compute 3 different percentages:
SELECT @LowPercent = CAST(ROUND((@LowCount / @ValidCount) * 100.0, 0) AS INT)
SELECT @MidPercent = CAST(ROUND((@MidCount / @ValidCount) * 100.0, 0) AS INT)
SELECT @HighPercent = CAST(ROUND((@HighCount / @ValidCount) * 100.0, 0) AS INT)
However, the sum of the percentages can’t exceed 100.
Consider the following scenario: Suppose the intermediate calculations (e.g. @XXXCount/@ValidCount * 100.0) are computed as 1.8, 91.5, and 6.6. Rounding to 0 decimal places yields 2, 92, and 7 which sum to 101. Other than a simple “rule of thumb” such as “offset the highest number,” is there a straight-forward way to address this requirement?
Thanks.
First of all, why
int?If you really want to use int maybe the better option would to decrease the highest first.
Or
You could decrease the nearest to .5
If you need 0 places, the problem may occur if you do a round 🙂 So you’ll have to choose where to drop the unit.