declare @t table(tr varchar(8),num INT)
insert @t
select
substring(serie,1,8) as tr,
substring(serie,9,12) as num
from STOCK
select * from @t
select tr, min(num) numMin, max(num) numMax from
(
select
tr, num,
num -(select count(distinct num) from @t as tt where (tt.tr=@t.tr and tt.num < @t.num) ) e
from @t
) z
group by tr, e |