zoukankan      html  css  js  c++  java
  • [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 
    ;

  • 相关阅读:
    Vivian's Problem UVA
    Prime k-tuple UVA
    M
    Make a Crystal UVA
    Cryptography Reloaded UVALive
    Gauss Prime UVA
    Discrete Square Roots UVALive
    The Bells are Ringing UVALive
    Huge Mods UVA
    指数循环节
  • 原文地址:https://www.cnblogs.com/x-you/p/14850348.html
Copyright © 2011-2022 走看看