1

我需要报告每个支持组的事件处于待处理/已分配/进行中状态的持续时间。数据存储在两个表中;ASSIGNMENT_LOG 保存支持组之间分配转移的历史记录,以及 STATUS_LOG 保存事件状态更改的历史记录。

附件是一个事件的样本数据。请查看并建议如何进行。

WITH slog AS (
select '000000000266702' REQUEST_ID, 'INC000000128540' INCIDENT_NUMBER, 'Assigned' INCIDENTSTATUSTEXT, 1431348757 TIMESTAMPSTART,'11.5.2015 15:52:37' STARTTEXT, 1432033832 TIMESTAMPEND,'19.5.2015 14:10:32' ENDTEXT from dual
union
select '000000000268200','INC000000128540','In Progress',1432033832,'19.5.2015 14:10:32',1432034915,'19.5.2015 14:28:35' from dual
union
select '000000000268205','INC000000128540','Pending',1432034915,'19.5.2015 14:28:35',1432034927,'19.5.2015 14:28:47' from dual
union
select '000000000268206','INC000000128540','Assigned',1432034927,'19.5.2015 14:28:47',1432034970,'19.5.2015 14:29:30' from dual
union 
select '000000000268207','INC000000128540','Pending',1432034970,'19.5.2015 14:29:30',1432034988,'19.5.2015 14:29:48' from dual
union
select '000000000268208','INC000000128540','In Progress',1432034988,'19.5.2015 14:29:48',null,null from dual
), 
alog AS(
select 'INC000000128540' INCIDENT_NUMBER, '000000000103677' REQUEST_ID, 'Helpdesk' ASSIGNED_GROUP, 1431348757 TIMESTAMPSTART, 1431434286 TIMESTAMPEND, '11.5.2015 15:52:37' STARTTEXT,'12.5.2015 15:38:06' ENDTEXT from dual
union
select 'INC000000128540','000000000103816','L1',1431434286,1432033740,'12.5.2015 15:38:06','19.5.2015 14:09:00' from dual
union
select 'INC000000128540','000000000104352','L2',1432033740,1432033777,'19.5.2015 14:09:00','19.5.2015 14:09:37' from dual
union
select 'INC000000128540','000000000104353','L1',1432033777,1432034927,'19.5.2015 14:09:37','19.5.2015 14:28:47' from dual
union
select 'INC000000128540','000000000104354','L2',1432034927,1432034980,'19.5.2015 14:28:47','19.5.2015 14:29:40' from dual
union
select 'INC000000128540','000000000104355','L1',1432034980,null,'19.5.2015 14:29:40',null from dual
)
SELECT 
alog.*, slog.*
from alog, slog
where alog.incident_number = slog.incident_number
order by alog.request_id, slog.request_id
4

1 回答 1

0

我认为您正在尝试确定两个表中时间戳范围之间的重叠。然后这样做,计算每个重叠时段的持续时间。这可能会让你开始:

select a.incident_number,
  a.assigned_group,
  s.incidentstatustext,
  greatest(s.timestampstart, a.timestampstart) as overlapstart,
  least(nvl(s.timestampend,
      86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')),
    nvl(a.timestampend,
      86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')))
    as overlapend,
  least(nvl(s.timestampend,
      86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')),
    nvl(a.timestampend,
      86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')))
    - greatest(s.timestampstart, a.timestampstart) as duration,
  -- just to see where values are coming from
  a.timestampstart as a_start,
  a.timestampend as a_end,
  s.timestampstart as s_start,
  s.timestampend as s_end
from alog a
join slog s on s.incident_number = a.incident_number
and (a.timestampend is null or s.timestampstart <= a.timestampend)
and (s.timestampend is null or s.timestampend >= a.timestampstart)
order by incident_number, overlapstart;

您的样本数据将获得:

INCIDENT_NUMBER ASSIGNED INCIDENTSTA   OVERLAPSTART     OVERLAPEND       DURATION        A_START          A_END        S_START          S_END
--------------- -------- ----------- -------------- -------------- -------------- -------------- -------------- -------------- --------------
INC000000128540 Helpdesk Assigned        1431348757     1431434286          85529     1431348757     1431434286     1431348757     1432033832
INC000000128540 L1       Assigned        1431434286     1432033740         599454     1431434286     1432033740     1431348757     1432033832
INC000000128540 L2       Assigned        1432033740     1432033777             37     1432033740     1432033777     1431348757     1432033832
INC000000128540 L1       Assigned        1432033777     1432033832             55     1432033777     1432034927     1431348757     1432033832
INC000000128540 L1       In Progress     1432033832     1432034915           1083     1432033777     1432034927     1432033832     1432034915
INC000000128540 L1       Pending         1432034915     1432034927             12     1432033777     1432034927     1432034915     1432034927
INC000000128540 L2       Pending         1432034927     1432034927              0     1432034927     1432034980     1432034915     1432034927
INC000000128540 L1       Assigned        1432034927     1432034927              0     1432033777     1432034927     1432034927     1432034970
INC000000128540 L2       Assigned        1432034927     1432034970             43     1432034927     1432034980     1432034927     1432034970
INC000000128540 L2       Pending         1432034970     1432034980             10     1432034927     1432034980     1432034970     1432034988
INC000000128540 L1       Pending         1432034980     1432034988              8     1432034980                    1432034970     1432034988
INC000000128540 L1       In Progress     1432034988     1432205373         170385     1432034980                    1432034988               

我假设您希望分别查看每个重叠时间,而不是按分配的组和状态求和。我还使用(希望是 UTC)纪元日期而不是文本版本来简化时区调整——这是为开放式记录提供标称结束日期所必需的。

在状态和分配的组同时发生变化的情况下,您获得的组合比您真正想要的要多,而冗余组合的持续时间为零,因此您可以消除这些组合。使用子查询更容易做到这一点,无论如何这简化了整个事情:

select incident_number,
  assigned_group,
  incidentstatustext,
  overlapstart,
  overlapend,
  overlapend - overlapstart as duration
from (
  select a.incident_number,
    a.assigned_group,
    s.incidentstatustext,
    greatest(s.timestampstart, a.timestampstart) as overlapstart,
    least(nvl(s.timestampend,
        86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')),
      nvl(a.timestampend,
        86400*(cast(sys_extract_utc(systimestamp) as date) - date '1970-01-01')))
        as overlapend
  from alog a
  join slog s on s.incident_number = a.incident_number
  and (a.timestampend is null or s.timestampstart <= a.timestampend)
  and (s.timestampend is null or s.timestampend >= a.timestampstart)
)
where overlapend != overlapstart -- get rid of zeros
order by incident_number, overlapstart;

对于非零持续时间,它得到相同的结果:

INCIDENT_NUMBER ASSIGNED INCIDENTSTA   OVERLAPSTART     OVERLAPEND       DURATION
--------------- -------- ----------- -------------- -------------- --------------
INC000000128540 Helpdesk Assigned        1431348757     1431434286          85529
INC000000128540 L1       Assigned        1431434286     1432033740         599454
INC000000128540 L2       Assigned        1432033740     1432033777             37
INC000000128540 L1       Assigned        1432033777     1432033832             55
INC000000128540 L1       In Progress     1432033832     1432034915           1083
INC000000128540 L1       Pending         1432034915     1432034927             12
INC000000128540 L2       Assigned        1432034927     1432034970             43
INC000000128540 L2       Pending         1432034970     1432034980             10
INC000000128540 L1       Pending         1432034980     1432034988              8
INC000000128540 L1       In Progress     1432034988     1432205374         170386

持续时间以秒为单位,您可以将其转换为您想要报告的任何格式。

于 2015-05-20T15:23:08.370 回答