我有表A和B
A B
ts ta ts ta
1 0,00 1 10,00
2 0,00 1 5,00
3 0,00 1 6,00
4 0,00 2 3,00
2 5,00
2 10,00
3 5,00
我想更新表A以获得此结果:
A
ts ta
1 21,00
2 18,00
3 5,00
到目前为止,我已尝试使用此查询:
update A
set A.ta = C.sta
from (SELECT SUM(B.ta) sta
FROM B INNER JOIN A ON B.ts = A.ts
GROUP BY B.ts) C
并得到这个不需要的结果:
ts ta
1 21,00
2 5,00
3 21,00
答案 0 :(得分:0)
UPDATE A
SET ta = (SELECT sum(ta) FROM B WHERE ts = A.ts)
FROM A