calculating percentages php/mysql

Posted on 2012-09-17
Last Modified: 2012-09-17
I have three tables in MySQL - total sales, tuff sales, and float sales
tuff and float are types of sale.
Each table has columns year, month and sales.
The sum of sales for each type of sale = total. So for each month tuff sales + float sales = total sales.
I have a php query that reads the figures for each table in separate columns.
So if total sales  for August 2012 were £120, and tuff sales = £100 and float = £20, then the php result would read:
2012  08  120  100  20
However is it possible for php to execute the SQL that presents the tuff and float sales as percentage of total?
So  then the php result would read:
2012   08   120  83.3 16.7
I could write the percentage to the source table as a fixed calculation, and php would then read that column, but where is the fun in that?!
Question by:ClaytonGlass
    LVL 58

    Expert Comment

    If we had the actual sql it would be easier to give an example
    But basically just use SUM(field/100) as tuffsales_percentage
    But as you're using 3 tables (???) then the sql will be considerably more complicated.

    As you have the sales in one table then I don't see the need for another seperate table just to hold this value - you can achieve the result with a simple sql on your tuff sales table e.g.
    select sum(tuff_total) from tuff_sales_table where tuff_date BETWEEN '2012-08-01' AND '2012-08-31'
    LVL 34

    Accepted Solution

    I would just use PHP to do the percentage calculation. MySQL or PHP can do it, but I always try to keep the database calculations to a minimum - especially when the script can do it the exact same way.

    $year = "2012";
    $month = "08;
    $total = 120;
    $tuff = 100;
    $float = 20;

    echo "{$year} {$month}  {$total}  " . round(($tuff/$total)*100,1) . " " . round(($float/$total)*100,1);
    LVL 25

    Expert Comment

    The way I see it, you have two choices ...
    1. Change the SQL to calculate and return the percentage
    2. Add logic in PHP to calculate the percentage on the returned values.

    To help too much further with either would require more details on the SQL/PHP script.  But the match is simple enough ... either way you would need to consider a round() on the result.

    Author Closing Comment

    Thank you very much!
    LVL 25

    Expert Comment

    rounding can be an issue ... sometimes if you round two numbers in a % calculation, the total will not = 100%.  If this is of concern, calculate the first % then subtract that from 100 to get the other %.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now