TABLE :a 
F1    F2
100   0
200   0
300   0
tABLE :B 
F1   f2
100  10
100  20
200  10
200  15把b表的F2字段由F1 汇总(SUM) 
再由汇总后的值 填充 A 表中的。F2  条件是 F1 相等 
求一句SQL语句

解决方案 »

  1.   

    update a set f2=b.f2 from (select f1,sum(f2) f2 from b group by f1) b
    where a.f1=b.f1
      

  2.   

    update a set f2=tmp.hj
    from a,(select f1,sum(f2) as hj from b
            group by f1) tmp
    where a.f1=tmp.f1
      

  3.   

    update a set f2=tmp.hj
    from a,(select f1,sum(f2) as hj from b
            group by f1) tmp
    where a.f1=tmp.f1
    -----------------------------这个对。
      

  4.   

    5555
    被 人搶先了。
    不過我一般是這麼寫
    update a set F2=
    isnull((select sum(f2) from b where a.F1=b.F1),0)這種寫法跟樓上幾位的寫法哪種好一點呢?
    請教高手了