LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2013, 07:54 PM   #1
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Rep: Reputation: 59
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
 
Old 07-23-2013, 04:49 PM   #2
adelabarra
Member
 
Registered: May 2008
Location: Argentina
Distribution: Debian, Slackware
Posts: 49

Rep: Reputation: 3
Dear ceyx:
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'
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.

Alejandro.
 
1 members found this post helpful.
Old 07-24-2013, 06:04 PM   #3
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Original Poster
Rep: Reputation: 59
Hello Alejandro !

Thanks for your response. You certainly pointed me in the right direction.

The error message that was returned from your suggestion above :

Quote:
#1054 - Unknown column 'Total_Hours' in 'field list'
A quick search had me placing single quotes around the suggested fields like so :

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, (
'Total_Hours' - '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'
And it worked . The math was off but that is because my feilds were hastily chosen - but the engine works !

Thanks for your assistance.

Regards
 
Old 07-24-2013, 06:20 PM   #4
adelabarra
Member
 
Registered: May 2008
Location: Argentina
Distribution: Debian, Slackware
Posts: 49

Rep: Reputation: 3
Dear ceyx:
Thanks for the point.
Quote:
Also, if someone could recommend a good primer for novices on SQL math it would be appreciated.
There is a manual you can check for maths.
I downloaded it from mysql. Don't know if it is still available.
I tried to upoload it but I couldn't.

Alejandro

Last edited by adelabarra; 07-24-2013 at 06:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk - sum total if field = string schneidz Programming 12 03-20-2010 04:56 PM
sql query; group by / having h/w Programming 2 01-25-2010 12:12 AM
subtract total numbers of files from 2 directories Michael235 Linux - Newbie 2 03-28-2009 05:49 PM
SQL Query - sum / group by one year periods smaida Programming 2 03-09-2007 08:10 AM
sum total of file sizes allelopath Linux - General 3 01-07-2006 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration