日期:2014-05-18 浏览次数:20511 次
改改 SELECT c.* FROM #T AS a INNER JOIN #T AS b ON a.command=b.command AND a.dt<b.dt AND b.dt=(SELECT min(dt) FROM #T WHERE command=a.command AND dt>a.dt) INNER JOIN #T AS c ON c.dt >= a.dt AND c.dt<b.dt WHERE a.command='start' AND EXISTS(SELECT 1 FROM #T WHERE [dt] BETWEEN a.dt AND b.dt HAVING count(CASE WHEN [command] IN(N'complete',N'failed') THEN 1 END)=1 AND COUNT(CASE WHEN [command]=N'complete' THEN 1 end)=1)
------解决方案--------------------
应为>10
;with c1 as( select *,rn=ROW_NUMBER()over(order by dt) from tb ),c2 as( select *,dt as flg1,0 as flg2 from c1 where command='start' union all select b.*,a.flg1,(case when b.command='complete' then a.flg2+10 when b.command='failed' then 20 else a.flg2 end) from c2 a inner join c1 b on b.rn=a.rn+1 and b.command<>'start' ) select dt,command,ci,col,col2 from c2 a where exists(select 1 from c2 where flg1=a.flg1 and flg2=10) and not exists(select 1 from c2 where flg1=a.flg1 and flg2>10)