Assuming cardSerial is an INT or other numeric data type ...
WITH sequenced AS ( SELECT ROW_NUMBER() OVER (PARTITION BY Denomination ORDER BY cardSerial) AS sequence_id, * FROM Cards ) SELECT min(cardSerial), max(cardSerial), count(*), Denomination FROM Sequenced GROUP BY Denomination, cardSerial - sequence_id ORDER BY Denomination, cardSerial - sequence_id
If it is not a numeric data type, convert it into a query into a suitable large numeric data type to allow cardSerial - sequence_id to work.
As the calculation of cardSerial - sequence_id gives something meaningful to the group ...
214026002 - 01 = 214026001 214026003 - 02 = 214026001 214026004 - 03 = 214026001 214026005 - 04 = 214026001 214026006 - 05 = 214026001 214026100 - 06 = 214026094 214026101 - 07 = 214026094 214026102 - 08 = 214026094 214026103 - 09 = 214026094 214026104 - 10 = 214026094 214026200 - 11 = 214026189 214026201 - 12 = 214026189 214026202 - 13 = 214026189 214026203 - 14 = 214026189 214026204 - 15 = 214026189 214026205 - 16 = 214026189 214026206 - 17 = 214026189
source share