Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Finding gaps between consecutive shifts where gap is greater than one hour

$
0
0

Problem:

California applies a penalty to a company when an employee has a gap between two shifts greater than hour (>60mins) in the same day, for a given pay week. There are other caveats, but for simplicity sake, I am just looking to identify any workers who have a gap between two shifts worked, in the same day, greater than an hour, for any given week.

Desired output:

EmployeeId CalDate WarehouseId StartTime EndTime

001856 2013-07-09 762 11:45AM 6:10PM 001856 2013-07-09 762 7:55PM 10:45PM 004146 2013-07-10 122 1:45PM 6:05PM 004146 2013-07-10 122 7:45PM 10:45PM

004232 2013-07-10 12 8:45AM 12:45PM
004232     2013-07-10 12 2:00PM 4:45pm

SQL Server 2012 Standard

DDL:

CREATE TABLE [fct].[TIMECARD_ACTIVITY](
	[Timecard_ActivityId] [int] IDENTITY(1,1) NOT NULL,
	[RegionId] [int] NOT NULL,
	[DivisionId] [int] NOT NULL,
	[WarehouseId] [int] NOT NULL,
	[CalDate] [datetime] NOT NULL,
	[EmployeeId] [int] NOT NULL,
	[Employee_HistId] [int] NOT NULL,
	[TimeCard_StatusId] [int] NOT NULL,
	[Override_ReasonId] [int] NOT NULL,
	[ActivityId] [int] NOT NULL,
	[TimeCardActivityId] [int] NOT NULL,
	[StartTime] [datetime] NOT NULL,
	[EndTime] [datetime] NOT NULL,
	[Minutes] [int] NOT NULL,
	[LastModified] [datetime] NOT NULL,
	[LastModifiedBy] [varchar](30) NULL,
	[Created] [datetime] NOT NULL,
	[CreatedBy] [varchar](30) NULL,
 CONSTRAINT [PK_TIMECARD_ACTIVITY_Timecard_ActivityId] PRIMARY KEY CLUSTERED 
(
	[Timecard_ActivityId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

INSERTS:

INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829753','1','1','144','7/16/2013 0:00','19412','77180','2','48','8','3227943','7/16/2013 10:45','7/16/2013 17:10','385','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');
INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829752','1','4','43','7/16/2013 0:00','18546','67249','2','48','8','3227940','7/16/2013 10:45','7/16/2013 17:15','390','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');
INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829751','1','1','144','7/16/2013 0:00','19412','77180','2','48','7','6848549','7/16/2013 13:35','7/16/2013 14:05','30','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');
INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829750','1','4','43','7/16/2013 0:00','18546','67249','2','48','7','6848520','7/16/2013 14:25','7/16/2013 14:55','30','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');
INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829749','1','4','43','7/15/2013 0:00','18546','67249','2','48','5','3227939','7/15/2013 13:15','7/15/2013 16:50','215','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');
INSERT INTO [fct].[TIMECARD_ACTIVITY] (Timecard_ActivityId,RegionId,DivisionId,WarehouseId,CalDate,EmployeeId,Employee_HistId,TimeCard_StatusId,Override_ReasonId,ActivityId,TimeCardActivityId,StartTime,EndTime,Minutes,LastModified,LastModifiedBy,Created,CreatedBy) VALUES ('25829748','3','9','106','7/14/2013 0:00','21248','67441','2','48','8','3227937','7/14/2013 11:15','7/14/2013 17:45','390','7/18/2013 5:31','NULL','7/18/2013 5:31','NULL');

I've tried self joins, my own attempt at a cross apply, and I have failed... Can anyone help please? The desired output Start and End times can be in Standard time like the INSERTS..

Regards,

C








Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>