本文共 3168 字,大约阅读时间需要 10 分钟。
?Oracle?????OVER?????????????????????????????????????Partition By??????????Order By????????????????OVER???????????????????????????????
OVER??????????
SELECT column_name, sum(column_name) OVER(partition_by_clause order_by_clause) FROM table_name;
SELECT name, class, s, sum(s) OVER(partition_by class order_by s desc) mm FROM t2WHERE mm = 1;
| name | class | s | mm |
|---|---|---|---|
| dss | 1 | 95 | 190 |
| ffd | 1 | 95 | 190 |
| gds | 2 | 92 | 92 |
| cfe | 2 | 74 | 166 |
| gf | 3 | 99 | 198 |
| ddd | 3 | 99 | 198 |
| 3dd | 3 | 78 | 276 |
| asdf | 3 | 55 | 331 |
| asdf | 3 | 45 | 376 |
OVER???????Partition By?Order By????Partition By???????????????Order By????????????????
SELECT deptno, ename, sal, rank() OVER(partition_by deptno order_by sal) r, (SELECT COUNT(1) n FROM emp GROUP BY deptno) bFROM empWHERE deptno IN (30, 60);
SELECT class, s, rank() OVER(partition_by class order_by s desc) mm FROM t2WHERE mm = 1;
OVER????Range?Rows???????????Range?????????Rows????N??
SELECT name, class, s, sum(s) OVER(order_by s range between 5 preceding and 5 following) mm FROM t2WHERE mm = 1;
SELECT name, class, s, sum(s) OVER(order_by s rows between 2 preceding and 2 following) mm FROM t2WHERE mm = 1;
?Range?Rows???unbounded????????????????
SELECT opr_id, res_type, first_value(res_type) OVER(partition_by opr_id order_by res_type) low, last_value(res_type) OVER(partition_by opr_id order_by res_type rows BETWEEN unbounded preceding AND unbounded following) highFROM rm_circuit_routeWHERE opr_id IN ('000100190000000000021311', '000100190000000000021355', '000100190000000000021339')ORDER BY opr_id; rank()????????????????????????
dense_rank()???rank()????????????????
percent_rank()????????????????????
SELECT deptno, ename, sal, (a.r-1)/(n-1) pr1, percent_rank() OVER(partition_by deptno order_by sal) pr2FROM ( SELECT deptno, ename, sal, rank() OVER(partition_by deptno order_by sal) r FROM emp WHERE deptno IN (30, 60)) a,(SELECT deptno, COUNT(1) n FROM emp GROUP BY deptno) bWHERE a.deptno = b.deptno;
cume_dist()?????????????CDF???????????????????
percentile_cont()??????????????????
SELECT ename, sal, deptno, percentile_cont(0.7) within group(order_by sal) over(partition_by deptno) "Percentile_Cont", percent_rank() OVER(partition_by deptno order_by sal) "Percent_Rank"FROM empWHERE deptno IN (30, 60);
PERCENTILE_DISC()????????????????
SELECT ename, sal, deptno, percentile_disc(0.7) within group(order_by sal) over(partition_by deptno) "Percentile_Disc", cume_dist() over(partition_by deptno order_by sal) "Cume_Dist"FROM empWHERE deptno IN (30, 60);
???????????Oracle?OVER??????????????????????????????????????????????????
转载地址:http://qopfk.baihongyu.com/