(select [2],1 as beginId,min([3])-1 as endId from [c] group by [2] having min([3])<>1) union
(select a.[2], (select max([3])+1
from [c]
where [3]<a.[3] and [2]=a.[2]) as beginId,
([3]-1) as endId
from [c] a
where
a.[3]>(select max([3])+1 from [c] where [3]<a.[3] and [2]=a.[2]))