Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Percentages don't add *EXACTLY* to 100

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2004-02-12 : 11:21:36
I need a majic potion to make the integer values returned of percentages ALWAYS add to 100.


SELECT Answer ,
Total ,
'<img src="bar.gif" height="15px" width='+ CAST(CAST([Percent] AS INT) AS VARCHAR) + 'px"> ' as ' ',
CAST(CAST([Percent] AS INT) AS VARCHAR)+ '%'as ' '
FROM (
SELECT BodyText AS Answer,
ST.Choice,
IsNull(Total, 0) AS Total,
CASE WHEN Total Is NULL
THEN 0
ELSE Total * 100.0 / (SELECT COUNT(*) FROM dbo.SurveyResult WHERE Abbr=@Abbr AND CourseID = @CourseID)
END AS [Percent]
FROM dbo.SurveyText ST
LEFT OUTER JOIN (SELECT Abbr, Choice, Count(*) AS Total -- Count up the Survey Results for this question
FROM dbo.SurveyResult
WHERE Abbr = @Abbr
AND CourseID = @CourseID
GROUP BY Abbr, Choice
) SR
ON ST.Abbr = SR.Abbr AND ST.Choice = SR.Choice
WHERE Language = @Language
AND ST.Abbr = @Abbr
AND CourseID = @CourseID
AND ST.Choice > 0
) X
ORDER BY Choice



I've posted the whole taco here, along with the guacamole.

Problem is one of rounding. If every row is rounded down, then it'll probably add up to 99% if all the percentages are added.

If every row is rounded up, then it'll probably add up to 101%.

Any ideas?

Sam

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-02-12 : 11:55:45
Why don't you hard-code the total to be 100?....A small and simple sleight-of-hand...
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2004-02-12 : 12:17:03
No total is displayed.

This proc returns "User Survey Results". Sometimes when there's three rows returned, and the integers are simple values, an occasional user will point out that they add up to 99%, not 100%.

Here's a simple example: 3 answers, all different, 3 users only.

COUNT(*) IS 3

33%
33%
33%

I can't beat my way outta this paper bag. I'll tell the users to pound sand.

Sam
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-12 : 12:24:48
NOTE: The preceeding User Survey Results results are accurate to (+/-) 1.2378954152...%.
Thank you and good night
Go to Top of Page

theGrimm
Starting Member

4 Posts

Posted - 2004-02-13 : 07:22:55
Don't Round UP or DOWN, though, round OFF (33.3 -> 33 and 33.5 -> 34). It still won't always add up. Just explain nicely that the actual values DO add up to a hundred, but that you've rounded off 33.33333333333 to 33 to make it easier to read.

If they still have a problem with it, let them decide how they want it rounded off.

theGrimm
Go to Top of Page
   

- Advertisement -