SELECT id,Name FROM TeachSite GROUP BY id select top 1000 1 as [type],SchoolRollID,ChargeableAmount into #temp from ChargeRecord where bflag=1 select * from #temp select 2 as [type], SchoolRollID,SUM(ChargeableAmount) as xj into #tempxj from #temp group by SchoolRollID select 0 as [typq], null as SchoolRollID ,SUM(xj) as hj into #temphj from #tempxj select * from #temp insert into #temp select * from #tempxj insert into #temp select * from #temphj select * from #temp order by SchoolRollID,[type] desc