从带有重复开始/停止记录的开始/停止事件的时间戳列中查看会话

Posted

技术标签:

【中文标题】从带有重复开始/停止记录的开始/停止事件的时间戳列中查看会话【英文标题】:Review sessions from timestamp column with start/stop event with duplicate start/stop records 【发布时间】:2021-04-19 10:00:49 【问题描述】:

我有以下案例变更记录:

id case_id state time_created
1 100 REVIEW_NEEDED 2021-03-30 15:11:58.015907000
2 100 REVIEW_NEEDED 2021-04-01 13:08:17.945926000
3 100 REVIEW 2021-04-07 06:20:48.873865000
4 100 WAITING 2021-04-07 06:32:47.159664000
5 100 REVIEW_NEEDED 2021-04-09 06:32:51.132127000
6 100 REVIEW 2021-04-12 04:39:36.426467000
7 100 REVIEW 2021-04-12 04:40:36.000000000
8 100 CLOSED 2021-04-12 04:40:43.133736000
9 101 REVIEW_NEEDED 2021-03-30 20:37:58.015907000
10 101 REVIEW 2021-04-04 13:08:17.945926000
11 101 CLOSED 2021-04-06 06:20:48.873865000
12 101 CLOSED 2021-04-06 06:20:50.000000000

我想报告以下这些会话:

open_id close_id case_id waiting_time_start handling_time_start handling_time_end
1 4 100 2021-03-30 15:11:58.015907000 2021-04-07 06:20:48.873865000 2021-04-07 06:32:47.159664000
5 8 100 2021-04-09 06:32:51.132127000 2021-04-12 04:39:36.426467000 2021-04-12 04:40:43.133736000
9 11 101 2021-03-30 20:37:58.015907000 2021-04-04 13:08:17.945926000 2021-04-06 06:20:48.873865000

Waiting_time_start:当状态 = REVIEW_NEEDED

Handling_time_start:当 state = REVIEW

Handling_time_end:当状态 = WAITING 或 CLOSED

我目前的解决方案是对每个案例的 Waiting_time_startHandling_time_startHandling_time_end 进行排名,然后将这些事件加入排名,但这是由于存在重复记录,因此并不完美,因此每个案例的开始/停止事件的数量可能会有所不同。

非常感谢您的任何想法!

【问题讨论】:

【参考方案1】:

这是相当复杂的。首先添加基于“等待”和“关闭”计数的分组——但仅当它们更改值时:

select t.*,
       sum(case when (state <> next_state or next_state is null) and
                     state in ('WAITING', 'CLOSED')
                then 1 else 0
           end) over (partition by caseid order by time_created desc) as grouping
from (select t.*,
             lead(state) over (partition by caseid order by time_created) as next_state
      from t
     ) t

然后,你可以聚合:

with cte as (
      select t.*,
             sum(case when (state <> next_state or next_state is null) and
                           state in ('WAITING', 'CLOSED')
                      then 1 else 0
                 end) over (partition by caseid order by time_created desc) as grouping
      from (select t.*,
                   lead(state) over (partition by caseid order by time_created) as next_state
            from t
           ) t
     )
select caseid, min(id), max(id),
       min(case when status = 'REVIEW_NEEDED' then time_created end),
       min(case when status = 'REVIEW' then time_created end),
       max(time_created)
from cte
group by grouping, caseid;

【讨论】:

谢谢,效果很好。 (虽然这两个代码 sn-ps 应该交换)

以上是关于从带有重复开始/停止记录的开始/停止事件的时间戳列中查看会话的主要内容,如果未能解决你的问题,请参考以下文章

开始拖拽一个带有 draggable 属性的 DOM 元素后,我们可以停止拖拽并退出拖拽操作吗?

使用 jquery/javascript 停止带有动画的函数

Agora Web SDK NG - 存在停止屏幕共享事件?

Openhab2停止处理来自ZWave ZW096 Smart Switch 6的事件

使用 Buzz.js 音频从我停止的最后一个位置开始,而不是从头开始。 (ipad1)

Android应用开始和结束事件