|M| 更新A表 数据由B表取要怎么来更新 谢谢
如表
A
ID 总销售价 总成本价
1 100 50
B
ID 销售单ID 销售价 成本价
1 1 30 20
2 1 50 40
3 1 50 40
------------------------------------
现在要由B表更新A表 最后为
A
ID 总销售价 总成本价
1 130 100
SQL语句要怎么来写
------解决方案--------------------update A set 总销售价=销售价 ,总成本价= 成本价
from A as t1
inner join
(select 销售单ID,sum(销售价) as 销售价,sum(成本价) as 成本价 from B group by 销售单ID) as t2 on t1.id=t2.销售单ID
------解决方案--------------------UPDATE A set A.[总销售价] = B.[总销售价],A.[总成本价] = B.[总成本价]
from
(select ID,sum([总销售价])[总销售价],sum([总成本价])[总成本价] from B group by ID) B
where A.ID = B.ID