Select *
Into #tb
From (
Select 474 as 张数,231527 as 起号,232000 as 止号 Union All
Select 550 , 240451 ,241000 Union All
Select 642 , 241359 ,242000 Union All
Select 526 , 244475 ,245000 Union All
Select 557 , 246444 ,247000 Union All
Select 100 , 247901 ,248000 Union All
Select 2000 , 248001 ,250000 Union All
Select 20000, 250001 ,270000 Union All
Select 50000, 270001 ,320000)as tt
--查询
with t as (Select COALESCE(t2.张数,0) + t1.张数 as 张数,t1.起号,COALESCE(t2.止号,t1.止号) as 止号
From #tb as t1
Left Join #tb as t2
ON t1.止号 = t2.起号 - 1
Left Join #tb as t3
On t2.止号 = t3.起号 - 1
)
Select *
From t as t1
Where Not Exists(
Select *
From t as t2
where t1.止号 = t2.止号
And t1.张数 > t2.张数
)
---结果
474 231527 232000
550 240451 241000
642 241359 242000
526 244475 245000
557 246444 247000
2100 247901 250000
22000 248001 270000
50000 270001 320000