注意,这是一个家庭作业问题。
请显示平均工资最高的部门的部门编号和最低工资。
这是我到目前为止所拥有的,
SELECT DEPARTMENT_ID, MAX_AVG_SALARY
FROM
(SELECT DEPARTMENT_ID, AVG(SALARY) AS MAX_AVG_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID)
WHERE MAX_AVG_SALARY =
(SELECT MAX(MAX_AVG_SALARY)
FROM
(SELECT DEPARTMENT_ID,
AVG(SALARY) AS MAX_AVG_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
));
我可以获得薪水最高的department_id,但是如何找到同一部门的最低工资?
请帮忙!
谢谢!
答案 0 :(得分:1)
SELECT MINIMUM_SALARY,DEPARTMENT_ID
FROM
(
SELECT AVG(SALARY) AS AVERAGE_SALARY,
MIN(SALARY) AS MINIMUM_SALARY,
DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
)EMPLOYEE_AGGREGATED
WHERE
AVERAGE_SALARY = (SELECT MAX(AVG(SALARY)) FROM EMPLOYEES GROUP BY DEPARTMENT_ID)
答案 1 :(得分:0)
analytic functions的解决方案:
select department_id, ms min_salary
from (
select department_id, max(avg(salary)) over () mav,
min(min(salary)) over (partition by department_id) ms,
min(avg(salary)) over (partition by department_id) av
from employees group by department_id )
where av = mav order by department_id
答案 2 :(得分:0)
您可以使用可以重复使用的命名子查询:
WITH grouped_salaries AS (
SELECT department_id,
MIN( salary ) AS min_dept_salary,
AVG( salary ) AS avg_dept_salary
FROM Employees
GROUP BY department_id
)
SELECT department_id,
min_dept_salary
FROM grouped_salaries
WHERE avg_dept_salary = ( SELECT MAX( avg_dept_salary )
FROM grouped_salaries );
答案 3 :(得分:0)
select e.department_id, min(e.salary) min
from employees e
having avg(e.salary) = (select max(avg(e.salary))
from employees e
group by e.department_id)
group by e.department_id
答案 4 :(得分:0)
这是不使用行限制功能的答案,因为旧的 oracle 不提供行限制功能
SELECT job_id, avg(salary) FROM Employees
GROUP BY job_id
HAVING AVG(salary) = (SELECT MIN(AVG(salary)) FROM EMPLOYEES GROUP BY job_id);
答案 5 :(得分:0)
WITH CTE(ID, SAL) AS
(
SELECT DISTINCT
DEPARTMENT_ID,
AVG(SALARY) OVER(PARTITION BY DEPARTMENT_ID)"AVGSAL"
FROM
EMPLOYEES
ORDER BY
AVGSAL DESC
)
SELECT
MIN(SALARY)
FROM
EMPLOYEES E1
WHERE
E1.DEPARTMENT_ID =
(
SELECT
ID
FROM
CTE
WHERE
ROWNUM = 1
)
;