此SQL查询有什么问题?
SELECT
department_id, MAX(AVG(SALARY))
FROM
EMPLOYEES
GROUP BY
department_id;
它不显示单组组功能
答案 0 :(得分:2)
2一个查询中的聚合函数无法完成,您应该使用子查询来实现结果。
我现在无法对其进行测试,因此无法保证此查询,但您可能会有所了解。
select max (avg_salary)
from (select department_id, avg(SALARY) AS avg_salary
from EMPLOYEES
group by department_id);
内部查询选择deparment_id和平均工资。 使用AS语句使用别名avg_salary选择Avarage薪水。
外部查询选择avg_salary的最大值 -
这可能不是你问题的完整解决方案,正如我所说,没有经过测试,所以没有保证,但你现在应该知道如何开始。 ; - )
答案 1 :(得分:0)
您无法在一个查询中拥有多个聚合函数。试试这个
select dept, max(average) over (partition by dept)
from (SELECT department_id dept,
(AVG(SALary) OVER (PARTITION BY department_id)) average
FROM employees);
答案 2 :(得分:0)
备选方案1,双GROUP BY
:
SELECT department_id, AVG(SALARY)
FROM EMPLOYEES
GROUP BY department_id
HAVING AVG(SALARY) = (select max(avg_sal)
from (select avg(salary) as avg_sal
from EMPLOYEES
group by department_id))
如果有平局,将返回department_id
两个!
备选方案2,使用cte(公用表表达式):
with
(
SELECT department_id, AVG(SALARY) as avg_sal
FROM EMPLOYEES
GROUP BY department_id
) as cte
select department_id, avg_sal
from cte
where avg_sal = (select max(avg_sal) from cte)
如果有平局,这也将返回department_id
两个!