分析函式系列之sum(col1) over(partition by col2 order by col3):實現分組彙總或遞增彙總
https://blog.csdn.net/rfb0204421/article/details/7672207
https://blog.csdn.net/wawmg/article/details/40840093
語法: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
---------------------
0、select * from wmg_test; ---測試資料
1、select v1,v2,sum(v2) over(order by v2) as sum --按照 v2排序,累計n+n-1+....+1
from wmg_test;
2、select v1,v2,sum(v2) over(partition by v1 order by v2) as sum --先分組,組內在進行 1 中的操作
from wmg_test;
3、select v1,v2,sum(v2) over(partition by v1 order by v1) as sum ---穩定排序
from wmg_test;
4、select v1,v2,sum(v2) over(partition by v1) as sum --相同key的進行回填處理
from wmg_test;
5、select distinct v1,sum_01 --取一條
from (
select v1,sum(v2) over(partition by v1) as sum_01
from wmg_test
) a;
6、當然也可以逆序累加,只需order by desc 即可
總結區別:group by 和partition by的區別
group 單純分組
partition 也能分組,但還具備累計的功能
---------------------
相關文章
- row_number() OVER (PARTITION BY COL1 ORDER BY COL2)
- Oracle 分組彙總統計函式的使用Oracle函式
- sum()over()和count()over()分析函式函式
- Oracle 10gR2分析函式彙總Oracle 10g函式
- ROWNUMBER() OVER( PARTITION BY COL1
- 常用函式彙總函式
- Oracle函式彙總Oracle函式
- 【彙總】scapy 函式函式
- SQL-ROWNUMBER-OVER彙總SQL
- 報告彙總資料使用組函式函式
- 分散式鎖實現彙總分散式
- 【Mysql】Mysql似oracle分析函式sum over的實現MySqlOracle函式
- Excel 特殊分組彙總示例Excel
- MySQL常用函式彙總MySql函式
- PHP字串函式彙總PHP字串函式
- string 函式彙總函式
- PHP 常用函式彙總PHP函式
- oracle常用函式彙總Oracle函式
- AA常用函式彙總函式
- 開窗函式彙總函式
- PHP陣列函式彙總PHP陣列函式
- 陣列常用函式彙總陣列函式
- PHP字串函式大彙總PHP字串函式
- PHP部分字串函式彙總PHP字串函式
- WordPress模板常用函式彙總函式
- mysql常用函式彙總(分享)MySql函式
- ORA-600 函式彙總函式
- MySQL視窗函式彙總MySql函式
- Mysql 常用函式(1)- 常用函式彙總MySql函式
- OVER(PARTITION BY)函式用法函式
- 輸出輸入函式彙總函式
- PHP網路操作函式彙總PHP函式
- SAP 的CALL FUNCTION 函式彙總Function函式
- MySQL1:MySQL函式彙總MySql函式
- Java 實現彙總排序Java排序
- Oracle 中分析函式用法之--rank(),dense_rank(),partition,over()Oracle函式
- oracle的分析函式over(Partition by...) 及開窗函式Oracle函式
- php常用函式彙總.以備查.PHP函式