我想创建一个基本的案例记录系统,当有人打开新问题时,该问题被分配了带有给定数量的SR_NUMBER。例如,sr_number 1为4小时,2为6小时,3个是8小时,4个小时为24小时。
现在,在时间戳记上增加数小时很容易,但是我需要考虑工作时间为09:00至17:00星期一至周五。基本上,截止日期为12个工作时间。计算应在同一天登录,剩余11个小时到下一个工作日的问题进行计算。如果是太阳,应该直接考虑到星期一。
示例:casus创建于:10/06/2015 12:04:39 PM-与SR_NUMBER 1(12小时)截止日期为现在:10/07/2015 12.05 PM 有意义吗?
另一个捕获量是我需要搁置数小时,这两个只需在工作时间内即可。 在某些情况下,星期六有些假期正在工作。我应该继续进行。 我尝试执行DatePart,DateAdd和日期函数。但是我只能找到工作日。 我是SQL的新手。
最简单的会计延迟方法是剩下的几个小时,然后用递归的cte ptecte几天,直到工作时间比剩下的延迟较多的工作时间。
Id T sr_number
delay
with
wh as
(
select *, datediff(second, starttime, endtime) / 3600.0 wh
from working_hours
),
delays as
(
select
row_number() over (order by contact_date) id, -- Will ease reading.
contact_date t,
a.sr_number,
cast(floor(cast(contact_date as float)) as datetime) d,
cast(contact_date as time) h,
delay
from assign a join contract c on c.sr_number = a.sr_number
),
-- The day the contact occurred is special, as the timer began to run in the middle of the day.
firstday as
(
select
*, -- wh = working hours of the current day.
-- If contact occurred after 1 working hour with a delay of 4 hours,
-- it's as if it occurred at working hours start but with a delay of 5 hours.
delay + case
when h <= wh.starttime or wh.starttime is null then 0
when h < wh.endtime then datediff(second, starttime, h) / 3600.0
else wh
end as remaining
from delays left join wh on datepart(dw, t) between wh.startday and wh.endday
),
running as
(
select id, t, sr_number, delay, d, wh, cast(remaining as float) remaining from firstday
union all
select
-- Stable part (attributes of the call):
id, t, sr_number, delay,
-- Evolving part, relative to the day now explored:
d + 1,
(
select wh.wh from wh where datepart(dw, d + 1) between wh.startday and wh.endday
-- Here you could put hold days (specific to Name, which would then have to be included in the CTEs):
--and not exists (select 1 from on_hold oh where oh.Name = running.Name and d + 1 between oh.startday and oh.endday)
),
-- What remains in the delay after the previous day was elapsed:
remaining - coalesce(wh, 0.0)
from running
where wh is null or remaining > wh
)
select
id, t, sr_number, delay,
dateadd(second, datediff(second, 0, starttime) + remaining * 3600.0, d) complete_date
from running join wh on datepart(dw, d + 1) between wh.startday and wh.endday
where running.remaining <= running.wh;
1 | 2015-09-0805:39:29.000 | 3 | 8 | |
---|---|---|---|---|
2 | 2015-10-0612:04:39.000 | 1 | 4 | 2015-10-0616:04:39.000 |
3 | 2015-10-0700:45:00.000 | 3 | 8 | 2015-10-0717:00:00.000 |
8 | 2015-10-1212:00:00.000 | 2 | 6 | 2015-10-1310:00:00.000 |
7 | 2015-10-1012:45:00.000 | 4 | 24 | 2015-10-1417:00:00.000 |
6 | 2015-10-0912:45:00.000 | 4 | 24 | 2015-10-1412:45:00.000 |
5 | 2015-10-0906:45:00.000 | 4 | 24 | 2015-10-1317:00:00.000 |
4 | 2015-10-0822:45:00.000 | 4 | 24 | 2015-10-1317:00:00.000 |
尽管我找不到SQL Server 2005, |