• My apologies if I was out of line earlier, somehow I get kind of agitated when requirements focus more on HOW than WHAT, especially when it's incorrect.

    The problem itself is elementary, sequential self-join or Lead/Lag on 2012 and later, hence the question about the Version/Edition.

    😎

    USE tempdb;

    GO

    create table #table1(steps int)

    insert into #table1(steps)values(1)

    insert into #table1(steps)values(2)

    insert into #table1(steps)values(3)

    insert into #table1(steps)values(4)

    insert into #table1(steps)values(5)

    insert into #table1(steps)values(6)

    insert into #table1(steps)values(7)

    insert into #table1(steps)values(9)

    insert into #table1(steps)values(10)

    insert into #table1(steps)values(17)

    insert into #table1(steps)values(31)

    ;WITH BASE_DATA AS

    (

    select

    ROW_NUMBER() OVER (ORDER BY T.steps) AS T_RID

    ,T.steps

    from #table1 T

    )

    SELECT

    B_FROM.steps AS stepFrom

    ,B_TO.steps AS stepTo

    FROM BASE_DATA B_FROM

    INNER JOIN BASE_DATA B_TO

    ON B_FROM.T_RID = B_TO.T_RID - 1

    DROP TABLE #table1;

    /* Window Function 2012 */

    SELECT

    X.stepFrom

    ,X.stepTo

    FROM

    (

    SELECT

    steps AS stepFrom

    ,LEAD(steps,1) OVER (ORDER BY steps) AS stepTo

    FROM #table1

    ) AS X

    WHERE X.stepTo IS NOT NULL;

    Results

    stepFrom stepTo

    ----------- -----------

    1 2

    2 3

    3 4

    4 5

    5 6

    6 7

    7 9

    9 10

    10 17

    17 31

    Edit: forgot the Lead version