zoukankan      html  css  js  c++  java
  • rank() within group用法【转】

     参考:http://www.itpub.net/thread-241824-1-1.html

        http://blog.itpub.net/13379967/viewspace-481811/

    select rank(1500) within group (order by salary desc) "rank of 1500" from employees;
    
    实际得到的结果就是:
    如果存在一条记录,这条记录的salary字段值为1500。
    那么将该条记录插入employees表中后,按照salary字段降序排列后,该条记录的序号为多少?
    比如原表employees内容如下
    SQL> select * from employees;
    
    EMP_ID     EMP_NAME         SALARY
    ---------- -------------------- ----------
    10001      ZhangSan             500
    10002      LiSi                 1000
    10003      WangWu               1500
    10004      MaLiu                2000
    10005      NiuQi                2500
    
    则如果一个员工的薪水为1500,那么该员工在员工表中的薪水排名应与WangWu相同,并列排名第三。
    通过聚合函数RANK() WITHIN GROUP验证一下:
    SQL> select rank(1500) within group (order by salary) as "rank number" from employees;
    rank number
    -----------
              3
    
    若原表内容如下
    SQL> select * from employees;
    
    EMP_ID     EMP_NAME         SALARY
    ---------- -------------------- --------------
    10001      ZhangSan             500
    10004      MaLiu                2000
    10005      NiuQi                2500
    
    则排名应为第2,验证如下
    SQL> select rank(1500) within group (order by salary) as "rank number" from employees;
    rank number
    -----------
              2
    合计功能:计算出数值(4,1)在Orade By Col1,Col2排序下的排序值,也就是col1=4,col2=1在排序以后的位置
      
      SELECT RANK(4,1) WITHIN GROUP (ORDER BY col1,col2) "Rank" FROM table;
      
      结果如下:
      Rank
      4
     
    通过以上方法,得出col1为4,col2为1的那行数据的rank排名为多少
  • 相关阅读:
    Groovy 设计模式 -- null对象模式
    Groovy 设计模式 -- 借贷
    Groovy 设计模式 -- 抽象工厂 模式
    Groovy 设计模式 -- Strategy 模式
    Groovy 设计模式 -- proxy & delegate
    Groovy 类名称赋值为变量使用(newInstance & new)
    yaml
    REST POST PUT差别
    Continuous Design
    通配符 Globbing赏析
  • 原文地址:https://www.cnblogs.com/willspring/p/5818826.html
Copyright © 2011-2022 走看看