Mysql 选择每个工资高于其部门平均工资的员工
我的数据库中只有一个名为EMPLOYEE的表,其中有3个表:Mysql 选择每个工资高于其部门平均工资的员工,mysql,sql,Mysql,Sql,我的数据库中只有一个名为EMPLOYEE的表,其中有3个表: Employee_Name, Employee_Salary, Department_ID 现在,我必须选择每一位工资高于其部门平均水平的员工我该怎么做? 我面临的主要问题是,在比较每个员工的工资时 SELECT AVG(department_ID) FROM employee GROUP BY Department_ID 内部队列的返回集返回多行 我想我需要执行联接操作,但我不知道如何执行。试试这个。。。这一点没有经过测试 S
Employee_Name, Employee_Salary, Department_ID
现在,我必须选择每一位工资高于其部门平均水平的员工我该怎么做?
我面临的主要问题是,在比较每个员工的工资时
SELECT AVG(department_ID) FROM employee GROUP BY Department_ID
内部队列的返回集返回多行
我想我需要执行联接操作,但我不知道如何执行。试试这个。。。这一点没有经过测试
SELECT * from employee join (SELECT AVG(employee_salary) as sal, department_ID
FROM employee GROUP BY Department_ID) as t1
ON employee.department_ID = t1.department_ID
where employee.employee_salary > t1.sal
请尝试以下查询
Select * from employee a where Employee_Salary > (select avg(Employee_Salary) from
employee b group by Department_ID having b.Department_ID = a.Department_ID)
或
在不分组的情况下尝试此项
SELECT * FROM employee E1
WHERE [Employee_Salary] > (
SELECT AVG([Employee_Salary]) FROM employee E2
WHERE E2.[Department_ID] = E1.[Department_ID]
)
尝试使用EXISTS()
如:
SELECT t1.Employee_Name, t1.Employee_Salary, t1.Department_ID
FROM Employee t1
WHERE EXISTS
(
SELECT t2.Department_ID, AVG(t2.Employee_Salary) as AvgSalary
FROM Employee t2
WHERE t1.Department_ID = t2.Department_ID
GROUP BY t2.Department_ID
HAVING t1.Employee_Salary>AVG(t2.Employee_Salary)
))
请参见您也可以尝试这种方法
select FirstName,E.DepartmentName,BaseRate,EB.avgSAL
From DimEmployee E
inner join
(select avg(BaseRate) As avgSAL,DepartmentName
from DimEmployee
group by DepartmentName ) EB
ON E.DepartmentName = Eb.DepartmentName
where E.BaseRate > Eb.avgSAL
担任博士后, 试试这个
select e1.* from emp e1 inner join (select avg(sal) avg_sal,dept_id from emp group by dept_id) as e2 on e1.dept_id=e2.dept_id and e1.sal>e2.avg_sal
也可以尝试以下方法:
Select e.ename, e.sal, e.deptno
from (select e.*, avg(sal) over (partition by deptno) as avgsalary
from EMP_TABLE e
) e
where e.sal > e.avgsalary;
第一个问题解决了,非常感谢。请详细说明一下HAVING命令的用法好吗?这是我唯一没有真正理解的部分。两个查询都可以。Having通常用于过滤分组结果。并且它仅与GROUPBY子句一起使用。”where'子句通常用于过滤结果并执行操作。这将首先过滤出结果,然后在deptid上应用groupby。希望这个解释对你有用。@tκηηπαλσαμδηη你确定吗?
Select e.ename, e.sal, e.deptno
from (select e.*, avg(sal) over (partition by deptno) as avgsalary
from EMP_TABLE e
) e
where e.sal > e.avgsalary;