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
I am confused with how to satisfy both condition in 1 query?
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.