5

我有一个数据库表,其中包含从应用程序报告的错误日志。

如果发生某些错误,应用程序需要人工干预才能再次激活。

我需要对日志进行排序并确定每对事件之间累积的总时间。

因此,当应用程序在某个时间进入需要干预的 ERROR 状态时,我需要找到应用程序重新启动的下一个错误日志的经过时间。

然后我需要每对事件之间的总经过时间的总和。

该表如下所示:

ErrorID |  ErrorMessage | ErrorDateTime
---------------------------------------------
 20     | ex. msg 1     | 2013-09-01 00:10:10 
 21     | ex. msg 2     | 2013-09-01 00:10:15   
 22     | ex. msg 3     | 2013-09-01 00:10:20 
 23     | ERROR         | 2013-09-01 00:10:25
 24     | ex. msg 4     | 2013-09-01 00:10:30 
 25     | ex. msg 5     | 2013-09-01 00:10:35       
 26     | ex. msg 6     | 2013-09-01 00:10:37   
 27     | App Restarted | 2013-09-01 00:11:30 
 28     | ex. msg 7     | 2013-09-01 00:11:35 
 29     | ex. msg 8     | 2013-09-01 00:11:40   
 30     | ex. msg 9     | 2013-09-01 00:11:43 
 31     | ERROR         | 2013-09-01 00:11:45
 32     | ex. msg 10    | 2013-09-01 00:12:10 
 33     | ex. msg 11    | 2013-09-01 00:12:20       
 34     | ex. msg 12     | 2013-09-01 00:12:22   
 35     | App Restarted | 2013-09-01 00:13:30        

所以基本上我需要找到每个 ERROR 的时间戳和随后的 App Restarted 日志消息之间的差异。

然后得到所有这些持续时间的总和

谁能指出我正确的方向?

4

3 回答 3

3
;WITH x AS
(
  SELECT ErrorID, ErrorMessage, ErrorDateTime,
    rn = ROW_NUMBER() OVER (ORDER BY ErrorDateTime, ErrorID)
  FROM dbo.YourLogTable
  WHERE ErrorMessage IN ('ERROR', 'App Restarted')
)
SELECT
  y.ErrorID, 
  x.ErrorID, 
  [Back_Up] = y.ErrorDateTime, 
  SecondsDown = DATEDIFF(SECOND, y.ErrorDateTime, x.ErrorDateTime)
FROM x
LEFT OUTER JOIN x AS y
ON x.rn = y.rn + 1
WHERE x.ErrorMessage = 'App Restarted';

这为您提供了每个停机时间。我不确定SUM在应用程序的整个生命周期内有什么价值?受限于特定的时间范围?还有什么?但是你可以这样得到:

;WITH x AS
(
  SELECT ErrorID, ErrorMessage, ErrorDateTime,
    rn = ROW_NUMBER() OVER (ORDER BY ErrorDateTime)
  FROM dbo.YourLogTable
  WHERE ErrorMessage IN ('ERROR', 'App Restarted')
)
SELECT
   TotalDowntime = SUM(DATEDIFF(SECOND, y.ErrorDateTime, x.ErrorDateTime))
FROM x
LEFT OUTER JOIN x AS y
ON x.rn = y.rn + 1
WHERE x.ErrorMessage = 'App Restarted';
于 2013-09-11T18:14:13.720 回答
1

以下查询获取每个错误的重新启动时间:

select l.*,
       (select top 1 ErrorDateTime
        from logs l2
        where l2.ErrorId > l.ErrorId and
              l2.ErrorMessage = 'App Restarted'
        order by l2.ErrorId
       ) as RestartTime
from logs l
where l.ErrorMessage = 'ERROR';

要得到总和需要求和时间。这是以秒为单位的总和:

with errors as (
    select l.*,
           (select top 1 ErrorDateTime
            from logs l2
            where l2.ErrorId > l.ErrorId and
                  l2.ErrorMessage = 'App Restarted'
            order by l2.ErrorId
           ) as RestartTime
    from logs l
    where l.ErrorMessage = 'ERROR'
   )
select sum(datediff(second, ErrorDateTime, RestartTime)) as SecondsDown
from errors;
于 2013-09-11T18:10:22.097 回答
0

你也可以试试这个方法...

;WITH x AS
(
  SELECT ErrorID, ErrorMessage, ErrorDateTime,
    rn = ROW_NUMBER() OVER (ORDER BY ErrorDateTime)
  FROM dbo.YourLogTable
  WHERE ErrorMessage IN ('ERROR', 'App Restarted')
)
SELECT
   TotalDowntime = SUM(DATEDIFF(SECOND, y.ErrorDateTime, x.ErrorDateTime))
FROM x
LEFT OUTER JOIN x AS y
ON x.rn = y.rn + 1
WHERE x.ErrorMessage = 'App Restarted';
于 2015-05-26T11:23:23.483 回答