sql(oracle)資料處理實用總結開窗函式(over partition)使用
1.開窗函式over partition
![11188611-4b85e49e9e4657d6.png](https://i.iter01.com/images/45c6071cc7609c090c7a6697fc5a5ab773e94741fefeda9374d43bb60c9ade0c.png)
開窗函式使用於取出多列分組,取一列分組下另一組前幾名,先利用開窗函式對其分組排名,開窗函式排名函式較多使用row_number(),還有rank()等,生成排名列之後將結果集篩選其排名前幾<=2或者你想要的前幾名。
上述例子sql:
select test_name,test_id,n_rank,test_salary_sum from ( select t_alias.test_name,t_alias.test_id, sum(t_alias.test_salary) test_salary_sum, row_number() over(partition by t_alias.test_name order by sum(t_alias.test_salary) desc) n_rank from (select 1 test_id, 'name_1' test_name, 100 test_salary from dual union all select 2 test_id, 'name_1' test_name, 200 test_salary from dual union all select 2 test_id, 'name_1' test_name, 300 test_salary from dual union all select 3 test_id, 'name_1' test_name, 300 test_salary from dual union all select 4 test_id, 'name_1' test_name, 400 test_salary from dual union all select 1 test_id, 'name_2' test_name, 200 test_salary from dual union all select 2 test_id, 'name_2' test_name, 200 test_salary from dual union all select 2 test_id, 'name_2' test_name, 200 test_salary from dual union all select 3 test_id, 'name_2' test_name, 400 test_salary from dual union all select 4 test_id, 'name_2' test_name, 400 test_salary from dual) t_alias group by t_alias.test_name,t_alias.test_id) where n_rank<=2 order by test_name,n_rank;
上述執行結果:
![11188611-476b25d87a378661.png](https://i.iter01.com/images/66cdeb71c553a28dff822f2cafa9604659b6ac32468e66f3ae3e2e8afc39ada4.png)
獲得每個test_name欄位下test_id的test_salary的前2名。
開窗函式例項:
![11188611-1d5cca78c0deeb8b.png](https://i.iter01.com/images/ce7b6bdbddc8d1811570090dea17cd1fef23d2f5faac9c80fd851543ca54a3ca.png)
另有開窗函式替代寫法,可以瞭解:
![11188611-d7ee7525ae170858.png](https://i.iter01.com/images/d814fa7903c9c657eb756c5534e00f16a0599e11e965f9314b768c449bad54da.png)
相關文章
- Sql Server資料庫開窗函式Over()的使用例項詳解SQLServer資料庫函式
- Oracle分析函式之開窗函式over()詳解Oracle函式
- 【SQL】Lag/Rank/Over視窗函式揭秘,資料分析之旅SQL函式
- Spark Streaming--開窗函式over()Spark函式
- Flink處理函式實戰之四:視窗處理函式
- Spark SQL 開窗函式SparkSQL函式
- 開窗函式彙總函式
- 11. 使用MySQL之使用資料處理函式MySql函式
- MySQL視窗函式用法總結MySql函式
- SQL 視窗函式SQL函式
- Oracle Partition 分割槽詳細總結Oracle
- Oracle SQL處理OracleSQL
- Oracle分析函式與視窗函式Oracle函式
- 【SQL】Oracle SQL處理的流程SQLOracle
- MySQL-日期和資料處理函式MySql函式
- echarts 繫結事件處理函式Echarts事件函式
- Clickhouse SQL日期處理函式及案例分享SQL函式
- 與SQL視窗函式相同SQL函式
- (特徵工程實戰)ML最實用的資料預處理與特徵工程常用函式!特徵工程函式
- SQL視窗分析函式使用詳解系列三之偏移量類視窗函式SQL函式
- 分析函式系列之sum(col1) over(partition by col2 order by col3):實現分組彙總或遞增彙總函式
- Spark SQL學習——UDF、UDAF和開窗函式SparkSQL函式
- Pandas 基礎 (6) - 用 replace () 函式處理不合理資料函式
- 使用SQL以及函式等做資料分析SQL函式
- 使用自定義函式實現資料編解碼、格式處理與業務告警函式
- SQL中的常用的字串處理函式大全SQL字串函式
- ROWNUMBER() OVER( PARTITION BY COL1
- SQL-ROWNUMBER-OVER彙總SQL
- Oracle函式彙總Oracle函式
- SparkSQL開窗函式SparkSQL函式
- SparkSQL 開窗函式SparkSQL函式
- Spark 開窗函式Spark函式
- MySQL視窗函式彙總MySql函式
- CnosDB:深入瞭解時序資料處理函式函式
- 【SQL】18 SQL NULL 函式、SQL 通用資料型別、SQL 用於各種資料庫的資料型別SQLNull函式資料型別資料庫
- 詳解SQL操作的視窗函式SQL函式
- mysql常用函式例項總結---這篇文章好在有例項資料表,可直接上機使用【聚集函式、字串、數值、時間日期處理等MySql函式字串
- Emgucv使用中常用函式總結函式