ROW_NUMBER() OVER函式的基本用法

thamsyangsw發表於2014-04-21

語法: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排序,而此函式計算的值就表示每組內部排序後的順序編號(組內連續的唯一的)

例項:

初始化資料

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)

資料顯示為

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.00

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

預期結果:

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

 

轉自:http://www.cnblogs.com/digjim/archive/2006/09/20/509344.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26613085/viewspace-1145927/,如需轉載,請註明出處,否則將追究法律責任。

相關文章