Regarding "Can a shift extend to the next day? "
Yes a shift can start on monday and end on tuesday. In such a case, the end time will be smaller than or equal to start time. For example 10:00-08:00 indicates that the shift starts at 10 AM and ends at 08:00 AM the next day. 0800-0800 indicates a 24 hour shift which starts at 8 am on one day and ends at 8 am the next day.
Can there be a case where a shift starts on Sunday and the end time is smaller that the start time ? This would mean that the shift ends on Monday:
INSERT INTO TC27( Patient, Nurse, dy, st, en) SELECT 2000, 201, 'Sun', '2300','0200'
Yes, a shift can start on sunday and end on monday. It is acceptable to have a shift start on sunday 2300 and end on monday morning 0200.
Just to be sure everyone has understood FAQ 5, here is some tricky data and the result it should produce. They are 3 distinct cases.
IF OBJECT_ID('TC27','U') IS NOT NULL DROP TABLE TC27 GO CREATE TABLE TC27( SchID INT IDENTITY PRIMARY KEY, Patient INT, Nurse INT, dy CHAR(3), St CHAR(4), En CHAR(4) ) INSERT INTO TC27( Patient, Nurse, dy, st, en) SELECT 2000, 201, 'Mon', '0800','1200' UNION ALL SELECT 2000, 201, 'Mon', '0900','1300' UNION ALL SELECT 2000, 201, 'Mon', '1000','1400' UNION ALL SELECT 2000, 201, 'Mon', '1100','1600' UNION ALL SELECT 2000, 201, 'Mon', '1500','1700' UNION ALL SELECT 2000, 201, 'Tue', '0800','1000' UNION ALL SELECT 2000, 201, 'Tue', '0900','1200' UNION ALL SELECT 2000, 201, 'Tue', '1100','1400' UNION ALL SELECT 2000, 201, 'Tue', '1300','1600' UNION ALL SELECT 2000, 201, 'Tue', '1500','1700' UNION ALL SELECT 2000, 201, 'Wed', '0800','1000' UNION ALL SELECT 2000, 201, 'Wed', '0900','1400' UNION ALL SELECT 2000, 201, 'Wed', '1100','1500' UNION ALL SELECT 2000, 201, 'Wed', '1200','1600' UNION ALL SELECT 2000, 201, 'Wed', '1300','1700'
1 2000 201 Mon 0800 1200 No 2 2000 201 Mon 0900 1300 Yes 3 2000 201 Mon 1000 1400 Yes 4 2000 201 Mon 1100 1600 Yes 5 2000 201 Mon 1500 1700 No 6 2000 201 Tue 0800 1000 No 7 2000 201 Tue 0900 1200 Yes 8 2000 201 Tue 1100 1400 No 9 2000 201 Tue 1300 1600 Yes 10 2000 201 Tue 1500 1700 No 11 2000 201 Wed 0800 1000 No 12 2000 201 Wed 0900 1400 Yes 13 2000 201 Wed 1100 1500 No 14 2000 201 Wed 1200 1600 Yes 15 2000 201 Wed 1300 1700 Yes
What is the expected result for this data?
INSERT INTO TC27( Patient, Nurse, dy, st, en) SELECT 2000, 201, 'Mon', '0100','2300' UNION ALL SELECT 2000, 201, 'Mon', '2200','0200' UNION ALL SELECT 2000, 201, 'Tue', '0100','2300' UNION ALL SELECT 2000, 201, 'Tue', '2200','0200' UNION ALL SELECT 2000, 201, 'Wed', '0100','2300' UNION ALL SELECT 2000, 201, 'Wed', '2200','0200' UNION ALL SELECT 2000, 201, 'Thu', '0100','2300' UNION ALL SELECT 2000, 201, 'Thu', '2200','0200' UNION ALL SELECT 2000, 201, 'Fri', '0100','2300' UNION ALL SELECT 2000, 201, 'Fri', '2200','0200' UNION ALL SELECT 2000, 201, 'Sat', '0100','2300' UNION ALL SELECT 2000, 201, 'Sat', '2200','0200' UNION ALL SELECT 2000, 201, 'Sun', '0100','2300' UNION ALL SELECT 2000, 201, 'Sun', '2200','0200'
They overlap in an alternating fashion No, Yes, No, Yes,... to the end.The idea is that once an interval is marked as overlapping, it is 'eliminated' and can no longer participate in determining other overlaps.
In this case we must assume first schedule, since all schedules are overlaping.
The answer to your question is NO, you cannot assume that only the first schedule does not overlap.It's true they are all overlapping 'together' - what I call a chain of overlaps.But maybe one way to describe the objective is that, for a single nurse, we want the largest subset of non-overlapping intervals.Thuse the answer should be:Mon 0100 2300 NoMon 2200 0200 YesTue 0100 2300 NoTue 2200 0200 Yesetc.
And for subsets with same summary period time?
SELECT 2000, 201, 'Mon', '0100','2300' UNION ALL SELECT 2000, 201, 'Mon', '2200','2000' UNION ALL SELECT 2000, 201, 'Tue', '0100','2300' UNION ALL SELECT 2000, 201, 'Tue', '2200','2000' UNION ALL
etc.
I don't understand your question.All you have changed is the end time of the 2nd and 4th period from 0200 to 2000. This will not change the result. It's still No, Yes, No, Yes,...
My mistake. I have use only same period.
Another data.
SELECT 2000, 201, 'Mon', '0800','2200' UNION ALL - period 14h, overlap 2h left, 2h right SELECT 2000, 201, 'Mon', '2000','1000' UNION ALL - same period 14h, overlap 2h left, 2h right
Same start,end in Tue,Wed,Thu,Fri,Sat,Sun
Possible results:
Mon 0800 2200 No Mon 2000 1000 Yes Tue 0800 2200 No Tue 2000 1000 Yes
or
Mon 0800 2200 Yes Mon 2000 1000 No Tue 0800 2200 Yes Tue 2000 1000 No
Managed Windows Shared Hosting by OrcsWeb