update a set 数量=(select isnull(sum(数量),0)+isnull(sum(变量),0) from tb where 产品=a.产品 and id<a.id)
from tb a
where exists(
select * from tb where 产品=a.产品 and id<a.id)

解决方案 »

  1.   

    --如果加入时间与id顺序可能不一样,则改为:update a set 数量=(select isnull(sum(数量),0)+isnull(sum(变量),0) from tb where 产品=a.产品 and( 加入时间>a.加入时间 or 加入时间=a.加入时间 and id<a.id))
    from tb a
    where exists(
    select * from tb where 产品=a.产品 and( 加入时间>a.加入时间 or 加入时间=a.加入时间 and id<a.id))
      

  2.   

    Update a set a.数量=((Select isNULL(Sum(数量),0)
                          from tbl where 产品名=a.产品名
                          and 加入时间<a.加入时间)+a.加入数量)
     from tbl as a
      

  3.   

    我以让UPDATE按时间顺序来更新吗???