[英]Sql query to calculate total based on 2 conditions in 1 query
I have a table like below:我有一个如下表:
Employee :
EmpNo
FirstName
LastName
Dept :
DeptName
Head
NoOfEmployees
EmpContracts :
EmpNo DeptName Salary StartDate EndDate
1 d1 1000 2017-12-12 2018-12-12
1 d2 1000 2017-06-12 2018-12-12
2 d3 1000 2017-06-12 2018-12-12
2 d4 1000 2017-12-12 2018-12-12
3 d5 4000 2017-12-12 2018-12-12
This is what I am trying to do:这就是我想要做的:
Display the dept details and the total salary amount for the contracts on each department, with the following conditions: 1) Calculated total for contracts must be from employees that work exclusively for that department 2) Total calculated for the contracts should be after 2001 order result by NoOfEmployees显示每个部门的部门详细信息和合同的工资总额,条件如下: 1)合同计算总额必须来自专门为该部门工作的员工 2)合同总额计算应在 2001 订单结果之后由 NoOfEmployees 提供
I am confused with how to satisfy both condition in 1 query?我对如何在 1 个查询中同时满足这两个条件感到困惑?
This is what I have tried so far:到目前为止,这是我尝试过的:
select [EmpNO],[DeptName],[StartDate], SUM([Salary]) from [dbo].[EmpContracts]
group by [EmpNo],[DeptName],[StartDate]
having YEAR([StartDate]) > 2001
Can anybody please help me with this?有人可以帮我吗?
I am not sure but here it is:我不确定,但它是:
select EmpNO, max(DeptName), SUM(Salary) total_sal
from EmpContracts
where year(StartDate) > 2001
group by EmpNo
having count(distinct DeptName) =1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.