select min(a.billno)+'--'+a.c,sum(a.billamt)
from
(
select a.billno,a.billamt,min(c.billno) as c from test a,test c where right(c.billno,5)+1 not in (select right(b.billno,5) from test b)
and a.billno<=c.billno group by a.billno,a.billamt
) as a
group by a.c