答案 0 :(得分:1)
LEFT JOIN
与GROUP BY
会有所帮助:
SELECT T1.Emp_Id, T1.Safe_Id, T1.Salary,
SUM(T2.SubSalary) AS SubSalary,
(T1.Salary - SUM(T2.SubSalary)) AS [Difference]
FROM TableOne T1
LEFT JOIN TableTwo T2 ON T2.Emp_Id = T1.Emp_Id AND T2.Safe_Id = T1.Safe_Id
GROUP BY T1.Emp_Id, T1.Safe_Id, T1.Salary
答案 1 :(得分:0)
类似的事情应该可以解决问题
SELECT t1.EmployeeID
,t1.safe_id
,MAX(t1.salary)
,SUM(COALESCE(t2.subSalary, 0)) AS SubSalary
,MAX(t1.salary) - SUM(COALESCE(t2.subSalary, 0)) AS difference
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.emp_id = t2.emp_id
AND t1.safe_id = t2.safe_id
GROUP BY t1.EmployeeID
,t1.safe_id