ORA-00934:此处不允许组功能||选择最高薪酬部门的MIN(薪水)

时间:2012-03-14 18:05:08

标签: sql oracle11g subquery ora-00934

O社区,你知道我怎么能选择department_ID,以及平​​均工资最高的部门的最低工资?或者如何消除' ORA-00934:group function not not not允许来这里'问题?我需要使用两个子查询吗?

到目前为止,这是我提出的,试图获得收入最高的部门的department_ID:

SELECT department_ID, MIN(salary
FROM employees
WHERE department_ID = (SELECT department_ID
    FROM employees WHERE salary = MAX(salary));

谢谢,非常感谢您的协助。

2 个答案:

答案 0 :(得分:0)

我无法对此进行测试,但它应该有效:

;WITH DepartmentsSalary AS
(
    SELECT department_ID, AVG(Salary) AvgSalary, MIN(Salary) MinSalary
    FROM employees
    GROUP BY department_ID
)
SELECT department_ID, MinSalary
FROM (  SELECT department_ID, AvgSalary, MAX(AvgSalary) OVER() MaxSalary, MinSalary
        FROM DepartmentsSalary) D
WHERE MaxSalary = AvgSalary

答案 1 :(得分:0)

您可以使用join(那么您只有一个子查询)

select e1.department_ID, min(e1.salary)
from employees e1
join (
    select avg_query.department_ID, max(avg_query.avg_value) 
    from (
        select department_ID, avg(salary) as avg_value
        from employees
        group by department_ID
    ) avg_query
) e2 on e2.department_ID = e1.department_ID
;
  • 第一个子查询返回所有部门的平均工资
  • 基于第一个子查询的下一个子查询返回最高平均值 薪水和相关部门_ID
  • 主查询返回具有最高平均值的department_ID的最低工资 薪水