我详细介绍了构建的内容以及构建失败的内容。我希望能够测量两次失败之间的时间(意味着两次失败之间至少有一次传递)。到目前为止,我有示例数据和示例查询here。
我希望我的查询输出,因为这个数据只有2行
FailedDt | SecondFailedDt
-------------------------------------------------
1/2/2010 12:00:00 AM | 1/4/2010 12:00:00 AM
1/6/2010 12:00:00 AM | 1/10/2010 12:00:00 AM
如何修改查询以获得我想要的输出?
答案 0 :(得分:1)
这可能比你的方法容易得多:
只需将其粘贴到一个空的查询窗口中,然后执行......根据您的需要调整
declare @rsBuildDetails table(dt datetime, build varchar(255), val varchar(255));
insert into @rsBuildDetails (dt, build, val) values
('20100101', '1', 'pass')
,('20100102', '2', 'fail')
,('20100103', '3', 'pass')
,('20100104', '4', 'fail')
,('20100105', '5', 'fail')
,('20100106', '6', 'fail')
,('20100107', '7', 'pass')
,('20100108', '8', 'pass')
,('20100109', '9', 'pass')
,('20100110', '10', 'fail');
with passed as
(
select *
from @rsBuildDetails
where val='pass'
)
select distinct preFail.dt AS FailedDt
,postFail.dt AS SecondFailedDt
from passed
outer apply(
select top 1 pre.*
from @rsBuildDetails as pre
where pre.dt<passed.dt
and pre.val='fail'
order by pre.dt desc
) as preFail
outer apply(
select top 1 post.*
from @rsBuildDetails as post
where post.dt>passed.dt
and post.val='fail'
order by post.dt asc
) as postFail
where preFail.dt IS NOT NULL and postFail.dt IS NOT NULL
结果
FailedDt SecondFailedDt
2010-01-02 00:00:00.000 2010-01-04 00:00:00.000
2010-01-06 00:00:00.000 2010-01-10 00:00:00.000