Jump to content

[SOLVED] Calculate Percent from Query


Zergman

Recommended Posts

So I have a bunch of queries on a page and what I want to do is calculate the percent of each one from the total.

 

Say each record has a column with either Value1 or Value2.  All of them have either one.

 

I just want to output the percent of each.

Value1 = 68%

Value2 = 32%

 

Is this done easier via php or mysql?

Link to comment
Share on other sites

Thanks for the quick reply!

 

I sorta see what your doing, can't picture how to use it in my head.

 

Here are 2 queries that I want to calculate the percent.

SELECT * FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Resolved'

 

SELECT * FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Routed'

 

Any examples would be greatly appreciated  :)

Link to comment
Share on other sites

The amount of each varies a lot from month to month, but neither is always the lowest/highest.

 

Im just grabbing the total amount of records from each of the queries above.

 

$totalRows_rsdatadisplayresolved = mysql_num_rows($rsdatadisplayresolved);

and

$totalRows_rsdatadisplayrouted = mysql_num_rows($rsdatadisplayrouted);

Link to comment
Share on other sites

So if I get the total amount of records from each query and set them as a variable, I can do something like this?

 

 

$resolved = $_GET['$totalRows_rsdatadisplayrouted'];
$routed = $_GET['$totalRows_rsdatadisplayresolved'];

 

then

 

$percent = round ( ( ($resolved/$routed) *100) , 2);

Link to comment
Share on other sites

So if I get the total amount of records from each query and set them as a variable, I can do something like this?

 

 

$resolved = $_GET['$totalRows_rsdatadisplayrouted'];
$routed = $_GET['$totalRows_rsdatadisplayresolved'];

 

then

 

$percent = round ( ( ($resolved/$routed) *100) , 2);

 

That's if you want to know how many were resolved out of how many were routed.

 

if you want a total,

$total = $resolved+$routed;
$res_percent = round ( ( ($resolved/$total) *100) , 2);
$rout_percent = round ( ( ($routed/$total) *100) , 2);

Link to comment
Share on other sites

Got ya :)

 

 


$res = "SELECT COUNT ( * ) AS resnum  FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Resolved'";
$query = mysql_query($res);
$totRes = mysql_fetch_object($query);

$rou = "SELECT COUNT ( * ) AS rounum FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Routed'";
$query2 = mysql_query($rou);
$totRou = mysql_fetch_object($query2);

$total = ($totRes->resnum + $totRou->rounum);

echo 'Resolved: ' . round ( ( ($totRes->resnum/$total) *100), 2) . '%<br>';

echo 'Routed: ' . round ( ( ($totRou->rounum/$total) *100), 2) . '%<br>';

 

Like that?

Link to comment
Share on other sites

Awesome stuff, works like a charm.  Thanks guys for the help, both suggestions work fantastic!

 

Just curious now more than anything.

 

Can either of these examples be adapted to more than 2 queries?  Say I have a group of 8 queries that I want to calculate the percent.  Can I just expand on these or are these meant for only 2?

Link to comment
Share on other sites

Awesome stuff, works like a charm.  Thanks guys for the help, both suggestions work fantastic!

 

Just curious now more than anything.

 

Can either of these examples be adapted to more than 2 queries?  Say I have a group of 8 queries that I want to calculate the percent.  Can I just expand on these or are these meant for only 2?

 

yup, you can do this with as many queries as you want to give you parts of a whole.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.