ROW_NUMBER() OVER函數的基本用法

語法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)函數

簡單的說row_number()從1開始,爲每一條分組記錄返回一個數字,這裏的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再爲降序之後的沒條xlh記錄返回一個序號。 
示例: 
xlh           row_num 
1700              1 
1500              2 
1085              3 
710                4排序

row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根據COL1分組,在分組內部根據 COL2排序,而此函數計算的值就表示每組內部排序後的順序編號(組內連續的惟一的)ci

實例:it

初始化數據io

create table employee (empid int ,deptid int ,salary decimal(10,2))
insert into employee values(1,10,5500.00)
insert into employee values(2,10,4500.00)
insert into employee values(3,20,1900.00)
insert into employee values(4,20,4800.00)
insert into employee values(5,40,6500.00)
insert into employee values(6,40,14500.00)
insert into employee values(7,40,44500.00)
insert into employee values(8,50,6500.00)
insert into employee values(9,50,7500.00)table

數據顯示爲語法

empid       deptid      salary
----------- ----------- ---------------------------------------
1           10          5500.00
2           10          4500.00
3           20          1900.00
4           20          4800.00
5           40          6500.00
6           40          14500.00
7           40          44500.00
8           50          6500.00
9           50          7500.00im

需求:根據部門分組,顯示每一個部門的工資等級數據

預期結果:腳本

empid       deptid      salary                                  rank
----------- ----------- --------------------------------------- --------------------
1           10          5500.00                                 1
2           10          4500.00                                 2
4           20          4800.00                                 1
3           20          1900.00                                 2
7           40          44500.00                                1
6           40          14500.00                                2
5           40          6500.00                                 3
9           50          7500.00                                 1
8           50          6500.00                                 2

SQL腳本:

SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee

相關文章
相關標籤/搜索