我希望获得每个部门的前3名不同薪水。我能够使用RANK()
或DENSE_RANK()
或ROW_NUMBER()
来执行此操作,但我的桌子上有一些工资相同的记录。
下面提到的是我的查询及其结果。
部门20的前三名薪水应为6000,3000,2975。 但是有2名员工的工资为3000,他们都有2级。所以它给了我这个部门的4条记录(1级为1级,2级为2级,1级为3级)。
请建议/建议如何为每个部门获得截然不同的前三名工资。
SELECT * FROM (
SELECT EMPNO, DEPTNO, SAL,
DENSE_RANK() over (partition by deptno order by sal DESC) as RANK,
row_number() over (partition by deptno order by sal DESC) as ROWNO
from EMP)
WHERE RANK <= 3;
Empno Deptno Salary Rank Rowno
----------------------------------------
7839 10 5000 1 1
7782 10 2450 2 2
7934 10 1300 3 3
7935 20 6000 1 1
7788 20 3000 2 2
7902 20 3000 2 3
7566 20 2975 3 4
7698 30 2850 1 1
7499 30 1600 2 2
7844 30 1500 3 3
答案 0 :(得分:4)
如果您使用row_number
在partitioning by dept,salary
中更具体,那么您可以将row_number
和dense_rank
合并为此查询:
with data_row as
(
select 7839 as empno, 10 as deptno, 5000 as salary from dual union all
select 7782 as empno, 10 as deptno, 2450 as salary from dual union all
select 7934 as empno, 10 as deptno, 1300 as salary from dual union all
select 1111 as empno, 10 as deptno, 1111 as salary from dual union all
select 7935 as empno, 20 as deptno, 6000 as salary from dual union all
select 7788 as empno, 20 as deptno, 3000 as salary from dual union all
select 7902 as empno, 20 as deptno, 3000 as salary from dual union all
select 7566 as empno, 20 as deptno, 2975 as salary from dual union all
select 2222 as empno, 20 as deptno, 2222 as salary from dual union all
select 7698 as empno, 30 as deptno, 2850 as salary from dual union all
select 7499 as empno, 30 as deptno, 1600 as salary from dual union all
select 7844 as empno, 30 as deptno, 1500 as salary from dual union all
select 3333 as empno, 30 as deptno, 1333 as salary from dual
)
select *
from
(
select
deptno,
salary,
dense_rank() over (partition by deptno order by salary desc) as drank,
row_number() over (partition by deptno, salary order by salary desc) as rowno
from data_row
)
where drank <=3 and
rowno =1
答案 1 :(得分:1)
您使用的row_number
函数应该可以解决问题:
SELECT *
FROM (SELECT empno, deptno, sal
DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal DESC) as rk,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sal DESC) as rowno
FROM emp)
WHERE rowno <= 3;
答案 2 :(得分:-1)
请尝试:
SELECT empno, deptno, DISTINCT(sal)
FROM (SELECT empno, deptno, sal
DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal DESC) as rk,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sal DESC) as rowno
FROM emp)
WHERE rowno <= 3;