1

我试图计算一个表中只有一个会话的唯一事件的数量。我们有一个名为 Session Tracking 的表,它存储事件编号,还存储该会话的先前状态和当前状态。我想计算只有一个会话的事件数量,并且我还想计算可能有多个会话的事件数量,但前提是以下会话开始于关闭并结束于关闭。基本上不计算在聚合列表中不改变状态的会话。

例子:

Incident ID  Start Status  End Status
1            Open          Closed
1            Closed        Closed
2            Open          Closed
3            Open          Closed
4            Open          Open
4            Open          Closed

我想获得前三个事件,因为事件 2 和 3 都只有一个会话,并且想要计算第一个事件,因为它在它已经关闭之后有一个会话,而第二个会话不计算在内。第四个事件不会被计算在内,因为它有不止一个会话是开放的。

这是我正在使用的声明,并认为可能有问题......

SELECT Incident, COUNT(Incident)
FROM Session_Tracking
WHERE NOT (Prev_Status = 'Closed' AND Current_Status = 'Closed')
GROUP BY Incident
HAVING COUNT(Incident) = 1
4

1 回答 1

2

假设您正在使用支持窗口函数的合理数据库,您可以通过计算事物来做到这一点:

select SUM(case when numInIncident = 1 then 1 else 0 end) as Singletons,
       SUM(case when ClosedClosed = numInIncident - 1 and numInINcident > 1 then 1 else 0
           end) as RestClosed
from (select st.*,
             COUNT(*) over (partition by incident) as numInIncident,
             sum(case when startStatus = 'Closed' and endStatus = 'Closed' then 1 else 0 end) over (partition by incident) as ClosedClosed
      from session_tracking
     ) st

内部子查询计算每个事件的行数,以及当开始和结束状态都“关闭”时的 rof 行数。外部查询应用此逻辑来获取您想要的数字。

于 2013-03-21T16:02:56.993 回答