• DesNorton - Wednesday, January 31, 2018 10:31 PM

    Try changing your JOIN to a LEFT JOIN

    LEFT JOIN alone will not cut it. You'll also have to move the WHERE condition to ON:

    CREATE TABLE #StatusHistory (
     UniqueID varchar(30) ,
     Sequence int,
     BeforeStatus varchar(10),
     AfterStatus varchar(10),
     HistoryStatusDate date,
     PRIMARY KEY(UniqueID, Sequence)
     )

    INSERT INTO #StatusHistory VALUES('08-40089445',1,'','BNT','20171017')
    INSERT INTO #StatusHistory VALUES('08-40089445',2,'BNT','VFD','20180118')
    INSERT INTO #StatusHistory VALUES('08-40089445',3,'VFD','NST','20180123')
    INSERT INTO #StatusHistory VALUES('08-40089445',4,'NST','SLD','20180123')

    SELECT * FRom
    (SELECT s.*,h.HistoryStatusDate statusEnddate
    FROM [#StatusHistory] S
    LEFT JOIN [#StatusHistory] h
    ON s.UniqueID=h.UniqueID
    AND  s.Sequence=h.Sequence-1
    ) s
    --Still trying to find the 7th record for this example
    WHERE s.[UniqueID]='08-40089445'
    ORDER BY s.UniqueID, s.Sequence
    Result:
    UniqueID                       Sequence    BeforeStatus AfterStatus HistoryStatusDate statusEnddate
    ------------------------------ ----------- ------------ ----------- ----------------- -------------
    08-40089445                    1                        BNT         2017-10-17        2018-01-18
    08-40089445                    2           BNT          VFD         2018-01-18        2018-01-23
    08-40089445                    3           VFD          NST         2018-01-23        2018-01-23
    08-40089445                    4           NST          SLD         2018-01-23        NULL

    (4 rows affected)

    However, I'd be reluctant to rely on the Sequence column being an unbroken sequence (the -1 condition). So I'd likely utilize an OUTER APPLY in stead of the LEFT JOIN:
    SELECT * FRom
    (SELECT s.*,h.HistoryStatusDate statusEnddate
    FROM [#StatusHistory] S
    OUTER APPLY (
     SELECT TOP(1) HistoryStatusDate
     FROM [#StatusHistory] h
     WHERE s.UniqueID=h.UniqueID
     AND h.Sequence > s.Sequence
     ORDER BY h.Sequence ASC
     ) h
    ) s
    --Still trying to find the 7th record for this example
    WHERE s.[UniqueID]='08-40089445'
    ORDER BY s.UniqueID, s.Sequence

    If we put in a 5 instead of 4 in the last row (thereby creating a gap in the Sequence column sequence), we can see the difference:
    UniqueID                       Sequence    BeforeStatus AfterStatus HistoryStatusDate statusEnddate
    ------------------------------ ----------- ------------ ----------- ----------------- -------------
    08-40089445                    1                        BNT         2017-10-17        2018-01-18
    08-40089445                    2           BNT          VFD         2018-01-18        2018-01-23
    08-40089445                    3           VFD          NST         2018-01-23        NULL
    08-40089445                    5           NST          SLD         2018-01-23        NULL

    (4 rows affected)

    UniqueID                       Sequence    BeforeStatus AfterStatus HistoryStatusDate statusEnddate
    ------------------------------ ----------- ------------ ----------- ----------------- -------------
    08-40089445                    1                        BNT         2017-10-17        2018-01-18
    08-40089445                    2           BNT          VFD         2018-01-18        2018-01-23
    08-40089445                    3           VFD          NST         2018-01-23        2018-01-23
    08-40089445                    5           NST          SLD         2018-01-23        NULL

    (4 rows affected)

    Only the last one can cope.

    Anyway, both methods will need an index on UniqueID, Sequence in order to perform adequately if more than a few rows are to be processed.