不完整的答案,因为你的问题不够复杂。
只是快速简单的示例如何实现这一点。
一些样本数据
DECLARE @Table table
( dcreporttime datetime, event_name varchar(55))
insert INTO @Table
SELECT '2013-04-09 13:34:01', 'RUNNING'
union
SELECT '2013-04-09 13:34:19', 'STOPPED'
union
SELECT '2013-04-09 13:34:29', 'RUNNING'
union
SELECT '2013-04-09 13:34:39', 'RUNNING'
union
SELECT '2013-04-09 13:35:19', 'STOPPED'
union
SELECT '2013-04-09 13:35:29', 'RUNNING'
union
SELECT '2013-04-09 13:35:39', 'RUNNING'
union
SELECT '2013-04-09 13:36:19', 'RUNNING'
union
SELECT '2013-04-09 13:37:19', 'STOPPED'
union
SELECT '2013-04-09 13:37:39', 'RUNNING'
和查询。它使用 CTE(公用表表达式)和排名函数:
;with cte as
(
SELECT RANK() OVER(ORDER by dcreporttime) as RankN, *
FROM @Table
)
select a.dcreporttime, a.event_name, a.dcreporttime, b.dcreporttime, DATEDIFF(ms, a.dcreporttime, b.dcreporttime) as runtime
from cte a
-- shift by Rank to compare with previous row
join cte b on a.RankN = b.RankN + 1
这只是您从上一个事件开始获得时间。我希望这可以为您指出解决方案。如果没有,请更具体地说明您的需要:)