if OBJECT_ID('tempdb..#linshi') is not null drop table #linshi SELECT id=identity(int,1,1), Cost into #linshi FROM XXXXOrder By Cost Desc select * ,count=( select sum(Cost) from linshi i where i.id<o.id ) from #linshi o