码迷,mamicode.com
首页 > 数据库 > 详细

【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例

时间:2019-04-03 23:48:09      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:table   保留   排列   删除重复数据   oat   16px   序列   datetime   详解   

原文:【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例

 

#用法说明

select row_number() over(partition by A order by B ) as rowIndex from table

 

  A :为分组字段

  B:为分组后的排序字段。

  table 表的结构 多为:  多人 多条的相关数据。(比如:订单信息)

  此条sql语句,多用于对数据进行分组排序,并对每个组中的数据分别进行编号,编号从1开始递增,每个组内的编号不会重复;

#经典实例

0、填充数据

 1 create table [OrderInfo](
 2        [Id] [int] PRIMARY KEY  IDENTITY(1,1) NOT NULL,
 3        [UserId] [nvarchar](50) NOT NULL,
 4        [TotalPrice] [float] NOT NULL,
 5        [OrderTime] [datetime] NOT NULL,
 6 );
 7 
 8 INSERT INTO [dbo].[OrderInfo]
 9            ([UserId]
10            ,[TotalPrice]
11            ,[OrderTime])
12      VALUES
13            (N1, 111, CAST(N2011-01-01 AS DateTime)),
14            (N1, 112, CAST(N2011-01-02 AS DateTime)),
15            (N3, 311, CAST(N2013-01-01 AS DateTime)),
16            (N3, 312, CAST(N2013-01-02 AS DateTime)),
17            (N2, 211, CAST(N2012-01-01 AS DateTime)),
18            (N2, 212, CAST(N2012-01-02 AS DateTime)),
19            (N1, 113, CAST(N2011-01-03 AS DateTime)),
20            (N2, 213, CAST(N2012-01-03 AS DateTime)),
21            (N3, 313, CAST(N2013-01-03 AS DateTime))
22 GO

1、使用row_number()函数对订单进行编号,按照订单时间倒序。(此需求多用于分页)

1 select Id,UserId,TotalPrice,OrderTime,ROW_NUMBER() over (order by OrderTime desc) as rowIndex from OrderInfo

技术图片

#分页场景:每页3条数据,取第2页

1 with
2 baseDate
3 as
4 (
5     select Id,UserId,TotalPrice,OrderTime,ROW_NUMBER() over (order by OrderTime desc) as rowIndex from OrderInfo
6 )
7 select * from baseDate where rowIndex>3 and rowIndex<7

2、所有订单按照客户进行分组,并按照客户下的订单的金额倒序排列。

1 select Id,UserId,orderTime,ROW_NUMBER() over(partition by UserId order by TotalPrice desc) as rowIndex from OrderInfo

技术图片

3、筛选出客户第一次下的订单。

  思路:利用rowIndex来判断订单是客户第几次下单;

1 with
2 baseDate
3 as
4 (
5     select Id,UserId,TotalPrice,orderTime,ROW_NUMBER() over (partition by UserId order by orderTime) as rowIndex from OrderInfo
6 )
7 select * from baseDate where rowIndex=1

技术图片

4、筛选出客户在‘2011年1月1日之后的第一次下的订单。

  思路:在分组排序之前进行实践筛选;

  注意在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by”的执行。

1 with
2 baseDate
3 as
4 (
5     select Id,UserId,TotalPrice,orderTime,ROW_NUMBER() over (partition by UserId order by orderTime) as rowIndex from OrderInfo
6     where OrderTime>2011-1-1
7 )
8 select * from baseDate where rowIndex=1

技术图片

5、只保留每个客户的最近的一次订单,其余的订单删掉。(常用于删除重复数据)

1 with
2 baseDate
3 as
4 (
5     select Id,UserId,TotalPrice,OrderTime,ROW_NUMBER()over (partition by UserId order by OrderTime desc) as rowIndex from OrderInfo
6 )
7 delete from baseDate where rowIndex <> 1

6、统计每一个客户所有的订单中金额最大,并统计该订单是客户第几次购买;

  思路:

    1)先按照客户进行分组,然后按照客户下单的时间进行正序排列,并编号(rowIndex),生成临时表baseDate;

    2)再按照客户进行分组,然后按照客户下单的金额进行倒序排列,并编号(rowIndex),生成临时表basePrice;

    3)最后取basePrice中编号为1的数据,然后根据id到baseDate中去查,即可;

 1 with
 2 baseDate
 3 as
 4 (
 5     select Id,UserId,TotalPrice,orderTime,ROW_NUMBER() over (partition by UserId order by orderTime) as rowIndex from OrderInfo
 6 ),
 7 basePrice
 8 as
 9 (
10     select Id,UserId,orderTime,ROW_NUMBER() over(partition by UserId order by TotalPrice desc) as rowIndex from OrderInfo
11 )
12 select * from baseDate 
13 where Id in (
14     select Id from basePrice where rowIndex=1
15 )

 技术图片

#图中的rowIndex字段就是该订单是第几次购买;

 

【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例

标签:table   保留   排列   删除重复数据   oat   16px   序列   datetime   详解   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10652413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!