語法格式:row_number() over(partition by 分組列 order by 排序列 desc)row_number() over()分組排序功能:
在使用 row_number() over()函式時候,over()裡頭的分組以及排序的執行晚於 where 、group by、 order by 的執行。
例一:表資料:
create table test_row_number_over(
id varchar(10) not null,
name varchar(10) null,
age varchar(10) null,
salary int null
);select * from test_row_number_over t;
insert into test_row_number_over(id,name,age,salary) values(1,'a',10,8000);
insert into test_row_number_over(id,name,age,salary) values(1,'a2',11,6500);
insert into test_row_number_over(id,name,age,salary) values(2,'b',12,13000);
insert into test_row_number_over(id,name,age,salary) values(2,'b2',13,4500);
insert into test_row_number_over(id,name,age,salary) values(3,'c',14,3000);
insert into test_row_number_over(id,name,age,salary) values(3,'c2',15,20000);
insert into test_row_number_over(id,name,age,salary) values(4,'d',16,30000);
insert into test_row_number_over(id,name,age,salary) values(5,'d2',17,1800);
一次排序:對查詢結果進行排序(無分組)
select id,name,age,salary,row_number()over(order by salary desc) rn
from test_row_number_over t
結果:進一步排序:根據id分組排序
select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from test_row_number_over t
結果: 再一次排序:找出每一組中序號為一的資料
select * from(select id,name,age,salary,row_number()over(partition by id order by salary desc) rank
from test_row_number_over t)
where rank <2
結果:排序找出年齡在13歲到16歲資料,按salary排序
select id,name,age,salary,row_number()over(order by salary desc) rank
from test_row_number_over t where age between '13' and '16'
結果:結果中 rank 的序號,其實就表明了 over(order by salary desc) 是在where age between and 後執行的
例二:1.使用row_number()函式進行編號,如
select email,customerid, row_number() over(order by psd) as rows from qt_customer
原理:先按psd進行排序,排序完後,給每條資料進行編號。
2.在訂單中按**的公升序進行排序,並給每條記錄進行排序**如下:
select did,customerid,totalprice,row_number() over(order by totalprice) as rows from op_order
3.統計出每乙個各戶的所有訂單並按每乙個客戶下的訂單的金額 公升序排序,同時給每乙個客戶的訂單進行編號。這樣就知道每個客戶下幾單了:
select row_number() over(partition by customerid order by totalprice)
as rows,customerid,totalprice, did from op_order
4.統計每乙個客戶最近下的訂單是第幾次下的訂單:
with tabs as
( select row_number() over(partition by customerid order by totalprice)
as rows,customerid,totalprice, did from op_order
) select max(rows) as '下單次數',customerid from tabs
group by customerid
5.統計每乙個客戶所有的訂單中購買的金額最小,而且並統計改訂單中,客戶是第幾次購買的:
思路:利用臨時表來執行這一操作。
1.先按客戶進行分組,然後按客戶的下單的時間進行排序,並進行編號。
2.然後利用子查詢查詢出每乙個客戶購買時的最小**。
3.根據查詢出每乙個客戶的最小**來查詢相應的記錄。
with tabs as
( select row_number() over(partition by customerid order by insdt)
as rows,customerid,totalprice, did from op_order
) select * from tabs
where totalprice in
( select min(totalprice)from tabs group by customerid
) 6.篩選出客戶第一次下的訂單。
思路。利用rows=1來查詢客戶第一次下的訂單記錄。
with tabs as
( select row_number() over(partition by customerid order by insdt) as rows,* from op_order
) select * from tabs where rows = 1
select * from op_order
7.注意:在使用over等開窗函式時,over裡頭的分組及排序的執行晚於「where,group by,order by」的執行。
select
row_number() over(partition by customerid order by insdt) as rows,
customerid,totalprice, did
from op_order where insdt>'2011-07-22'
————————————————
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分組,在分組...