update tablename
set d=d.v_c
from (select min(a) v_a,b,sum(c) v_c from tablename group by b) d
where tabkebane.a=d.v_a and tablename.b=d.b
这是在sql server里面的方法

解决方案 »

  1.   

    SQL Server:update TableName set D = tt.sumC
    from (select min(A) minA,B,sum(C) sumC from TableName group by B) tt
    where TableName.A = tt.minA; 
      

  2.   

    沒想到wwl007兄快我一步,
    而且還是一樣.
    :)BTW:wwl007,你的那個SQL好像不能再优化了.要我也是那麼寫.
      

  3.   

    update TableName set D = tt.sumC  --更新表TableName的D列為結果集tt的sumC列
    from (select min(A) minA,B,sum(C) sumC from TableName group by B) tt  --從按B分組的結果集tt中
    where TableName.A = tt.minA and TableName.B = tt.B;  --關聯minA是min(A)的別名,sumC是sumC的別名.
    tt是select min(A) minA,B,sum(C) sumC from TableName group by B這樣一個結果集的別名.