首页 > 数据库技术 > 详细

[SQL] 用标准SQL实现 DENSE_RANK() 和 RANK() 函数

时间:2021-06-04 17:52:51      阅读:32      评论:0      收藏:0      [点我收藏+]

先看 DENSE_RANK() 和 RANK() 函数用处:

DENSE_RANK() : 排序,不跳过重复位次。

RANK() : 排序,跳过重复位次。

create table test_order(
 name varchar2(100),    -- 名称
 id   int               -- 加入编号
);

insert into test_order values(CAA, 100);
insert into test_order values(CAB, 80);
insert into test_order values(CAC, 80);
insert into test_order values(CAD, 70);
insert into test_order values(CAE, 60);
commit ; 


select name, id, rank() over(order by id), dense_rank() over(order by id )
from test_order ;

技术分享图片

 

 

使用标准SQL实现:

select name
       ,id
       ,(select count(id) from test_order b where a.id > b.id) + 1          as DENSE_RANK
       ,(select count(distinct id) from test_order b where a.id > b.id) + 1 as RANK
from test_order a 
order by id 
;

技术分享图片

 

[SQL] 用标准SQL实现 DENSE_RANK() 和 RANK() 函数

原文:https://www.cnblogs.com/x-you/p/14850348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!