【问题标题】:SQL Timestamps - calculating SLASQL 时间戳 - 计算 SLA
【发布时间】:2012-11-29 14:19:39
【问题描述】:

我有几张工单,想提供一份基于 PHP 服务水平协议的报告。

新工单的状态可能为“新”并带有特定时间戳,一段时间后状态为“已关闭”且带有特定时间戳。

应该计算这两个时间戳之间的差异,但是只有从周一到周五(上午 8 点到下午 4.30)的工作时间应该是相关的。因此,如果工单在周五下午 4 点创建并在下周一上午 9 点关闭,则经过的时间应为 1.5 小时。

有人知道如何从数据库中获取异常结果吗?

数据库是mysql数据库,bugtracking系统是开源的BTS Mantis Bugtracker。

数据库表的一个非常简单的部分:

表格错误:

ID |状态 | date_created (TIMESTAMP) | last_modified (TIMESTAMP)

表格历史

ID | bug_id(参考错误)|状态_旧 |新状态 | date_modified (TIMESTAMP)

我在 PHP 中的查询: 获取在特定时间范围内设置为状态 30 的所有错误。 对于最高 SLA 级别,此帧介于 0 到 2 小时之间。 查询工作正常 - 但时间范围不关心工作时间......

选择 bug.id 作为 ID FROM mantis_bug_table 作为 bug,mantis_bug_history_table 作为历史 其中 history.new_status = 30 和 history.bug_id = bug.id AND (history.date_modified - bug.date_submitted) '{$timeframe_start}'";

【问题讨论】:

  • 对我来说似乎很清楚。计算两个瞬间之间经过的工作时间。
  • @Ben - 然后告诉我,尝试过什么?正在使用哪个数据库和版本?表是如何定义的?
  • @t-clausen.dk,它可以“以目前的形式得到合理的回答”,因此它不符合“不是一个真正的问题”的条件。跨度>
  • 数据库为mySQL数据库,系统为Mantis BT票务系统。结构非常简单,作为示例,您可以想象如下设计:表错误:ID、date_created (TIMESTAMP)、date_modified (TIMESTAMP) - 以及表历史记录:ID、bug_id、date_modified (TIMESTAMP)、字段(例如状态),旧值,新值

标签: mysql sql timestamp


【解决方案1】:

您需要一个显示所有工作时间段的表格,例如:

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   

为什么要使用表格?

  • 您可以考虑公共假期,但不包括它们
  • 如果工作日在未来发生变化,表格可以针对未来的日期进行更改,而不影响过去的日期,因此过去事件的工作时间计算仍然正确。
  • 您甚至可以排除午餐时间、周末休息半天或其他任何您想要的时间。

【讨论】:

  • 我不确定我是否真的理解这个概念。我用 WorkingPeriods(开始和结束时间)创建了一个表 - 但该函数实际上做了什么?
  • @t-clausen.dk,感谢您的修复!
  • johaaachim 此解决方案计算给定时间跨度的秒数(例如 @dtStart 到 @dtEnd),该时间跨度在您预定义的工作周期表内,您需要在执行此函数之前填充该表。干得好,@Ben!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 2015-01-26
  • 2015-08-06
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多