I’m a self-taught TSQLer and leverage a bunch off of this forum.
You guys are a great help.
I use MS SQL Server Management Studio 12.0.2000.8 and MS SQL Server 2014.
My SQL is supposed to find any employee that has over 40 hours in any week (overtime) and, also, that employee has a pay code of 2 on a Thursday of that week.
The SQL below finds overtime OK but I can’t get it to only include folks that have a pay code of 2 on a Thursday of that week. I can't make multiple HAVING statements without getting an error.
Suggestions please.
Expected results from test data below:
EmployeeNum | Start of Pay Week | Total Punched Hours | Total Breaks | Total Work Hours | Non_overtime | Overtime |
1640 | 3/19/2017 | 42.11666667 | 1.5 | 40.62 | 40 | 0.62 |
4218 | 3/19/2017 | 51.46666667 | 2 | 49.47 | 40 | 9.47 |
IFOBJECT_ID('dbo.ZTable1','U')
ISNOTNULL
DROPTABLE dbo.ZTable1;
GO
CREATETABLE dbo.ZTable1( [EmployeeNum] Numeric(5,0), [TimeIn]datetime, [TimeOut]datetime, [PayCode]Numeric (1,0),[LBreak] Numeric (3,2));
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/06 06:00','2017/03/06 14:32',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/07 05:59','2017/03/07 14:31',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/08 05:59','2017/03/08 14:30',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/09 05:59','2017/03/09 14:31',9,.50 --< Thursday
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/10 05:59','2017/03/10 14:31',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/20 06:01','2017/03/20 14:31',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/21 06:01','2017/03/21 14:33',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/22 05:59','2017/03/22 14:33',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/23 05:59','2017/03/23 14:30',2,0 --< Thursday
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/24 09:00','2017/03/24 17:00',2,0
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/27 06:00','2017/03/27 14:32',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 1640,'2017/03/28 06:00','2017/03/28 14:34',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/06 06:12','2017/03/06 14:50',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/07 05:11','2017/03/07 15:46',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/08 06:12','2017/03/08 14:50',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/09 07:12','2017/03/09 14:54',9,.50 --< Thursday
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/10 07:12','2017/03/10 14:52',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/20 06:12','2017/03/20 15:49',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/21 07:13','2017/03/21 15:49',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/22 06:13','2017/03/22 15:51',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/23 07:12','2017/03/23 10:08',2,0 --< Thursday
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/23 05:00','2017/03/23 15:01',9,0 --< Thursday
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/24 05:10','2017/03/24 15:50',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/27 06:11','2017/03/27 15:48',9,.50
INSERTINTO dbo.ZTable1([EmployeeNum],[TimeIn],[TimeOut],[PayCode],[LBreak])SELECT 4218,'2017/03/28 06:16','2017/03/28 15:45',9,.50
GO
SELECT
[EmployeeNum]
,CONVERT(DATE,DATEADD(week,datepart(week,[TimeIn])- 1,
DATEADD(DAY,@@datefirst-
DATEPART(weekday,CAST(YEAR(GETDATE())ASVARCHAR)+'-01-01')- 6,
CAST(YEAR(GETDATE())ASVARCHAR)+'-01-01')))as'Start of Pay Week'
,sum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)as'Total Punched Hours'
,sum([LBreak])as'Total Breaks'
,sum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)-sum([LBreak])as'Total Work Hours'
,case
whensum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)-sum([LBreak])<= 40 then
sum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)-sum([LBreak])else 40 end
as Non_overtime
,case
whensum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)-sum([LBreak]) > 40 thensum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)- 40 -
sum([LBreak])else 0
endas Overtime
FROM dbo.ZTable1
GROUPby [EmployeeNum],datepart(week,[TimeIn])
HAVINGsum(DateDiff(second,[TimeIn],[TimeOut])/60.0/60.0)-sum([LBreak]) > 40
ORDERby [EmployeeNum],datepart(week,[TimeIn])