Update theA set sum1=theB.aa,sum2=theB.bb
 from (Select sum(the1) aa,sum(the2) bb,id from theA  group by id) theB
where theB.id=thea.id

解决方案 »

  1.   

    可以
    但应改为 UPDATE QQ SET 。。
      

  2.   

    to caiyunxia(monkey):
    Update qq set sum1=theB.aa,sum2=theB.bb from theA qq,(Select sum(the1) aa,sum(the2) bb from theA where id<=qq.id) theB
    仍然错误 ,列前缀 'qq' 与查询中所用的表名或别名不匹配。
    你的第一个回答也不对。
      

  3.   

    是累加的。Select sum(the1) aa,sum(the2) from theA where id<=qq.id)
      

  4.   

    我原来是这么实现的
    Update theA set sum1=(select sum(the1) from theA where id<qq.id),sum2=(select sum(the2) from theA where id<qq.id) from theA qq
    但对每一个字段都进行一个查询我觉得效率不高。
      

  5.   

    呵,这个挺有意思的,我想明白了一点,就是在from后面的表中的数据是静态的。是不能随每次查询而改变的,对不对?有想法的上来交流一下。
      

  6.   

    能写出比
    Update theA set sum1=(select sum(the1) from theA where id<qq.id),sum2=(select sum(the2) from theA where id<qq.id) from theA qq
    效率高的我会别开贴给100分。最好能改正我提问时的错误。
      

  7.   

    Update qq set sum1=(select sum(the1) from theA where id<qq.id),sum2=(select sum(the2) from theA where id<qq.id) from theA qq
      

  8.   

    Update aa set sum1=(select isNULL(sum1,0) from theA where id=(select max(id) from
    theA where id<aa.id))+the1,sum2=(select isNULL(sum2,0) from theA where id=select max(id) from theA where id<aa.id))+the2 from theA aa
    这种的写出来不给分。
      

  9.   


    Update theA set sum1=theB.aa,sum2=theB.bb 
    from 
    (
    select l.id,sum(r.the1) aa,sum(r.the2) bb
    from theA l join theA r on l.id>=r.id
    group by l.id) theB
    where theA.id=theB.id
      

  10.   

    id      the1      the2     sum1        sum2 
    -----------------------1 2 2 2 2
    2 3 1 5 3
    3 4 3 9 6
      

  11.   

    declare @a int, @b int
    set @a=0 
    set @b=0update theA set @a=@a+isnull(the1,0),sum1=@a update theA set @b=@b+isnull(the2,0),sum2=@b