Emp_id  Emp_Name    Dept_ID Dept_Name salary
1   a   AA  IT  2000       100
2   b   AA  IT  3000       200
3   c   AA  IT  4000       300
4   d   BB  HR  1000       400
5   e   BB  HR  2000       500
6   f   BB  HR  3000       600
7   g   BB  HR  4000       700
8   h   CC  FI  2500       800
9   i   CC  FI  3500       900

这是我的表结构。我想找出薪水高于其组中平均薪水的emp_iddept_id。我尝试了SQL查询,但显示错误
select Emp_id
from dbo.Sheet1$
where Salary >= (select AVG(Salary) from dbo.Sheet1$ group by Dept_ID)

最佳答案

select Emp_id,Dept_ID from (
  select Emp_id, Emp_Name,
  avg(salary) over (partition by dept_id) avg_sal
  from yourtable
) a
where salary > avg_sal

关于sql - 查询在SQL Server 2008中返回多个错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9663006/

10-12 18:39