I have a table called shifts which has client_id, employee_id, Shift_start, shift_end.
shift_start and shift_end are timestamps.
employees (nurses) are allocated to the client and are often on 24 hour shifts, so the following is a typical situation.
client_id employee_id shift_start shift_end
15 100 2018-09-01 07:00:00 2018-09-01 19:00:00
15 200 2018-09-01 19:00:00 2018-09-02 07:00:00
Now assume employee 200 is 30 minutes late, her shift_start should be 2018-09-01 19:30:00 and employee 100 shift_end should be changed to 2018-09-01 19:30:00. Straight forward so far.
however imagine employee 200 dishonestly does not change the shift start time and claims she came on time. Then we would have more than 24 hours and cannot bill the client for more than 24 hours a day.
How could I search for overlapping shift times in this scenario?
I have a query that looks for shifts more than 24 hours and it works like a charm. However the above scenario perhaps is two 6 hour shifts but a similar scenario like so:
client_id employee_id shift_start shift_end
15 100 2018-09-01 06:00:00 2018-09-01 12:30:00
15 200 2018-09-01 12:00:00 2018-09-02 18:00:00
My query for the 24 hour scenario is as follows:
select client_id, date(shift_start) ,
sum(time_to_sec(timediff(shift_end, shift_start )) / 3600) as totalTime
from shifts
where shift_month = '2018-09'
group by date(shift_start),client_id
having sum(time_to_sec(timediff(shift_end, shift_start )) / 3600) > 24
How could I find the overlap regardless of how long the shifts actually are?