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