PHP/MYSQL Percentage of total with rollup

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP/MYSQL Percentage of total with rollup

Post by cjkeane »

Hi everyone,

I'm trying to get the percentage of each person who performed work, but the total percentage is never 100%. It shows up as 99%. I've been struggling with this for quite some time. Does anyone have any helpful hints which may help resolve my issue?

Code: Select all

<?PHP
$string = "SELECT COUNT(Therapist) AS Count, Round(100*COUNT(Therapist)/Count2,0) + '%' AS Percentage, Date_Of_Entry, Therapist, SUM(BILL_TIME) AS BILL_TOTAL
FROM tblprivatepracticedetails, 
     (SELECT COUNT( Therapist ) AS Count2
     FROM tblprivatepracticedetails
     ) AS c
WHERE Date_Of_Entry Between '$FromDate' And '$ToDate' GROUP BY Therapist WITH ROLLUP";
$query = mysql_query($string) or die (mysql_error());
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP/MYSQL Percentage of total with rollup

Post by califdon »

I would guess that it's just a rounding error, which is to be expected when you round to 0 digits, as you are doing. The actual result of dividing one count of therapists by the count of all the therapists might be numbers like 33.333333333 or 16.666666667 but when you round them and add them up, they are unlikely to ever total precisely 100.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL Percentage of total with rollup

Post by cjkeane »

thanks. i figured out my error. the issue is resolved.
Post Reply