分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总
Posted OkidoGreen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总相关的知识,希望对你有一定的参考价值。
oracle分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总_rfb0204421的专栏-CSDN博客
sum over partition by 的用法_小于号的专栏-CSDN博客
语法:sum(col1) over(partition by col2 order by col3 )
准备数据:
DEPT_ID ENAME SAL
1 1000 A 2500
2 1000 B 3500
3 1000 C 1500
4 1000 D 2000
5 2000 E 2500
6 2000 F 2000
7 2000 G 3500
主要有四种情况:
sum(sal) over (partition by deptno order by ename) 按部门“连续”求总和
sum(sal) over (partition by deptno) 按部门求总和
sum(sal) over (order by deptno,ename) 不按部门“连续”求总和
sum(sal) over () 不按部门,求所有员工总和,效果等同于sum(sal)。
1.有partition by有order by : 在partition by分组下,按照不同的order by col3实现递增汇总..
SQL>>select DEPT_ID,ENAME,SAL,sum(SAL) over(partition by dept_id order by ENAME) AS TOTAL from dept_sal
结果:按照部门分组,按名字排序实现递增汇总.
DEPT_ID ENAME SAL TOTAL
1 1000 A 3500 3500
2 1000 B 3500 7000
3 1000 C 1500 8500
4 1000 D 2000 10500
5 2000 E 2500 2500
6 2000 F 2000 4500
7 2000 G 3500 8000
如果col3重复会只加总一次(当然在本例中这种写法毫无意义):
SQL>> select DEPT_ID,ENAME,SAL,sum(SAL) over(partition by dept_id order by SAL) AS TOTAL from dept_sal
DEPT_ID ENAME SAL TOTAL
11000 C 1500 1500
2 1000 D 2000 3500
3 1000 A 3500 10500
4 1000 B 3500 10500
5 2000 F 2000 2000
6 2000 E 2500 4500
7 2000 G 3500 8000
2.有partition by无order by: 实现分组内所有数据的汇总
SQL>>select DEPT_ID,ENAME,SAL,sum(SAL) over(partition by dept_id) AS TOTAL from dept_sal
DEPT_ID ENAME SAL TOTAL
1 1000 A 3500 10500
2 1000 B 3500 10500
3 1000 C 1500 10500
4 1000 D 2000 10500
5 2000 E 2500 8000
6 2000 F 2000 8000
7 2000 G 3500 8000
3.无partition by有order by : 直接按order by 字段实现递增汇总
SQL>>select DEPT_ID,ENAME,SAL,sum(SAL) over(order by ENAME) AS TOTAL from dept_sal
DEPT_ID ENAME SAL TOTAL
1 1000 A 3500 3500
2 1000 B 3500 7000
3 1000 C 1500 8500
4 1000 D 2000 10500
5 2000 E 2500 13000
6 2000 F 2000 15000
7 2000 G 3500 18500
如果order by 的值相同,会进行汇总,但汇总后显示的值会是一样的,如下:
SQL>>select DEPT_ID,ENAME,SAL,sum(SAL) over(order by DEPT_ID) AS TOTAL from dept_sal
DEPT_ID ENAME SAL TOTAL
1 1000 A 3500 10500
2 1000 B 3500 10500
3 1000 C 1500 10500
4 1000 D 2000 10500
5 2000 E 2500 18500
6 2000 F 2000 18500
7 2000 G 3500 18500
4.无partition by无order by: 所有数据相加.
SQL>>select DEPT_ID,ENAME,SAL,sum(SAL) over() AS TOTAL from dept_sal
DEPT_ID ENAME SAL TOTAL
1 1000 A 3500 18500
2 1000 B 3500 18500
3 1000 C 1500 18500
4 1000 D 2000 18500
5 2000 E 2500 18500
6 2000 F 2000 18500
7 2000 G 3500 18500
--------------------- --------------------- --------------------- --------------------- ---------------------
over不能单独使用,要和分析函数:rank(),dense_rank(),row_number(),sum()等一起使用。
over函数的参数:over(partition by columnname1 order by columnname2)
含义,按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序。
例子:
select deptno,ename,sal,
sum(sal) over (partition by deptno order by ename) 部门连续求和,--各部门的薪水"连续"求和
sum(sal) over (partition by deptno) 部门总和, -- 部门统计的总和,同一部门总和不变
100*round(sal/sum(sal) over (partition by deptno),4) "部门份额(%)",
sum(sal) over (order by deptno,ename) 连续求和, --所有部门的薪水"连续"求和
sum(sal) over () 总和, -- 此处sum(sal) over () 等同于sum(sal),所有员工的薪水总和
100*round(sal/sum(sal) over (),4) "总份额(%)"
from emp;
以上是关于分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总的主要内容,如果未能解决你的问题,请参考以下文章