oracle很有用的统计函数rank和cume_dist

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)