• Thanks for all the inputs, they pointed me into the right direction:

    I've managed to work it out using this CTE Expression:

    ;WITH cte

    AS (

    SELECT distinct *

    ,CAST(SUBSTRING(SAMPLE_ID, 4, LEN(SAMPLE_ID)- 3) AS INT) - ROW_NUMBER() OVER (

    ORDER BY SAMPLE_ID

    ) AS Grp

    FROM (SELECT DESPATCH_ID,SAMPLE_ID

    FROM [dbo].[SAMPLE_TABLE]

    )A

    WHERE DESPATCH_ID = 'MIR00831'

    )

    SELECT distinct DESPATCH_ID

    ,min(SAMPLE_ID) AS FirstSample

    ,max(SAMPLE_ID) AS LastSamp

    ,count(*) AS Quantity

    FROM cte

    GROUP BY DESPATCH_ID

    ,Grp