SQL Server中row_number分页查询的用法详解
作者:无恨星晨 时间:2024-01-21 20:15:01
ROW_NUMBER()函数将针对SELECT语句返回的每一行,从1开始编号,赋予其连续的编号。在查询时应用了一个排序标准后,只有通过编号才能够保证其顺序是一致的,当使用ROW_NUMBER函数时,也需要专门一列用于预先排序以便于进行编号。
ROW_NUMBER()
说明:返回结果集分区内行的序列号,每个分区的第一行从1开始。
语法:ROW_NUMBER () OVER ([ <partition_by_clause> ] <order_by_clause>) 。
备注:ORDER BY 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。
参数:<partition_by_clause> :将 FROM 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。
<order_by_clause>:确定将 ROW_NUMBER 值分配给分区中的行的顺序。
返回类型:bigint 。
ROW_NUMBER()常用的几种情况
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.统计每一个客户所有的订单中购买的金额最小,而且并统计改订单中,客户是第几次购买的。
如图:
上图:rows表示客户是第几次购买。
思路:利用临时表来执行这一操作。
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.rows_number()可用于分页
思路:先把所有的产品筛选出来,然后对这些产品进行编号。然后在where子句中进行过滤。
实例
--分页存储过程
create proc usp_GetMyPhotos
@pageIndex int, --当前页码
@pageSize int, --每页多少条
@pageCount int output --计算 总共多少页
as
declare @count int --总共多少条
select @count =COUNT(*) from Photos
set @pageCount = CEILING( @count*1.0/@pageSize)
select * from
(select *,ROW_NUMBER() over(order by pid desc) as num
from Photos) as t
where num between @pageSize*(@pageIndex-1) + 1 and @pageSize*@pageIndex
8.在使用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'
以上代码是先执行where子句,执行完后,再给每一条记录进行编号。
来源:https://www.cnblogs.com/weixing/p/5460697.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python OpenCV超详细讲解调整大小与图像操作的实现
![](https://img.aspxhome.com/file/2023/2/105582_0s.png)
python基础之文件操作
![](https://img.aspxhome.com/file/2023/9/112149_0s.png)
Python打印“菱形”星号代码方法
![](https://img.aspxhome.com/file/2023/5/73005_0s.png)
python+django加载静态网页模板解析
![](https://img.aspxhome.com/file/2023/4/97754_0s.png)
Mootools 1.2教程(11)——Fx.Morph、Fx选项和Fx事件
使用python将excel数据导入数据库过程详解
Python 玩转图像格式转换操作
![](https://img.aspxhome.com/file/2023/5/83945_0s.jpg)
Sublime Text3快捷键汇总
Python打包后的exe还原成.py的实现步骤
![](https://img.aspxhome.com/file/2023/6/112166_0s.png)
Python性能测试工具Locust安装及使用
Python使用urllib2模块抓取HTML页面资源的实例分享
零基础写python爬虫之抓取糗事百科代码分享
![](https://img.aspxhome.com/file/2023/6/134436_0s.png)
防止web项目中的SQL注入
vue使用pdf.js预览pdf文件的方法
![](https://img.aspxhome.com/file/2023/1/133011_0s.jpg)
Pandas对每个分组应用apply函数的实现
![](https://img.aspxhome.com/file/2023/5/78775_0s.jpg)
浅谈python中拼接路径os.path.join斜杠的问题
如何利用Python解析超大的json数据(GB级别)
![](https://img.aspxhome.com/file/2023/9/135069_0s.jpg)
centos7上mysql8.0rpm方式安装教程图解
![](https://img.aspxhome.com/file/2023/1/121311_0s.png)
Python+OpenCV 图像边缘检测四种实现方法
![](https://img.aspxhome.com/file/2023/1/128331_0s.jpg)
Django自定义插件实现网站登录验证码功能
![](https://img.aspxhome.com/file/2023/1/80691_0s.jpg)