• There are several options for these problems called gaps and islands. You could look for more information, but here's a good article: https://www.simple-talk.com/sql/t-sql-programming/the-sql-of-gaps-and-islands-in-sequences/

    Here's an option:

    WITH CTE AS(

    SELECT STOCKID,

    WMNUMBER,

    WMNUMBER - ROW_NUMBER() OVER( PARTITION BY STOCKID ORDER BY WMNUMBER) grouper

    FROM #TEMP_WK_STOCK

    )

    SELECT STOCKID,

    MIN( WMNUMBER) AS MIN_WMNUMBER,

    MAX( WMNUMBER) AS MAX_WMNUMBER

    FROM CTE

    GROUP BY STOCKID, grouper

    ORDER BY STOCKID, MIN_WMNUMBER;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2