找到最高和最低的薪水

时间:2019-02-01 11:04:47

标签: sql oracle greatest-n-per-group

如何在薪水最高和最低的每个经理下查找员工姓名

我的桌子

EMPNO   ENAME   JOB      MGR    HIREDATE    SAL     COMM    DEPTNO.
7369    SMITH   CLERK     7902  17/12/1980  1250             20
7499    ALLEN   SALESMAN  7902  20/02/1981  1600     300     30
7521    WARD    SALESMAN  7839  22/02/1981  1250     500     30
7654    MARTIN  SALESMAN  7566  28/09/1981  1250    1400     30
7782    CLARK   MANAGER   7839  09/06/1981  2450             10
7788    SCOTT   ANALYST   7566  19/04/1987  3000             20
7839    KING    PRESIDENT 7566  17/11/1981  5000             10
7844    TURNER  SALESMAN  7788  08/09/1981  1500       0     30
7876    ADAMS   CLERK     7788  23/05/1987  1100             20
7900    JAMES   CLERK           03/12/1981   950             30
7902    FORD    ANALYST   7566  03/12/1981  3000             20
7934    MILLER  CLERK     7782  23/01/1982  1300             10

1 个答案:

答案 0 :(得分:1)

用您的表名替换T。

SELECT Mgr, EmpNo, EName, Sal
FROM (
    SELECT Mgr, EmpNo, EName, Sal,
        RANK() OVER (PARTITION BY Mgr ORDER BY Sal ASC ) AS RankAsc,
        RANK() OVER (PARTITION BY Mgr ORDER BY Sal DESC) AS RankDesc
    FROM T
) RankedSalaries
WHERE RankAsc = 1 OR RankDesc = 1

如果需要经理的名称,则可以将RankedSalaries回到表ON Mgr = EmpNo