1.row_number() 排序策略,連續排序,它會為查詢出來的每一行記錄生成一個序號,依次排序且不會重覆,例如1,2,3,4 SELECT names,dept,row_number() OVER(PARTITION BY dept ORDER BY age DESC) rank FROM wo ...
1.row_number() 排序策略,連續排序,它會為查詢出來的每一行記錄生成一個序號,依次排序且不會重覆,例如1,2,3,4
SELECT names,dept,row_number() OVER(PARTITION BY dept ORDER BY age DESC) rank FROM workers;
2.dense_rank() 排序策略,連續排序,如果有兩個同一級別時,接下來是第二級別 ,例如1,2,2,3
select names,dept,dense_rank() over(partition by dept order by age desc) rank from workers;
結果如下圖
3.rank() 排序策略, 跳躍排序,如果有兩個同一級別時,接下來是第三級別,例如1,2,2,4
select names,dept,rank() over(partition by dept order by age desc) rank from workers;
結果如下圖
WITH workers AS (
SELECT 'DOMA' dept ,'zhangsan' names,23 age,4000 salaries FROM dual
UNION ALL
SELECT 'DOMA' dept ,'lisi' names,35 age,9000 salaries FROM dual
union all
SELECT 'DOMB' dept ,'wangwu' names,26 age,6500 salaries FROM dual
UNION ALL
SELECT 'DOMB' dept ,'zhaoliu' names,28 age,7000 salaries FROM dual
UNION ALL
SELECT 'DOMB' dept ,'maqi' names,26 age,6000 salaries FROM dual
UNION ALL
SELECT 'DOMA' dept ,'fengba' names,25 age,6500 salaries FROM dual
UNION ALL
SELECT 'DOMB' dept ,'sujiu' names,25 age,7000 salaries FROM dual
)
--row_number() 排序策略,連續排序,它會為查詢出來的每一行記錄生成一個序號,依次排序且不會重覆,例如1,2,3,4
SELECT names,dept,row_number() OVER(PARTITION BY dept ORDER BY age DESC) rank FROM workers;
--dense_rank() 連續排序,如果有兩個同一級別時,接下來是第二級別
select names,dept,dense_rank() over(partition by dept order by age desc) rank from workers;
--rank() 跳躍排序,如果有兩個第一級別時,接下來是第三級別
select names,dept,rank() over(partition by dept order by age desc) rank from workers;
關於Parttion by:
Parttion by關鍵字是Oracle中分析性函數的一部分,用於給結果集進行分區。它和聚合函數Group by不同的地方在於它只是將原始數據進行名次排列,
能夠返回一個分組中的多條記錄(記錄數不變),而Group by是對原始數據進行聚合統計,一般只有一條反映統計值的結果(每組返回一條)。