I have following table for shifts.
AutoId StartTime EndTime
1 08:00:00 AM 0开发者_如何学Python4:00:00 PM
2 04:00:00 PM 12:00:00 AM
3 12:00:00 AM 08:00:00 AM
Now I want to assign Shifts to employees automatically..
For example, if someone comes 7:32 AM then his shift should be the first shift, that is, from 08:00:00 AM to 4:00:00 PM. If someone comes 11:45 PM then it should be automatically set as the third shift.
I also want that if an employee come in during the first hour of the shift then it should also work.
How do I find the nearest shift by giving employee InTime?
Something like:
SELECT AutoId, StartTime, EndTime
FROM Shifts
WHERE StartTime = (SELECT MIN(StartTime) FROM Shifts WHERE DATEADD(hh, 1, StartTime) > inTime)
EDIT: Added grace period
精彩评论