如何找到平均工资高于公司平均工资的部门?

时间:2014-10-08 22:04:02

标签: sql sql-server-2008

我试图只输出平均工资高于公司平均工资的部门。

SELECT Departments.DEPARTMENT_NAME
FROM Employees inner join departments
ON Employees.DEPARTMENT_ID = Departments.DEPARTMENT_ID
WHERE (SELECT AVG(CAST(Employees.salary AS decimal)) 
      GROUP BY Employees.DEPARTMENT_ID) > (SELECT AVG(CAST(Employees.salary AS decimal)))
GROUP BY Departments.DEPARTMENT_NAME; 

1 个答案:

答案 0 :(得分:1)

我认为这可能是你想要的:

SELECT D.DEPARTMENT_NAME
FROM Employees E
INNER JOIN Departments D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
GROUP BY D.DEPARTMENT_NAME
HAVING AVG(CAST(E.Salary AS decimal)) > 
  (SELECT AVG(CAST(Salary AS decimal)) FROM Employees)

Sample SQL Fiddle