Oracle分析函数学习笔记2
目的:以oracle自带的scott模式为测试环境,主要通过试验体会分析函数的用法。
内容来自dedecms
2.rank()、dense_rank() 的使用
原表信息:
copyright dedecms
SQL> break on deptno skip 1 -- 为效果更明显,把不同部门的数据隔段显示。
SQL> select deptno,ename,sal
2 from emp
3 order by deptno,sal desc; 内容来自dedecms
DEPTNO ENAME SAL
---------- ---------- ----------
10 KING 5000
CLARK 2450
MILLER 1300 copyright dedecms
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行。
使用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 / dedecms.com
DEPTNO RK ENAME SAL
---------- ---------- ---------- ----------
10 1 KING 5000
2 CLARK 2450
3 MILLER 1300 copyright dedecms
20 1 SCOTT 3000
1 FORD 3000
3 JONES 2975
30 1 BLAKE 2850
2 ALLEN 1600
3 TURNER 1500
本文来自织梦
已选择9行。
使用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 / 织梦内容管理系统
DEPTNO DRK ENAME SAL
---------- ---------- ---------- ----------
10 1 KING 5000
2 CLARK 2450
3 MILLER 1300
20 1 SCOTT 3000
1 FORD 3000
2 JONES 2975
3 ADAMS 1100 内容来自dedecms
30 1 BLAKE 2850
2 ALLEN 1600
3 TURNER 1500
本文来自织梦
已选择10行。
本文来自织梦
文章评论
共有位Admini5网友发表了评论 查看完整内容