【问题标题】:How can I calculate business date and time from date assigned in SQL如何从 SQL 中指定的日期计算业务日期和时间
【发布时间】:2017-05-04 07:31:58
【问题描述】:

例如,我们的日期是 5/4/2017 01:00:00 PM,我们的工作时间是工作日的 08:00 到 17:30 我应该如何计算 SLA 日期和时间?

分配日期:5/4/2017 01:00:00 PM

如果 SLA 是从指定日期算起的 8 小时。

截止日期应为:2017 年 5 月 5 日上午 11:30:00,不包括周末和非营业时间。

【问题讨论】:

  • 我不太确定。 SLA 代表什么?你不能只使用DateTimeAddHours方法吗?
  • @MightyBadaboom SLA = 服务水平协议。在这种情况下,它指的是他们同意提供回复的时间。
  • 啊,好的。谢谢你的信息:) 但对我来说,“指定日期”是什么意思仍然不清楚。
  • 你想在什么级别看到这个?到时、分、秒?

标签: c# asp.net sql-server-2008-r2


【解决方案1】:

这是一个更复杂的问题,然后我认为您对此表示赞赏。您需要考虑到工作时间以外的日子和时间。这将包括任何假期、半天、办公室关闭等。最好通过维护一个被视为工作时间的日期、小时甚至分钟表来处理。

如果您不想要一个仅用于此目的的表格,则可以使用一个简单的 Dates 表格以及第二张规则表格,说明何时将特定日期或时间视为工作时间你来推导出这个。

如果您甚至没有,则每次要运行查询时都需要派生您的表,并包括 所有工作此查询中的时间 规则。创建datetime 值表的最有效方法是 Tally 表,在您的情况下可以按如下方式使用:

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 |
+-------------------------+-------------------------+

【讨论】:

    猜你喜欢
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多