开发者

Need help in finding nearest shift time in SQL

开发者 https://www.devze.com 2023-03-10 21:38 出处:网络
I have following table for shifts. AutoId StartTimeEndTime 108:00:00 AM0开发者_如何学Python4:00:00 PM

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号