select  identity(int,1,1) id,* into #临时表 from T_tempselect *,(select sum(case when style>0 then quantity else -quantity end) from #临时表 where fitem=tem.fitem and id<=tem.id ) totalq from #临时表 tem

解决方案 »

  1.   

    如果要得到新表:
    select  identity(int,1,1) id,* into #临时表 from T_tempselect *,(select sum(case when style>0 then quantity else -quantity end) into T_totalq from #临时表 where fitem=tem.fitem and id<=tem.id ) totalq from #临时表 temdrop table #临时表
      

  2.   

    小改一下.:)
    select  identity(int,1,1) id,* into #临时表 from T_tempselect *,(select sum(style*quantity) from #临时表 where fitem=tem.fitem and id<=tem.id ) totalq from #临时表 tem