ROW NUMBER OVER函式的基本用法

2021-09-11 06:02:31 字數 975 閱讀 2293

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)

資料顯示為

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

預期結果:

sql指令碼:

select *, row_number() over (partition by deptid order by salary desc) rank from employee

注:出處:

row number over 分析函式

今天用到了row number over 現在就以知識點的形式總結一下,以便於以後的回顧。參考資料msdn 語法 row number over partition by value expression n order by clause 通過語法可以看出 over裡有兩個引數,partition...

row number over 分析函式

今天用到了row number over 現在就以知識點的形式總結一下,以便於以後的回顧。參考資料msdn 語法 row number over partition by value expression n order by clause 通過語法可以看出 over裡有兩個引數,partition...

ROW NUMBER OVER函式運用

語法 row number over partition by column order by column partition by 相當於資料庫中的group by 說明 row number over partition by col1 order by col2 表示根據col1分組,在分組...