Problem: Calculate the amount of time between two dates with only using the shift dates. The shift dates are the only allowed time to count time. So in the data example below, I'd want to see the time difference for each of the #Rejections records independently. Ideally, I'd like to also take into account the lunch break but I don't if that makes this even harder to handle.
Data:
CREATE TABLE #Shifts
(
ShiftId INT
,StartTime DECIMAL(6,2)
,EndTime DECIMAL(6,2)
,LunchStart DECIMAL(6,2)
,LunchEnd DECIMAL(6,2)
);
INSERT INTO #Shifts VALUES (1, 6.00, 16.75, 11.75, 12.50) /*6am to 4:45pm*/
INSERT INTO #Shifts VALUES (2, 17.00, 3.75, 23.00, 23.75) /*5pm to 3:45am (next day)*/
INSERT INTO #Shifts VALUES (3, 5.00, 17.75, 12.00, 12.75) /*5am to 5:45pm*/
CREATE TABLE #Rejections
(
JobId INT
,WeldId INT
,IndicationNum INT
,FirstRejectedDate DATETIME
,LastAcceptedDate DATETIME
);
INSERT INTO #Rejections VALUES (500, 700, 2, '2017-01-03 22:35:31.000', '2017-01-04 01:38:16.000')
INSERT INTO #Rejections VALUES (500, 701, 3, '2017-01-04 01:48:55.000', '2017-01-06 09:21:11.000')
I am seeking some assistance on how to solve this problem. I am a novice at SQL Server and this problem has me totally stumped. I don't even know where to begin. I am using SQL Server 2008 R2 if that helps for available commands. Can someone please help me figure out how to achieve this?