I have a table with working time hours in relation to the shift:
Shift Start End
------ ------- ------
1st 06:00 09:30
1st 10:00 14:30
Office 07:30 09:45
Office 10:00 13:15
Office 14:00 17:00
Based on that data, I am trying to build query that returns for each Shift the breaks that they are having:
Shift Start End
------ ------- ------
1st 09:30 10:00
Office 09:45 10:00
Office 13:15 14:00
I was trying to achieve this by first ordering the working times with Row_Number() over (partition by Shift order by Start) as rn and use that number in the join expression:
Select x.Shift, x.Finish, y.Start
From range
left outer join times x on range.IDDia = x.IDDia and x.rn%2 = 1
left outer join times y on range.IDDia = y.IDDia and y.rn%2 = 0
but it does not work. Anybody has an idea of how to get the result I need?
Create the data:
IF OBJECT_ID('tempdb..#times') IS NOT NULL
DROP TABLE #times
CREATE TABLE #times (Shift nvarchar(10), start time, finish time)
INSERT INTO #times (Shift, start, finish) Values ('1st', '06:00', '09:30')
INSERT INTO #times (Shift, start, finish) Values ('1st', '10:00', '14:30')
INSERT INTO #times (Shift, start, finish) Values ('Office', '07:30', '09:45')
INSERT INTO #times (Shift, start, finish) Values ('Office', '10:30', '13:15')
INSERT INTO #times (Shift, start, finish) Values ('Office', '14:00', '17:00')