Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Timesheet/Overtime Report -transact SQL -CA Rules

Author  Topic 

ImplicitSystems
Starting Member

2 Posts

Posted - 2009-04-24 : 03:43:11
I am working on a complex SP that parses a worked hours table and sums by employee id groups the hours worked for a given week. The issue that I am stuck on is validating and adjusting the total hours for the regular time, overtime and doubletime. The following is a snippet where the worked hours values need evaluating.

	INNER  JOIN
(SELECT
PunchID,
SUM(RegularTime) as RT, -- Past 40 hours needs to flow into overtime \ doubletime
SUM(OverTime) as OverTime,
SUM(DoubleTime) as DoubleTime ,
-- This is where I am stuck.
-- I am thinking of adding a case statement like the following:
(CASE
WHEN @RT <= 40 THEN @RT AS RegularTime
WHEN @RT > 40 THEN ‘40’ AS RegularTime, OverTime = OverTime+( OverTime-40)
END)
FROM
WorkedHours
WHERE (WorkDate BETWEEN @WeekStarts AND @WeekEnds))
) W ON E.PunchID = W.PunchID



I think a case statement is the right step but not 100% sure not to mention I do not know the exact syntax.

Any help is more than welcome. there is also a 7th day rule that I need to apply too.

On the 7th day:
The first 8 hours = overtime
More than 8 hours = double time


Thanks,
Andy

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-24 : 03:47:12
This may be a starter
INNER JOIN	(
SELECT PunchID,
CASE
WHEN SUM(RegularTime) > 40 THEN 40
ELSE SUM(RegularTime)
END AS RT, -- Past 40 hours needs to flow into overtime \ doubletime
CASE
WHEN SUM(RegularTime) > 40 THEN SUM(RegularTime) - 40
ELSE 0
END AS OverTime
FROM WorkedHours
WHERE WorkDate BETWEEN @WeekStarts AND @WeekEnds
GROUP BY PunchID
) W ON E.PunchID = W.PunchID



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

ImplicitSystems
Starting Member

2 Posts

Posted - 2009-04-25 : 01:57:50
Thank you, this worked great with very minor tweaks! I'm gonna give the 7 day overtime rule a few tries and well I stuck on this one. I posted the whole SP for more clarity of the desired results.

The section labeled -- Join the WorkedHours table to sum the worked hours for the 7th day -- is what I added to tally the seventh day. The reason I am doing a sum for one day is that an employee can work twice in a day.

Prior to adding this the procedure worked fine. Now it is repeating multiple entries per day. I also posted this to make sure that I am designing this properly as I am not a SQL Guru, yet. LOL.



DECLARE   @WorkDate           datetime ='4/6/2009'

set datefirst 7
DECLARE @WorkWeek int = DATEPART(WW,@WorkDate)
DECLARE @WeekStarts datetime = DATEADD(WW,DATEDIFF(WW,0,@WorkDate),-1)
DECLARE @WeekEnds datetime = DATEADD(WW,DATEDIFF(WW,0,@WorkDate),5)


SELECT DISTINCT
(SELECT ADPCode FROM Company WHERE (Company.CompanyID = E.CompanyID)) AS [CO CODE],
@WorkWeek AS [BATCH ID],
AR.ADP AS [FILE #],
'WorkWeek ' + CAST(@WorkWeek AS NVARCHAR)+ ' - ' + CAST(WorkDate AS NVARCHAR) AS [BATCH DESCRIPTION],
E.FirstName + ' ' + E.LAStName AS [Employee Name],
'1' AS [Pay #],
'' AS [Tax Frequency],
W.RegularTime AS [Reg Hours],
W.OverTime + W7.RegularTime7 AS [O/T Hours],
W.DoubleTime + W7.OverTime7 + W7.DoubleTime7 AS [Double Time],
'' AS [Piece Work],
'N' AS [Cancel Pay],
'' AS [Adjust Ded Code]

FROM Employee E

-- Join the WorkedHours table to sum the worked hours for the first 6 days
INNER JOIN
(SELECT
PunchID,
CASE
WHEN SUM(RegularTime) > 40 THEN 40
ELSE SUM(RegularTime)
END AS RegularTime,
CASE
WHEN SUM(RegularTime) > 40 THEN ((SUM(RegularTime) - 40) + SUM(OverTime))
ELSE SUM(OverTime)
END AS OverTime,

SUM(DoubleTime) AS DoubleTime
FROM WorkedHours
WHERE (WorkDate BETWEEN @WeekStarts AND DATEADD(day, -1, @WeekEnds))
GROUP BY PunchID
) W ON E.PunchID = W.PunchID

-- Join the WorkedHours table to sum the worked hours for the 7th day
INNER JOIN
(SELECT
PunchID,
SUM(RegularTime) AS RegularTime7,
SUM(OverTime) AS OverTime7,
SUM(DoubleTime) AS DoubleTime7
FROM WorkedHours
WHERE (WorkDate = CAST(@WeekEnds AS DATE))
GROUP BY PunchID
) W7 ON E.PunchID = W.PunchID

-- Join the ADP_OS3_EmployeesRoster table to add the ADP Employee [File #]
INNER JOIN
ADP_OS3_EmployeesRoster AR ON AR.SSN = E.SSN

-- Rejoin the WorkedHours table to add the WorkDate
INNER JOIN
(SELECT
PunchID, WorkDate
FROM WorkedHours
WHERE (WorkDate BETWEEN @WeekStarts AND @WeekEnds)

) WD ON E.PunchID = WD.PunchID

WHERE (WorkDate BETWEEN @WeekStarts AND @WeekEnds)
Order by [FILE #]

SELECT @WorkWeek AS WorkWeek, @WeekStarts AS WeekStarts, @WeekEnds AS WeekEnds




Go to Top of Page
   

- Advertisement -