MYSQL row_number()与over()函数用法详解
语法格式:row_number() over(partition by 分组列 order by 排序列 desc)
row_number() over()分组排序功能:
在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where 、group by、 order by 的执行。
例一:
表数据:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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); |
一次排序:对查询结果进行排序(无分组)
?1 2 | select id, name ,age,salary,row_number()over( order by salary desc ) rn from test_row_number_over t |
结果:
进一步排序:根据id分组排序
?1 2 | select id, name ,age,salary,row_number()over(partition by id order by salary desc ) rank from test_row_number_over t |
结果:
再一次排序:找出每一组中序号为一的数据
?1 2 3 | 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排序
?1 2 | 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()函数进行编号,如
?1 | select email,customerid, row_number() over( order by psd) as rows from qt_customer |
原理:先按psd进行排序,排序完后,给每条数据进行编号。
2.在订单中按价格的升序进行排序,并给每条记录进行排序代码如下:
?1 | select did,customerid,totalprice,row_number() over( order by totalprice) as rows from op_order |
3.统计出每一个各户的所有订单并按每一个客户下的订单的金额 升序排序,同时给每一个客户的订单进行编号。这样就知道每个客户下几单了:
?1 2 | select row_number() over(partition by customerid order by totalprice) as rows ,customerid,totalprice, did from op_order |
4.统计每一个客户最近下的订单是第几次下的订单:
?1 2 3 4 5 6 7 | 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.根据查找出每一个客户的最小价格来查找相应的记录。
?1 2 3 4 5 6 7 8 9 10 | 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来查询客户第一次下的订单记录。
?1 2 3 4 5 6 | 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”的执行。
?1 2 3 4 | select row_number() over(partition by customerid order by insdt) as rows , customerid,totalprice, did from op_order where insdt> '2011-07-22' |
到此这篇关于mysql row_number()与over()函数用法详解的文章就介绍到这了,更多相关mysql row_number()与over()函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_25221835/article/details/82762416
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。