关于oracle分组后组外排序的问题

[求助]关于oracle分组后组外排序的问题

只用查询语句实现:
DEPTNO ENAME SAL TOP3
---------- ---------- ---------- ----------
20 KING 5000 1
20 CLARK 2450 2
20 MILLER 1300 3

10 SCOTT 3000 1
10 FORD 3000 2
10 JONES 2975 3

30 BLAKE 2850 1
30 ALLEN 1600 2
30 TURNER 1500 3

按部门分组,部门内部的sal降序,部门间按每个部门sal最大值(粗体字)降序,该如何实现 ,请教各位高手!!谢谢!!
------------------------------------------------------------------------------------------------------------------------------------------

回复:[求助]关于oracle分组后组外排序的问题

selelct t1.*
from (select DEPTNO ,ENAME ,SAL , row_number() over(partition by deptno order by sal desc) top3 from tableName) t1,
(select rownum seq,depno from (select deptno ,max(sal) from tablename group by depno order by max(sal) desc)) t2
where t1.deptno=t2.deptno order by t2.seq,t1.sal desc;

你可能感兴趣的:(oracle)