好的,所以我有一个提取员工信息的SQL查询;职位,性别,工资率等。我有临时表,目前正在显示平均工资率。大。
现在我需要引入职称并保持平均工资率,但只限制男性和女性都在职位中的结果。帮助
非常感谢所有帮助!
另外,无论如何都要创建一个新列,显示男性和女性之间的薪酬差异吗?
If EXISTS (Select * FROM TempDB..SYSOBJECTS WHERE name = '##JTemp')
BEGIN
DROP TABLE ##JTemp
END
GO
SELECT
e.EmployeeID AS 'Employee ID',
c.FirstName + ' ' + c.LastName AS 'Employee Name',
e.Gender AS 'Employee Gender',
e.Title AS 'Job Title',
edh.DepartmentID,
d.Name AS 'Department Name',
MAX(eph.Rate) AS 'Pay Rate',
eph.PayFrequency AS 'Pay Frequency',
MAX(eph.Rate) * eph.PayFrequency AS 'Rate x Frequency'
INTO
##JTemp
FROM
HumanResources.Employee e
INNER JOIN
Person.Contact c on c.ContactID = e.EmployeeID
INNER JOIN
HumanResources.EmployeeDepartmentHistory edh on
edh.EmployeeID = e.EmployeeID
INNER JOIN
HumanResources.Department d on d.DepartmentID = edh.DepartmentID
INNER JOIN
HumanResources.EmployeePayHistory eph on eph.EmployeeID = e.EmployeeID
WHERE
edh.EndDate IS NULL
GROUP BY
e.EmployeeID, c.FirstName, c.LastName,
e.Gender, e.Title, edh.DepartmentID,
d.Name, eph.PayFrequency
SELECT
[Job Title],
[Employee Gender],
AVG([Pay Rate]) AS 'Average Pay Rate',
AVG([Rate x Frequency]) AS 'Average Rate x Frequency'
FROM
##JTemp
GROUP BY
[Job Title], [Employee Gender]
ORDER BY
[Job Title]
--WHERE [Employee Gender] = 'M'
IF EXISTS (Select * FROM TEMPDB..SYSOBJECTS WHERE name = '##JTemp')
BEGIN
DROP TABLE ##JTemp
END
GO
答案 0 :(得分:2)
Select
j1.[Job Title],
AVG(j1.[Pay Rate]) AS 'Average Pay Rate',
AVG(j1.[Pay Frequency]) AS 'Average Rate x Frequency',
(
(Select AVG(j2.[Pay Rate])
From ##JTemp j2
Where j2.[Employee Gender] = 'M'
and j2.[Job Title] = J1.[Job Title]
Group By
j2.[Job Title])
-
(Select AVG(j2.[Pay Rate])
From ##JTemp j2
Where j2.[Employee Gender] = 'F'
and j2.[Job Title] = J1.[Job Title]
Group By
j2.[Job Title])
) as [Pay Rate Diff]
From
##JTemp j1
Group By
j1.[Job Title]
HAVING COUNT(DISTINCT j1.[Employee Gender]) = 2
Order By
j1.[Job Title]
答案 1 :(得分:1)
select m.[Job Title], m.ap, m.mp, f.ap, f.mp
from ( Select [Job Title], AVG([Pay Rate]) as ap, max([Pay Rate]) as mp
From ##JTemp
Where [Employee Gender] = 'M'
Group By [Job Title] ) m
join ( Select [Job Title], AVG([Pay Rate]) as ap, max([Pay Rate]) as mp
From ##JTemp
Where [Employee Gender] = 'F'
Group By [Job Title] ) f
on m.[Job Title] = f.[Job Title]