select
d.DepartmentId,
d.Name,
d.GroupName,
eph.Rate * 40 * 52 * count(edh.EmployeeId) as AnnualPay
from
HumanResources.Department d,
HumanResources.EmployeePayHistory eph,
HumanResources.Employee e,
HumanResources.EmployeeDepartmentHistory edh
where
e.CurrentFlag = 'True' and
edh.DepartmentID = d.DepartmentID and
edh.EmployeeID = eph.EmployeeID and
e.EmployeeID = eph.EmployeeID
group by
d.departmentID;
目标是编写一份报告,以显示分段,姓名,组名,在该部门工作的所有员工的年度总薪酬以及在该部门工作的员工人数。
要计算annual pay
,请将每位员工的工资率乘以 40小时,一年52周。
仅包括所有员工年薪总额超过half a million dollars
的部门。
所有员工都应该是最新的(使用当前标志)。
按报价按员工的年薪总额降序排序。
包含了表格,我无法弄清楚如何按每个部门进行分组...... Database
答案 0 :(得分:0)
如果我理解你在寻找什么,这应该有效:
SELECT
d.DepartmentId,
d.Name,
d.GroupName,
SUM(eph.Rate * 40 * 52) as AnnualPay,
Count(edh.EmployeeId) as EmployeeCount
from HumanResources.Department d
JOIN HumanResources.EmployeeDepartmentHistory edh
ON edh.DepartmentID = d.DepartmentID
JOIN HumanResources.EmployeePayHistory eph
ON edh.EmployeeID = eph.EmployeeID
JOIN HumanResources.Employee e
ON e.EmployeeID = eph.EmployeeID
WHERE e.CurrentFlag = 'True'
GROUP BY d.departmentID,d.Name,d.GroupName
HAVING AnnualPay > 500000
ORDER BY AnnualPay DESC;
答案 1 :(得分:0)
请尝试以下方法......
SELECT DepartmentId AS DepartmentId,
DepartmentName AS DepartmentName,
DepartmentGroup AS DepartmentGroup,
SUM( expectedAnnualPay ) AS TotalExpectedAnnualPay,
COUNT( DepartmentId ) AS EmployeeCount
(
SELECT Department.DepartmentId AS DepartmentId,
Name AS DepartmentName,
GroupName AS DepartmentGroup
Rate *
40 *
52 AS expectedAnnualPay,
EmployeeID AS EmployeeID
FROM HumanResources.Department HRDepartment
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployeeDepartmentHistory.DepartmentID = HRDepartment.DepartmentID
JOIN HumanResources.Employee HREmployee ON HREmployee.EmployeeID = HREmployeeDepartmentHistory.EmployeeID
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployee.EmployeeID = HREmployeePayHistory.EmployeeID
WHERE HREmployee.CurrentFlag = 'True'
) departmentEmployee
GROUP BY DepartmentId
HAVING TotalExpectedAnnualPay > 500000
ORDER BY TotalExpectedAnnualPay DESC;
您似乎要求每个部门的预期年度总薪资额列表,其中该值超过$ 500,000.00,前提是该部门内的所有当前员工都将留在该部门以当前费率计算一年,并且在此期间不会添加任何员工。
我通过以下查询开始解决了这个问题(我给了别名departmentEmployeeLevel
)...
SELECT Department.DepartmentId AS DepartmentId,
Name AS DepartmentName,
GroupName AS DepartmentGroup
Rate *
40 *
52 AS expectedAnnualPay,
EmployeeID AS EmployeeID
FROM HumanResources.Department HRDepartment
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployeeDepartmentHistory.DepartmentID = HRDepartment.DepartmentID
JOIN HumanResources.Employee HREmployee ON HREmployee.EmployeeID = HREmployeeDepartmentHistory.EmployeeID
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployee.EmployeeID = HREmployeePayHistory.EmployeeID
WHERE HREmployee.CurrentFlag = 'True'
此子查询返回每个部门的DepartmentId
,部门Name
和GroupName
以及每个当前员工的EmployeeID
及其计算的预期年薪。
主查询使用子查询的结果将每个部门的结果聚合/分组,并为每个部门计算预期年薪和员工总数。然后,它使用HAVING
将列表细化为预期年度工资成本大于$ 500,000.00的部门,并按该字段对剩余记录进行排序。
如果您有任何问题或意见,请随时发表评论。