查询以查找连续事件之间的时间差

时间:2016-08-27 16:24:44

标签: sql sql-server-2012

目标:返回显示相同类型的连续事件之间的时间差异的数据集

表格结构:

CREATE TABLE [dbo].[Event](
[EventID] [int] IDENTITY(1,1) NOT NULL,
[JobID] [int] NOT NULL, --FK to Job Table
[LastRun] [datetime] NOT NULL,
[LastRunEnd] [datetime] NULL,
[LastRunResult] [int] NOT NULL, --FK to Result
) ON [PRIMARY]
--Event ID is PK

由于数据来自顺序事件,因此每个作业的LastRun总是会增加。如果在生成数据之前创建条目的进程失败,则LastRunEnd可能为null。
LastRun将始终大于之前的所有LastRunEnd

我试图编写一个TSQL查询,显示每个条目,上一个条目的JobID,LastRun和LastRun。这将给我两者之间的差距,从而检测错过的跑步。

到目前为止我最好的拍摄:

select this.EventId as thisEvent,prev.Eventid as  prevEvent,
this.lastrun as thisRun,
prev.LastRun as prevRun,
datediff(hh,prev.LastRun,this.lastrun) as gap
from Event this
join (select 
     EventID, JobID,LastRun from Event ) prev on prev.jobid = 
     this.jobid and prev.EventID = (Select max(EventID) from Event 
                                    where LastRun < This.LastRun)
where this.LastRun > '2016-08-01' and job.jobid = 57

这似乎适用于它返回的行,但它返回的行数太少。 使用where子句,有15个事件。 我预计会返回14行,但只有3行。 返回的三个看起来像这样:

thisEvent   prevEvent   thisRun          prevRun          gap
----------- ----------- ---------------- ---------------- ----
5172        5239        2016-08-01 16:16 2016-05-31 15:45 1489
5174        5239        2016-08-02 15:45 2016-05-31 15:45 1512
5176        5239        2016-08-03 15:45 2016-05-31 15:45 1536

我期待这样的事情:

thisEvent   prevEvent   thisRun          prevRun          gap
----------- ----------- ---------------- ---------------- ----
5176        5174        2016-08-03 15:45 2016-08-02 15:45 24
5174        5172        2016-08-02 15:45 2016-08-01 16:16 23

显然,所有行都在拾取相同的上一个事件。 我也不知道为什么只返回3行

任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:1)

您可以使用lag分析窗口函数:

select   jobid,
         thisevent,
         prevevent,
         thisrun,
         prevrun,
         datediff(hh, thisrun, prevrun) as gap
from     (select  jobid,
                  eventid thisevent, 
                  lastrun thisrun,
                  lag(eventid) over (partition by jobid order by lastrun) prevevent,
                  lag(lastrun) over (partition by jobid order by lastrun) prevrun
          from    event 
          where   lastrun > '2016-08-01'
              and jobid = 57
         ) base
order by jobid, 
         thisrun

答案 1 :(得分:0)

您也可以使用cte的帮助..

  1. 创建样本数据。

    CREATE TABLE #Event
    ([EventID] [int] NOT NULL,
     [JobID] [int] NOT NULL, --FK to Job Table
     [LastRun] [datetime] NOT NULL
    ) ON [PRIMARY]
    
    GO
    
    INSERT INTO #Event 
    VALUES (5172,1,'2016-08-01 16:16') 
      ,(5174,1,'2016-08-02 15:45') 
      ,(5176,1,'2016-08-03 15:45')
      ,(5239,1,'2016-05-31 15:45') 
    
  2. 使用以下查询获得结果

    With cte_1
         as (select  e.jobid,
                  e.eventid thisevent, 
                  e.lastrun thisrun,
                  lag(e.eventid) over (partition by e.jobid order by e.lastrun) prevevent,
                  lag(e.lastrun) over (partition by e.jobid order by e.lastrun) prevrun
          from    #event e
          where   e.lastrun > '2016-08-01')
    
        select   jobid,
                 thisevent,
                 prevevent,
                 thisrun,
                 prevrun,
                 ABS(datediff(hh, thisrun, prevrun)) as gap
         FROM cte_1 
         ORDER BY jobid, thisrun