Phpmyadmin, Sql query to total data then subtract one sum from a group of other sums
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Phpmyadmin, Sql query to total data then subtract one sum from a group of other sums
Hi,
Working with hourly payroll data; the payroll company does not provide day hours as such. It must be calculated in other ways.
It provides Total Hours, Afternoon Hours and Evening Hours etc, so the Afternoon and Evening Hours must be added and then subtracted from the Total Hours to arrive at Day Hours.
I have managed to get these totals from an .csv file using Phpmyadmin and Mysql on Ubuntu 12.04, using the following query :
Quote:
SELECT sum( `Reg Hourly Units` ) AS "Total Hours", sum(`Wknd Aft Prem Units` ) AS "WkEnd Aft Hours", sum( `Wknd Night Prem Units` ) AS "WkEnd Night", sum( `Wknd Day Prem Units` ) AS "Weekend Day hours", sum( `Wknd Aft Prem Units` ) AS "Weekend Afternoon", sum( `Wknd Night Prem Units` ) AS "Weekend Night hours")
FROM `PP1R`
WHERE `Occupation` = 'Tech'
AND `Home Department Name` = 'XXX'
Where I am stuck is how to calculate "Day Hours" by subtracting from Total Hours the other sums all in one query.
Also, if someone could recommend a good primer for novices on SQL math it would be appreciated.
Thanks in advance !
Last edited by ceyx; 07-22-2013 at 07:55 PM.
Reason: clarity
SELECT sum( `Reg Hourly Units` ) AS "Total Hours", sum(`Wknd Aft Prem Units` ) AS "WkEnd Aft Hours", sum( `Wknd Night Prem Units` ) AS "WkEnd Night", sum( `Wknd Day Prem Units` ) AS "Weekend Day hours", sum( `Wknd Aft Prem Units` ) AS "Weekend Afternoon", sum( `Wknd Night Prem Units` ) AS "Weekend Night hours")
FROM `PP1R`
WHERE `Occupation` = 'Tech'
AND `Home Department Name` = 'XXX'
I would try something like this: (please check grammar)
SELECT sum( `Reg Hourly Units` ) AS Total_Hours, sum(`Wknd Aft Prem Units` ) AS WkEnd_Aft_Hours, sum( `Wknd Night Prem Units` ) AS WkEnd_Night, sum( `Wknd Day Prem Units` ) AS Weekend_Day_hours, sum( `Wknd Aft Prem Units` ) AS Weekend_Afternoon, sum( `Wknd Night Prem Units` ) AS Weekend_Night_hours, (TotalHours - WkEnd_Aft_Hours - WkEnd_Night - Weekend_Day_hours - Weekend_Afternoon - Weekend_Night_hours) AS DayHours
FROM `PP1R`
WHERE `Occupation` = 'Tech'
AND `Home Department Name` = 'XXX'
I would no use double quotes because it depends on mysql version. Donīt remember if you have to create the fields in the database.
I used something like this using mysql and php. I takes time until it works. Is very tricky.
Tell me if there is any error. If there is no way to make it work, please ask.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.