?
Solved

Conditional Sums within SQL

Posted on 2008-10-31
3
Medium Priority
?
223 Views
Last Modified: 2013-11-11
I am attempting to calculate overtime hours worked from a query that obtains hours worked over a given time period.  The issue I'm having is that the query pulls back multiple associates for each date in the given date range and groups by 8 of the 9 columns in the data set.  So I'm at a loss at trying to determine how I can calculate each associate's overtime (if they worked any).

I am attaching the code I'm using as of now to pull back the hours worked.
SELECT	a.timeclock_date,
	a.employee_id, 
	b.payroll_id, 
	CASE WHEN a.work_code = 1 THEN 'WORKED HOURS' 
	WHEN a.work_code = 10 THEN 'VACATION' 
	WHEN a.work_code = 11 THEN 'FUNERAL' 
	WHEN a.work_code = 15 THEN 'SICK' 
	WHEN a.work_code = 16 THEN 'PERSONAL' ELSE a.work_code END   as work_code , 
	b.location_nmbr, 
	b.lname, 
	b.fname, 
	c.department_ID, 
	SUM(total_hours) AS total_hours
							
FROM 	edw1.timeclock a, 
	edw1.employee b, 
	(SELECT a.employee_ID AS payroll_id, 
	        a.department_ID, 
	        a.JOB, 
	        MAX(a.work_dt) AS work_dt
	 FROM   edw1.payroll_job a, 
	        (SELECT employee_ID AS payroll_id, 
		       MAX(work_dt) AS work_dt 
	         FROM   edw1.payroll_job 
		WHERE LOCATION = 01002 
                  GROUP BY 1) b 
          WHERE 	a.LOCATION = 01002 
          AND      a.employee_ID = b.payroll_id 
          AND	a.action_dt= b.action_dt
          GROUP BY 1,2,3) c 
							 
WHERE a.employee_id = b.employee_id 
AND b.payroll_id= c.payroll_id 
AND b.loc_num = 1002 
AND a.timeclock_date BETWEEN '2008-10-01' AND '2008-10-29'
GROUP BY 1,2,3,4,5,6,7,8

Open in new window

0
Comment
Question by:SpeedsterZ28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22850301
Can you just make your original query derived and then do formulat outside.  For example if overtime is anything over 160 hours (40 hours x 4 work weeks):
SELECT *
, CAST(CASE WHEN total_hours > 160 THEN 1 ELSE 0 END AS BIT) AS WorkedOvertime
FROM (
SELECT	a.timeclock_date,
	a.employee_id, 
	b.payroll_id, 
	CASE WHEN a.work_code = 1 THEN 'WORKED HOURS' 
	WHEN a.work_code = 10 THEN 'VACATION' 
	WHEN a.work_code = 11 THEN 'FUNERAL' 
	WHEN a.work_code = 15 THEN 'SICK' 
	WHEN a.work_code = 16 THEN 'PERSONAL' ELSE a.work_code END   as work_code , 
	b.location_nmbr, 
	b.lname, 
	b.fname, 
	c.department_ID, 
	SUM(total_hours) AS total_hours
							
FROM 	edw1.timeclock a, 
	edw1.employee b, 
	(SELECT a.employee_ID AS payroll_id, 
	        a.department_ID, 
	        a.JOB, 
	        MAX(a.work_dt) AS work_dt
	 FROM   edw1.payroll_job a, 
	        (SELECT employee_ID AS payroll_id, 
		       MAX(work_dt) AS work_dt 
	         FROM   edw1.payroll_job 
		WHERE LOCATION = 01002 
                  GROUP BY 1) b 
          WHERE 	a.LOCATION = 01002 
          AND      a.employee_ID = b.payroll_id 
          AND	a.action_dt= b.action_dt
          GROUP BY 1,2,3) c 
							 
WHERE a.employee_id = b.employee_id 
AND b.payroll_id= c.payroll_id 
AND b.loc_num = 1002 
AND a.timeclock_date BETWEEN '2008-10-01' AND '2008-10-29'
GROUP BY 1,2,3,4,5,6,7,8
) derived

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22850358
If it needs to be determined based on aggregate date from some of the other records that are showing as independing rows because of the grouping and you are running SQL 2005 you can try summing in an OVER statement that only groups by employeeID.

SUM(total_hours) OVER (PARTITION BY a.employeeID ORDER BY a.timeclock_date) AS total_emp_hours

You can then analyze that in outer query or try putting that in CASE statement -- don't have SQL Manager to try at the moment.

CASE WHEN (SUM(total_hours) OVER (PARTITION BY a.employeeID ORDER BY a.timeclock_date)) > 160 THEN 1 ELSE 0 END AS WorkedOvertime
0
 

Author Closing Comment

by:SpeedsterZ28
ID: 31512041
Thank you very much!  Just what I needed!
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

770 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