[英]update 2nd table column based on 1st table
I have two tables.我有两张桌子。 One is Employee
and other is Department
.一个是Employee
,另一个是Department
。
Employee table has following columns:员工表具有以下列:
Name | Salary | Department Id
Department table has部门表有
Department Id| Department Name | SumofSalary
I want to update SumofSalary department wise我想明智地更新 SumofSalary 部门
Code I have tried我试过的代码
update Department set sumofsalary =
(
select D.deptid, SUM(E.salary) from Department D inner join Employesalary E
on d.deptid=e.deptid group by D.deptid
)
Following will give you sum of salaries department wise and update in department table.以下将为您提供部门明智的工资总额并在部门表中更新。
UPDATE D SET D.SumOfSalary = T.Salary FROM Department D JOIN
(
SELECT SUM(Salary) Salary, [Department ID] DeptID FROM Employee Group By [Deptartment ID]
) T ON T.DeptID = D.[Department ID]
You can try the below code a simple subquery Which will update the sum salary department wise你可以试试下面的代码一个简单的子查询,它将更新工资部门的总和
UPDATE dept
SET dept.sum =h.totalSum FROM (select a.deptid, SUM( a.Salary) AS
totalSum FROM emp a
Group By a.deptid) h WHERE
dept.deptid=h.deptid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.