sql一表中求和并把值插入另一表的问题

有两个表A和B
A表中有如下数据:
DANHAO JINE
001 100
001 200
001 300
002 400
002 500
B表中有如下数据:
DANHAO JINE
001 NULL
002 NULL
现在要在A表中按DANHAO汇总JINE,就是select sum(jine) as jine from A group by danhao,再把结果按单号对应单号插入到B表中
最终结果是
B表:
DANHAO JINE
001 600
002 900
请问SQL语句应该怎么写?

update B
set JINE=A1.jine
from (select danhao ,sum(jine)as jine from A group by danhao)as A1,B
where A1.danhao=B.danhao
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-09
update b
set jine = (select sum(jine) as jine from A where a.danhao=b.danhao group by danhao)
相似回答