2017-05-04 66 views
1

例如,我们有日期2017/5/4 01:00:00 PM在工作日我们有工作时间08:00至17:30如何计算SLA日期和时间?分配如何从SQL中指定的日期计算业务日期和时间

日期:2017年5月4日下午1点00分零零秒

如果SLA是指定日期8小时。

截止日期应该是:2017/5/5 11:30:00 AM排除周末和非营业时间。

+0

我不是很确定。 SLA的立场是什么?不能使用'DateTimeAddHours'方法吗? –

+0

@MightyBadaboom SLA =服务水平协议。在这种情况下,它指的是他们同意提供答复的时间。 – iamdave

+0

啊,好的。感谢您的信息:)但对于我来说,目前仍不清楚“日期指定”是什么意思。 –

回答

0

这是一个非常复杂的问题,我认为你是因为它。有许多天和时间会超出工作时间,您需要考虑。这将包括任何假期,半天,办公室关闭等。这最好通过维护被认为是工作时间的日期,小时甚至分钟表来处理。

如果你不想只为这一个表,一个简单的Dates表再加当一个特定的日期或时间被认为是工作时间与否的规则,第二台可以让你得到这个。

如果你甚至不用说,你将需要你想运行您的查询,每次派生表包括所有的这个查询中规则的工作时间。创建datetime值表的最有效的方法是理货表,而你的情况可以如下利用:

declare @DateAssigned datetime = '05-04-2017 13:00:00'; 
declare @DayStart time = '08:00:00'; 
declare @DayEnd time = '17:30:00'; 
declare @SLAMinutes int = 480; -- 8 hours * 60 minutes per hour 


     -- cte to create a table with 10 rows in 
with n(n) as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1) 
     -- cte to cross join the rows together, resulting in 10^6 (1 million) rows. Add or remove joins per number of minutes required. 
     -- Use the row_number function to add an incremental number of minutes to the original @DateAssigned to get a table of minutes from the @DateAssigned value. 
    ,t(t) as (select dateadd(minute,row_number() over(order by (select null))-1,@DateAssigned) from n n1, n n2, n n3, n n4, n n5, n n6) 
     -- Select the first @SLANumber number of rows that meet your Working Time criteria. We add 2 to this value do resolve the fencepost problem. 
    ,d(d) as (select top (@SLAMinutes + 2) t 
       from t 
       where cast(t as time) >= @DayStart  -- Only return minutes from 08:00 onwards. 
       and cast(t as time) <= @DayEnd  -- Only return minutes up to 17:30. 
       and datepart(weekday,t) not in (7,1) -- Only return minutes not on Saturday or Sunday. 
       order by t) 
    -- The MAX value in the cte d will be the last minute of your SLA window. 
select @DateAssigned as DateAssigned 
     ,max(d) as SLADeadline 
from d; 

,输出:

+-------------------------+-------------------------+ 
|  DateAssigned  |  SLADeadline  | 
+-------------------------+-------------------------+ 
| 2017-05-04 13:00:00.000 | 2017-05-05 11:30:00.000 | 
+-------------------------+-------------------------+