2012-12-19 47 views
0

我需要检索获得比其部门的平均工资更多钱的员工的信息...我们有部门名为10,20,30,40,50 ......等等上。在这里,我设法找回了我只需要一个部门的东西。 (40)我可以如何为多个部门做到这一点?从表中检索信息

这是我的查询:

SELECT * FROM EMPLOYEE where (Department_ID='40')and 
(
employee_salary > 
    (select avg(employee_salary) from EMPLOYEE where Department_ID='40') 
) 

Datatabledata table

+0

与你的表结构提供第一。 –

+0

https://www.dropbox.com/s/gqoatxyglgllapr/Untitled.png – user1915345

回答

4

希望这会做,

SELECT emp.* FROM EMPLOYEE emp where emp.employee_salary > 
     ( select avg(employee_salary) from EMPLOYEE new1 
     where emp.Department_ID=new1.Department_ID 
     group by Department_ID 
    ) 
+0

非常感谢您的帮助!所以你创建了两个表来进行比较?我对吗? – user1915345

+0

@ user1915345他没有创建2个表格。他正在使用同一张表,并使用不同的别名。所以表格实际上只是比较自己。 –

+0

好吧,我想我现在明白了......感谢您的解释! – user1915345

0

尝试此查询

select * from EMPLOYEE as e1 
where e1.employee_salary > (select avg(employee_salary) 
         from EMPLOYEE as e2 
         where e1.department_id=e2.department_id 
         group by Department_id) 
0

你可以试试这个:

SELECT *, avg(employee_salary) as average_salary 
FROM EMPLOYEE 
where Department_ID='40' 
having average_salary<employee_salary 
0
select e.* from employee as e inner join 
    (select department_id, avg(employee_salary) as avg_salary 
     from employee group by department_id) as f 
on e.department_id = f.department_id 
where e.employee_salary > f.avg_salary;