简体   繁体   中英

SQL Server find consecutive failure records

I've look at so many other questions and nothing quite fits my question or gets me the answer I need, maybe I'm just slow today :(

DECLARE @t TABLE (
    [InstructionId] INT,
    [InstructionDetailId] INT,
    [Sequence] INT,
    [Status] INT
 ) 
 INSERT INTO @t SELECT 222,111,1, 2
 INSERT INTO @t SELECT 222,112,2,2
 INSERT INTO @t SELECT 222,113,3,4
 INSERT INTO @t SELECT 222,114,4,4
 INSERT INTO @t SELECT 222,115,5,2
 INSERT INTO @t SELECT 222,116,6,4
 INSERT INTO @t SELECT 222,117,7,2
 INSERT INTO @t SELECT 222,118,8,4
 INSERT INTO @t SELECT 222,119,9,4
 INSERT INTO @t SELECT 222,120,10,2
 INSERT INTO @t SELECT 222,121,11,2

I need to find for which InstructionDetailId's there are consecutive failures (Status = 4) by using the [Sequence] field for checking the order to determine if they are consecutive. So for the above InstructionDetailId 113 and 114 would be consecutive failures as their [Sequence] is 3 & 4, same for InstructionDetailId 118 and 119 would be consecutive failures. I've tried so many row number variation and cte's and I can't quite crack it :( This is for SQL Server 2008 R2 by the way.

Expected output:

InstructionId   InstructionDetailId Sequence    Status
222                            113       3           4
222                            114       4           4
222                            118       8           4
222                            119       9           4

Thanx all!

Perhaps the simplest method is to use lag() and lead() :

select t.*
from (select t.*,
             lag(t.status) over (partition by t.InstructionId order by t.sequence) as prev_status,
             lead(t.status) over (partition by t.InstructionId order by t.sequence) as next_status
      from @t t
     ) t
where status = prev_status or status = next_status;

You can use APPLY :

select t.*
from @t t outer apply
     ( select top (1) t1.*
       from @t t1
       where t1.InstructionId = t.InstructionId and
             t1.Sequence < t.Sequence
       order by t1.Sequence desc
     ) t1 outer apply
     ( select top (1) t2.*
       from @t t2
       where t2.InstructionId = t.InstructionId and
             t2.Sequence > t.Sequence
       order by t2.Sequence 
     ) t2
where t.status = 4 and (t.status = t1.status or t.status = t2.status);

Could you do something like...

    select t1.InstructionID,
           t1.InstructionDetailID,
           t1.Sequence,
           t1.Status,
      from @t t1
     inner join @t t2 on t1.InstructionID = t2.InstructionID
                     and t1.Sequence = (t2.Sequence - 1)
                     and t1.Status = t2.Status
                     and t1.Status = 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM