[SQL] 用标准SQL实现 DENSE_RANK() 和 RANK() 函数
先看 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 ;