oracle很有用的统计函数rank和cume
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle很有用的统计函数rank和cume相关的知识,希望对你有一定的参考价值。
oracle 10g 里面有scott用户
alter user scott account unlock ;
使用 scott 密码 tiger 进入,修改密码,改用户有emp 表;
rank函数 :
SELECT deptno, job, SUM(sal),
RANK() OVER(PARTITION BY deptno ORDER BY
SUM(sal) DESC)
AS jobdep_rank,RANK() OVER(ORDER BY SUM(sal) DESC) AS sumsal_rank
FROM emp
GROUP BY deptno, job ;
DEPTNO | JOB | SUM(SAL) | JOBDEP_RANK | SUMSAL_RANK |
20 | ANALYST | 6000 | 1 | 1 |
30 | SALESMAN | 5600 | 1 | 2 |
10 | PRESIDENT | 5000 | 1 | 3 |
20 | MANAGER | 2975 | 2 | 4 |
30 | MANAGER | 2850 | 2 | 5 |
10 | MANAGER | 2450 | 2 | 6 |
20 | CLERK | 1900 | 3 | 7 |
10 | CLERK | 1300 | 3 | 8 |
30 | CLERK | 950 | 3 | 9 |
cume_dist
SELECT deptno, job, SUM(sal),
CUME_DIST() OVER(PARTITION BY deptno ORDER BY
SUM(sal) DESC)
AS cume_dist_per_dep
FROM emp
GROUP BY deptno, job
ORDER BY deptno, SUM(sal);
DEPTNO | JOB | SUM(SAL) | CUME_DIST_PER_DEP |
10 | CLERK | 1300 | 1 |
10 | MANAGER | 2450 | 0.666667 |
10 | PRESIDENT | 5000 | 0.333333 |
20 | CLERK | 1900 | 1 |
20 | MANAGER | 2975 | 0.666667 |
20 | ANALYST | 6000 | 0.333333 |
30 | CLERK | 950 | 1 |
30 | MANAGER | 2850 | 0.666667 |
30 | SALESMAN | 5600 | 0.333333 |
有了这两个有用的函数,排名和百分比很快就能得到了吧。
以上是关于oracle很有用的统计函数rank和cume的主要内容,如果未能解决你的问题,请参考以下文章
Hive学习之路 (十五)Hive分析窗口函数 CUME_DIST和PERCENT_RANK