sql ROW_NUMBER()與OVER()方法案例詳解
語法格式:row_number() over(partition by 分組列 order by 排序列 desc)
row_number() over()分組排序功能:
在使用 row_number() over()函數(shù)時(shí)候,over()里頭的分組以及排序的執(zhí)行晚于 where 、group by、 order by 的執(zhí)行。
例一:
表數(shù)據(jù):
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);
一次排序:對(duì)查詢結(jié)果進(jìn)行排序(無分組)
select id,name,age,salary,row_number()over(order by salary desc) rn from TEST_ROW_NUMBER_OVER t
結(jié)果:

進(jìn)一步排序:根據(jù)id分組排序
select id,name,age,salary,row_number()over(partition by id order by salary desc) rank from TEST_ROW_NUMBER_OVER t
結(jié)果:

再一次排序:找出每一組中序號(hào)為一的數(shù)據(jù)
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
結(jié)果:

排序找出年齡在13歲到16歲數(shù)據(jù),按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'
結(jié)果:結(jié)果中 rank 的序號(hào),其實(shí)就表明了 over(order by salary desc) 是在where age between and 后執(zhí)行的

例二:
1.使用row_number()函數(shù)進(jìn)行編號(hào),如
select email,customerID, ROW_NUMBER() over(order by psd) as rows from QT_Customer
原理:先按psd進(jìn)行排序,排序完后,給每條數(shù)據(jù)進(jìn)行編號(hào)。
2.在訂單中按價(jià)格的升序進(jìn)行排序,并給每條記錄進(jìn)行排序代碼如下:
select DID,customerID,totalPrice,ROW_NUMBER() over(order by totalPrice) as rows from OP_Order
3.統(tǒng)計(jì)出每一個(gè)各戶的所有訂單并按每一個(gè)客戶下的訂單的金額 升序排序,同時(shí)給每一個(gè)客戶的訂單進(jìn)行編號(hào)。這樣就知道每個(gè)客戶下幾單了:
select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order
4.統(tǒng)計(jì)每一個(gè)客戶最近下的訂單是第幾次下的訂單:
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 '下單次數(shù)',customerID from tabs group by customerID
5.統(tǒng)計(jì)每一個(gè)客戶所有的訂單中購買的金額最小,而且并統(tǒng)計(jì)改訂單中,客戶是第幾次購買的:
思路:利用臨時(shí)表來執(zhí)行這一操作。
1.先按客戶進(jìn)行分組,然后按客戶的下單的時(shí)間進(jìn)行排序,并進(jìn)行編號(hào)。
2.然后利用子查詢查找出每一個(gè)客戶購買時(shí)的最小價(jià)格。
3.根據(jù)查找出每一個(gè)客戶的最小價(jià)格來查找相應(yīng)的記錄。
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等開窗函數(shù)時(shí),over里頭的分組及排序的執(zhí)行晚于“where,group by,order by”的執(zhí)行。
select
ROW_NUMBER() over(partition by customerID order by insDT) as rows,
customerID,totalPrice, DID
from OP_Order where insDT>'2011-07-22'
到此這篇關(guān)于sql ROW_NUMBER()與OVER()方法案例詳解的文章就介紹到這了,更多相關(guān)sql ROW_NUMBER()與OVER()方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ubuntu 16.04下mysql5.7.17開放遠(yuǎn)程3306端口
這篇文章主要介紹了ubuntu 16.04下mysql5.7.17開放遠(yuǎn)程3306端口的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的方法
order by排序是最常用的功能,但是排序有時(shí)會(huì)遇到數(shù)據(jù)為空null的情況,這樣排序就會(huì)亂了,這篇文章主要給大家介紹了關(guān)于MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的相關(guān)資料,需要的朋友可以參考下2024-06-06
實(shí)現(xiàn)MySQL與elasticsearch的數(shù)據(jù)同步的代碼示例
MySQL 自身簡(jiǎn)單、高效、可靠,是又拍云內(nèi)部使用最廣泛的數(shù)據(jù)庫,但是當(dāng)數(shù)據(jù)量達(dá)到一定程度的時(shí)候,對(duì)整個(gè) MySQL 的操作會(huì)變得非常遲緩,這個(gè)時(shí)候我們就需要MySQL與elasticsearch數(shù)據(jù)同步,接下來就給大家介紹如何實(shí)現(xiàn)數(shù)據(jù)同步2023-07-07
MySQL 索引和數(shù)據(jù)表該如何維護(hù)
使用合適的數(shù)據(jù)類型完成數(shù)據(jù)表創(chuàng)建和建立索引后,工作并沒有完結(jié)——你需要去維護(hù)數(shù)據(jù)表和索引以保證它們運(yùn)行良好。數(shù)據(jù)表維護(hù)的主要目的是查找和修復(fù)沖突,維護(hù)精確的索引統(tǒng)計(jì)和減少碎片。2021-05-05

