分析函式系列之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函式
- ROWNUMBER() OVER( PARTITION BY COL1
- 常用函式彙總函式
- 【彙總】scapy 函式函式
- Oracle函式彙總Oracle函式
- string 函式彙總函式
- SQL-ROWNUMBER-OVER彙總SQL
- MySQL常用函式彙總MySql函式
- 開窗函式彙總函式
- AA常用函式彙總函式
- PHP字串函式彙總PHP字串函式
- PHP 常用函式彙總PHP函式
- Excel 特殊分組彙總示例Excel
- MySQL視窗函式彙總MySql函式
- PHP陣列函式彙總PHP陣列函式
- 陣列常用函式彙總陣列函式
- Mysql 常用函式(1)- 常用函式彙總MySql函式
- sql(oracle)資料處理實用總結開窗函式(over partition)使用SQLOracle函式
- Openmp Runtime 庫函式彙總(上)函式
- 輸出輸入函式彙總函式
- Java 實現彙總排序Java排序
- php常用函式彙總.以備查.PHP函式
- MySQL日期和時間函式彙總MySql函式
- Android 系列教程 彙總Android
- Openmp Runtime 庫函式彙總(下)——深入剖析鎖?原理與實現函式
- Vagrant box 命令彙總彙總
- Leetcode刷題系列彙總LeetCode
- 前端模組化彙總前端
- Linux 效能分析工具彙總Linux
- 實用工具彙總
- hive常用的一些高階函式彙總Hive函式
- ORACLE中日期和時間函式彙總(轉載)Oracle函式
- 【彙總】EMQX 函式API、安裝與使用說明MQ函式API
- ThinkPHP6.0 核心分析彙總PHP
- python 系列文章彙總(持續更新…)Python
- 資料分析中常用小函式彙總【持續更新,個人筆記。。。】函式筆記
- jquery彙總jQuery