我有几张工单,想提供一份基于 PHP 服务水平协议(protocol)的报告。
新工单的状态可能为"new"并带有特定时间戳,一段时间后状态为“已关闭”并带有特定时间戳。
应该计算这两个时间戳之间的差异,但是只有周一到周五(上午 8 点到下午 4 点 30 分)的工作时间应该是相关的。因此,如果工单在周五下午 4 点创建并在下周一上午 9 点关闭,则耗时应为 1.5 小时。
有人知道如何从数据库中获取异常结果吗?
数据库是mysql数据库,bugtracking系统是开源的BTS Mantis Bugtracker。
数据库表的一个非常简单的部分:
表格错误:
编号 |状态 |创建日期(时间戳) |最后修改时间(时间戳)
表格历史
编号 | bug_id(引用错误)| status_old | status_new |修改日期(时间戳)
我在 PHP 中的查询: 获取在特定时间范围内设置为状态 30 的所有错误。 对于最高 SLA 级别,此帧介于 0 到 2 小时之间。 查询工作正常 - 但时间范围不关心工作时间......
选择 bug.id 作为 ID FROM mantis_bug_table 作为错误,mantis_bug_history_table 作为历史 WHERE history.new_status = 30 和 history.bug_id = bug.id AND (history.date_modified - bug.date_submitted) < '{$timeframe_end}' AND (history.date_modified - bug.date_submitted) > '{$timeframe_start}'";
最佳答案
您需要一个显示所有工作时段的表格,例如:
create table WorkingPeriod (
dtPeriodStart datetime primary key,
dtPeriodEnd datetime,
unique(dtPeriodEnd, dtPeriodStart)
)
您必须确保工作时间不重叠。
然后你就可以计算工作时间了。它将是整个周期的数量,加上开始和结束时的部分周期。此示例应适用于 Microsoft T-SQL,但您可能必须对 MySQL 使用 TIMESTAMPDIFF 或进行其他简单更改。
create function dbo.GetWorkingTimeSeconds(@dtStart, @dtEnd)
returns int
as
begin
declare @a int
-- Add up all the WorkingPeriods between @dtStart and @dtEnd
-- SUM adds them all up
select @a = SUM(
-- Get the number of seconds between the start of the period and the end
datediff(
-- We want the difference in seconds
second,
-- BUT if @dtStart is after the start of the period,
-- use @dtStart instead - so we don't count the part
-- of the period before @dtStart
case
when @dtStart < dtPeriodStart then dtPeriodStart
else @dtStart
end,
-- If @dtEnd is BEFORE the end of the period,
-- use @dtEnd instead, so we don't count the part of the period after @dtEnd
case
when @dtEnd > dtPeriodEnd then dtPeriodEnd
else @dtEnd
end
)
)
from
WorkingPeriods
-- Only include periods which overlap our time range
where dtPeriodEnd >= @dtStart and dtPeriodStart < @dtEnd
-- return the value
Return @a
end
为什么要用表格?
- 您可以考虑公共(public)假期,不包括在内
- 如果 future 的工作日发生变化,表格可以针对 future 的日期进行更改,而过去的日期保持不变,因此过去事件的工作时间计算仍然是正确的。
- 您甚至可以排除午餐时间、周末休息半天或任何您想要的时间。
关于mysql - SQL 时间戳 - 计算 SLA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627782/