I'm trying to assign a unique ID to a case where an employee is out of the office. This could be a single day, a entire week or a collection of weeks.
I have a table called 'absence' that looks like this:
Absence_Date employee_ID Hours_Lost DayofWeek
2020-09-28 001 7.5 Mon
2020-04-15 002 7.5 Wed
2020-04-16 002 7.5 Thurs
2020-02-10 003 7.5 Mon
2020-05-12 004 3.5 Tue
2020-06-08 004 1 Mon
2020-06-10 004 7.5 Wed
2020-06-11 004 7.5 Thurs
2020-06-15 004 7.5 Mon
2020-06-16 004 7.5 Tue
2020-06-17 004 7.5 Wed
2020-06-18 004 7.5 Thurs
2020-06-22 004 7.5 Mon
I also have a table called 'working_pattern', with their shift patterns:
employee_ID DayofWeek Working_Hours
001 Sun 0
001 Mon 7.5
001 Tue 7.5
001 Wed 7.5
001 Thurs 7.5
001 Fri 7.5
001 Sat 0
002 Sun 0
002 Mon 7.5
002 Tue 7.5
002 Wed 7.5
002 Thurs 7.5
002 Fri 7.5
002 Sat 0
002 Sun 0
003 Mon 7.5
003 Tue 7.5
003 Wed 7.5
003 Thurs 7.5
003 Fri 7.5
003 Sat 0
003 Sun 0
004 Mon 7.5
004 Tue 7.5
004 Wed 7.5
004 Thurs 7.5
004 Fri 0
004 Sat 0
Note that employee_ID 004 doesn't work a Friday
I can join these two tables using:
SELECT a.*, w.working_hours
FROM absence a
JOIN working_pattern w
ON a.employee_ID = w.employee_ID
AND a.DayofWeek = w.DayofWeek
Which gives me:
Absence_Date employee_ID Hours_Lost DayofWeek Working_Hours
2020-09-28 001 7.5 Mon 7.5
2020-04-15 002 7.5 Wed 7.5
2020-04-16 002 7.5 Thurs 7.5
2020-02-10 003 7.5 Mon 7.5
2020-05-12 004 3.5 Tue 7.5
2020-06-08 004 1 Mon 7.5
2020-06-10 004 7.5 Wed 7.5
2020-06-11 004 7.5 Thurs 7.5
2020-06-15 004 7.5 Mon 7.5
2020-06-16 004 7.5 Tue 7.5
2020-06-17 004 7.5 Wed 7.5
2020-06-18 004 7.5 Thurs 7.5
2020-06-22 004 7.5 Mon 7.5
I'm trying to create a result that will group continuous instances of absence. Should look like this:
Absence_Date employee_ID Hours_Lost DayofWeek Working_Hours Absence_ID
2020-09-28 001 7.5 Mon 7.5 1
2020-04-15 002 7.5 Wed 7.5 2
2020-04-16 002 7.5 Thurs 7.5 2
2020-02-10 003 7.5 Mon 7.5 3
2020-05-12 004 3.5 Tue 7.5 4
2020-06-08 004 1 Mon 7.5 5
2020-06-10 004 7.5 Wed 7.5 6
2020-06-11 004 7.5 Thurs 7.5 6
2020-06-15 004 7.5 Mon 7.5 6
2020-06-16 004 7.5 Tue 7.5 6
2020-06-17 004 7.5 Wed 7.5 6
2020-06-18 004 7.5 Thurs 7.5 6
2020-06-22 004 7.5 Mon 7.5 6
I added a column using
DATEDIFF(day,Absence_Date,LAG(Absence_Date,1) OVER (ORDER BY t.Employee_Number, Absence_Date))
which gave me the days difference between one row to the next. Thinking I could say all the values of '1' would be consecutive days. However, this doesn't account for weekends or non working days.
Any suggestions?
Thank you!
EDIT
This is different to Detect consecutive dates ranges using SQL as this very creative solution will group instances within a week (or in theory within a month/year), however my instances may group over several weeks and even months.