我假设您需要一个函数来增加升级时间的分钟数,但仅限于工作时间。
你可以通过使用类似的东西从你的第一张桌子计算你的分钟数
Select SUM(Minute) from auxiliary where Time <= CURRENT_TIMESTAMP
以下函数将执行此操作,跳过@SCStartHours 和@SCEndHours 之外的任何时间。周六和周日之外:
该函数将像这样使用:
将 60 个“工作分钟”添加到 OPEN_TIME:
select AddWorkingMinutes(OPEN_TIME, 60, 9,17)
create Function AddWorkingMinutes(@EscalateOnDate datetime, @escalationminutes int,
@SCStartHours int, @SCEndHours int)
returns datetime
as
begin
declare @workminutesperday int = (@SCEndHours - @SCStartHours)*60
declare @workdays int = @escalationminutes/@workminutesperday
declare @minutes int = @escalationminutes - @workminutesperday * @workdays
-- If the date falls out of hours then move it forward to the next earliest period
while DATENAME(weekday, @EscalateOnDate ) = 'Saturday'
OR DATENAME(weekday, @EscalateOnDate ) = 'Sunday'
OR DATEPART(hour, @EscalateOnDate ) >= @SCEndHours
begin
set @EscalateOnDate = DATEADD(dd,1,@EscalateOnDate )
set @EscalateOnDate = DATEADD(hh,@SCStartHours,DATEADD(dd,0, DATEDIFF(dd,0, @EscalateOnDate )))
end
while DATEPART(hour,@EscalateOnDate ) < @SCStartHours
begin
set @EscalateOnDate = DATEADD(hh,@SCStartHours,DATEADD(dd,0, DATEDIFF(dd,0, @EscalateOnDate )))
end
--now check if we have enough minutes in the day remaining, if not add a day and take the remaining minutes off.
declare @minutesremaining int = DATEDIFF(minute, @EscalateOnDate , DATEADD(hh,@SCEndHours,DATEADD(dd,0, DATEDIFF(dd,0, @EscalateOnDate ))))
if @minutesremaining < @minutes
begin
set @minutes = @minutes - @minutesremaining
set @workdays = @workdays + 1
set @EscalateOnDate = DATEADD(hh,@SCStartHours,DATEADD(dd,0, DATEDIFF(dd,0, @EscalateOnDate )))
end
return DATEADD(minute,@minutes,DATEADD(day, (@workdays % 5) + CASE ((@@DATEFIRST + DATEPART(weekday, @EscalateOnDate ) + (@workdays % 5)) % 7)
WHEN 0 THEN 2
WHEN 1 THEN 1
ELSE 0 END, DATEADD(week, (@workdays / 5), @EscalateOnDate )))
end