select * , id = identity(int,1,1) into tmp from tb
select 序号,金额 from
(
select id1 = (id - 1) / 3 , 序号 = cast(序号 as varchar),金额 from tmp
union all
select id1 = (id - 1) / 3 , 序号 = '合计' , 金额 = sum(金额) from tmp group by (id - 1) / 3
) t
order by id1 , case 序号 when '合计' then 2 else 1 end