按class类型归类求解total=a-b-c, 如  
class a   b   c  total  
a1    10  5   5  
a2    50  10  30  
a1    10  10  10  
a1    30  5   5  
a2    10  5   5  
a3    100 0   10  
a2    10  10  10  
a3    10  5   0 结果是怎样?

解决方案 »

  1.   

    declare @s int,@a varchar(10)
    alter table tb add id int identity 
    update a set total = (select sum(a-b-c) from tb where a.id >=id) from tb a
    alter table tb drop column id 
      

  2.   

    谢谢楼上的JiangHongTao ,我也写到类似的代码,但是我是按照class分类计算的,不是一起从头算到尾
    比如
    class a   b   c  total   
    a1    10  5   5   0
    a2    50  10  30  10 
    a1    10  10  10  -10  
    a1    30  5   5    10
      

  3.   

    declare @s int,@a varchar(10)
    alter table tb add id int identity 
    update a set total = (select sum(a-b-c) from tb where a.class = class and a.id >=id) from tb a
    alter table tb drop column id 
      

  4.   

    JiangHongTao ,谢谢,可是我写得代码和你上面那句查不多了。有这样一个问题,数据库计算的时候会出现更新A类的时候计算了该类值,但是B类也跟着计算。再更新B类值,A类值又错。
      

  5.   

    SQL会将原表复制一份放在一个临时空间里面,不会出现你说的情况。