sql(oracle)資料處理實用總結開窗函式(over partition)使用
1.開窗函式over partition
開窗函式使用於取出多列分組,取一列分組下另一組前幾名,先利用開窗函式對其分組排名,開窗函式排名函式較多使用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;
上述執行結果:
獲得每個test_name欄位下test_id的test_salary的前2名。
開窗函式例項:
另有開窗函式替代寫法,可以瞭解:
相關文章
- oracle的分析函式over(Partition by...) 及開窗函式Oracle函式
- 語法:OVER(PARTITION BY)及開窗函式函式
- (轉)Oracle語法之OVER(PARTITION BY)及開窗函式Oracle函式
- Oracle 語法之 OVER (PARTITION BY ..) 及開窗函式 轉載Oracle函式
- Sql Server資料庫開窗函式Over()的使用例項詳解SQLServer資料庫函式
- oracle的分析函式over 及開窗函式Oracle函式
- Oracle分析函式之開窗函式over()詳解Oracle函式
- OVER(PARTITION BY)函式用法函式
- 【函式】oracle視窗函式over()的理解函式Oracle
- Oracle 中 Over() 函式學習總結Oracle函式
- Oracle統計分析函式集,over(partition by..) 的運用Oracle函式
- Oracle over()函式使用Oracle函式
- ORACLE 實用函式總結Oracle函式
- Spark Streaming--開窗函式over()Spark函式
- Oracle 中分析函式用法之--rank(),dense_rank(),partition,over()Oracle函式
- [MYSQL -11]使用函式處理資料MySql函式
- Flink處理函式實戰之四:視窗處理函式
- Spark SQL 開窗函式SparkSQL函式
- Oracle分析函式使用總結Oracle函式
- sum over partition by函式的用法-但是查出來的資料格式不好給前臺用,廢棄函式
- python字串處理函式大總結Python字串函式
- over partition簡單使用
- MySQL視窗函式用法總結MySql函式
- over (partition by)
- oracle函式大全-字串處理函式Oracle函式字串
- Oracle使用over()partition by刪除重複記錄Oracle
- SQL字串處理函式大全SQL字串函式
- SQL Server函式總結SQLServer函式
- Oracle函式-->字元處理Oracle函式字元
- Oracle 函式總結Oracle函式
- 近期處理的Oracle資料庫問題總結Oracle資料庫
- Oracle中利用函式索引處理資料傾斜案例Oracle函式索引
- oracle over函式 詳解(轉)Oracle函式
- SQL 視窗函式SQL函式
- Oracle的pipelined函式實現高效能大資料處理Oracle函式大資料
- 幾個SQL 日期處理函式SQL函式
- SQL Server字串處理函式大全SQLServer字串函式
- 超實用PHP函式總結整理PHP函式