计算员工的贷方金额

时间:2014-04-29 10:02:47

标签: sql sql-server

我将计算员工绩效的数量:

select Job_Id, Sum(money1) as m1 from T1 where Job_Id='8'
group by Job_Id

然后我计算了他的工资额(在其他表= T2中):

select Job_Id, Sum(money2) as m2 from T2 where Job_Id='8'
group by Job_Id

现在,我将获得以下内容:m1 - m2

如何?

3 个答案:

答案 0 :(得分:2)

我认为这会做你想做的事情:

SELECT job, m1-m2 FROM 
(
SELECT T1.Job_Id AS job, Sum(money1) as m1, Sum(money2) as m2 
FROM T1 LEFT JOIN T2 
ON T1.Job_id=T2.Job_id 
WHERE T1.Job_Id='8'
group by T1.Job_Id
)

答案 1 :(得分:2)

WITH T1 AS 
( 
  SELECT 

    SUM(Money1) as Total 
  FROM M1 
) 
SELECT SUM(Money1 - Money2)
FROM T1 INNER JOIN M2 
ON M2.Job_ID = T1.Job_ID 
where M2.JOB_ID = 8 

答案 2 :(得分:0)

我找到了它:

select (select Sum(money1) from T1 where Job_Id='8') - 
       (select Sum(money2) from T2 where Job_Id='8')