1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ;with RchFin as(
select t1.ID FirstIdInclusive, t1.ID LastIdInclusive , t1.SendState, t1.AckState
from #tc9 t1
left join #tc9 t2
on t1.id = t2.id + 1
where t1.SendState <> isnull(t2.SendState, -1)
or t1.AckState <> t2.AckState
or t2.AckState is null
union all
select t1.FirstIdInclusive, t2.id, t1.SendState, t1.AckState
from rchfin t1
inner join #tc9 t2
on t1.LastIdInclusive = t2.id - 1
where t1.SendState = t2.SendState
and t1.AckState = t2.AckState
)
select FirstIdInclusive, MAX(LastIdInclusive) LastIdInclusive, SendState, AckState
from rchfin
group by FirstIdInclusive, SendState, AckState
order by 1 |