考虑一下我们正在完成三个班次。
Shift A -->StartTime 7:00:00 AM EndTime 15:30:00 PM
Shift B -->StartTime 15:30:00 PM EndTime 11:59:59 PM
Shift c -->StartTime 12:00:00 AM EndTime 06:59:59 AM
我需要最后三班开始时间和结束时间&最后一次运行班次开始时间&结束时间。
E.g:
Now, I am in fist shift. Today date is --> 14-Mar-2017 11:13 AM
需要输出如下格式:
Last Third Shift Start Time : 2017-03-13 07:00:00 AM
Last Third Shift End Time : 2017-03-14 06:59:59 AM
Last Running Shift Start Time : 2017-03-13 12:00:00.000
Last Running Shift End Time : 2017-03-14 06:59:59.000
答案 0 :(得分:0)
create table #tmp
(
shiftstarttime datetime,
shiftendtime datetime
)
insert into #tmp values ('1-Jan-2017 7:00:00','1-Jan-2017 15:30:00')
insert into #tmp values ('1-Jan-2017 15:30:00','1-Jan-2017 23:59:59')
insert into #tmp values ('2-Jan-2017 00:00:00','2-Jan-2017 06:59:59')
declare @currdate datetime=getdate()
declare @lastdate datetime=dateadd(d,-1,getdate())
declare @tab table
(
RankID int identity,
starttime datetime,
endtime datetime
)
insert into @tab
select dateadd(d,datediff(d,shiftstarttime,@currdate),shiftstarttime), dateadd(d,datediff(d,shiftendtime,@currdate),shiftendtime)
from #tmp
UNION
select dateadd(d,datediff(d,shiftstarttime,@lastdate),shiftstarttime), dateadd(d,datediff(d,shiftendtime,@lastdate),shiftendtime)
from #tmp
select starttime as LastThirdShiftStartTime,endtime as LastThirdShiftEndTime
from @tab where RankID = (select RankID from @tab where @currdate between starttime and endtime)-3
select starttime as LastRunningShiftStartTime,endtime as LastRunningShiftEndTime
from @tab where RankID = (select RankID from @tab where @currdate between starttime and endtime)-1
drop table #tmp
您可以尝试此示例代码。我已将换档时间存储在临时表中,并根据当前服务器时间,它将为您提供最后的第3班开始和结束时间以及最后班次开始和结束时间。