Oracle分析函数学习笔记2
目的:以oracle自带的scott模式为测试环境,主要通过试验体会分析函数的用法。 站长_之家
2.rank()、dense_rank() 的使用
原表信息:
站长_之家
SQL> break on deptno skip 1 -- 为效果更明显,把不同部门的数据隔段显示。
SQL> select deptno,ename,sal
2 from emp
3 order by deptno,sal desc; 站长之.家adminI5.com
DEPTNO ENAME SAL
---------- ---------- ----------
10 KING 5000
CLARK 2450
MILLER 1300
20 SCOTT 3000
FORD 3000
JONES 2975
ADAMS 1100
SMITH 800
30 BLAKE 2850
ALLEN 1600
TURNER 1500
WARD 1250
MARTIN 1250
JAMES 950
已选择14行。 站长之.家adminI5.com
使用rank()查出各部门薪水前三名的员工姓名、薪水。
SQL> select * from (
2 select deptno,rank() over(partition by deptno order by sal desc) rk,ename,sal
3 from emp
4 )
5 where rk<=3
6 /
DEPTNO RK ENAME SAL
---------- ---------- ---------- ----------
10 1 KING 5000
2 CLARK 2450
3 MILLER 1300
20 1 SCOTT 3000
1 FORD 3000
3 JONES 2975
30 1 BLAKE 2850
2 ALLEN 1600
3 TURNER 1500
已选择9行。 adminI5.com站.长.之.家
使用dense_rank()查出各部门薪水前三名的员工姓名、薪水。
SQL> select * from (
2 select deptno,dense_rank() over(partition by deptno order by sal desc) drk,ename,sal
3 from emp
4 )
5 where drk<=3
6 /
站长之.家adminI5.com
DEPTNO DRK ENAME SAL
---------- ---------- ---------- ----------
10 1 KING 5000
2 CLARK 2450
3 MILLER 1300 站长.之家adminI5.com
20 1 SCOTT 3000
1 FORD 3000
2 JONES 2975
3 ADAMS 1100
30 1 BLAKE 2850
2 ALLEN 1600
3 TURNER 1500
adminI5.com站长之_家
已选择10行。 adminI5.com站长_之家



文章评论
共有 0位Admini5网友发表了评论 查看完整内容