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 |
李世侠,网名caibird2005 , initdba , 战神
MySQL DBA经验6年;
Oracle DBA经验10年;
精通MySQL HA架构,复制,备份,恢复,SQL优化;
熟悉SHELL编程、Python编程、SQL&PL/SQL编程;
MySQL OCP课程讲师;
ORACLE官方认证OCM证书;
BLOG: http://www.cnblogs.com/caibird2005
http://www.initdba.cn
Wechat: caibird2005
TEL :+86-186-5881-5300 +86-180-7290-8651