Update table field by summing field from another table [duplicate]

南楼画角 提交于 2020-08-10 19:18:14

问题


I am trying to update a table filed taking value from another table by summing a field based on EmpID.

In tblEmpInfo EmpID is primary key. I have saw few post on this site but couldn't adopt to my file. Here is my code till yet.

UPDATE tblEmpInfo AS c 
INNER JOIN (SELECT EmpID, SUM(ProfidentFund) AS total FROM tblTransactions GROUP BY EmpID)  AS x ON c.EmpID = x.EmpID 
SET c.ProfidentFund = x.total;

Above code give me following warning and do not update value to tblEmpInfo

tblEmpInfo screenshot.

tblTransactions screenshot.

And my expected output.


回答1:


You can try the below -

UPDATE  c 
SET c.ProfidentFund = x.total
from tblEmpInfo AS c
INNER JOIN 
(SELECT EmpID, SUM(ProfidentFund) AS total FROM tblTransactions GROUP BY EmpID)  AS x 
    ON c.EmpID = x.EmpID 


来源:https://stackoverflow.com/questions/62929358/update-table-field-by-summing-field-from-another-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!