• Oracle/PLSQL: Rank Function


    In Oracle/PLSQL, the rank function returns the rank of a value in a group of values. It is very similar to the dense_rank function. However, the rank function can cause non-consecutive rankings if the tested values are the same. Whereas, the dense_rank function will always result in consecutive rankings.

    The rank function can be used two ways - as an Aggregate function or as an Analytic function.

    Syntax #1 - Used as an Aggregate Function

    As an Aggregate function, the rank returns the rank of a row within a group of rows.

    The syntax for the rank function when used as an Aggregate function is:

    rank( expression1, ... expression_n ) WITHIN GROUP ( ORDER BY expression1, ... expression_n )

    expression1 .. expression_n can be one or more expressions which identify a unique row in the group.

    Note

    There must be the same number of expressions in the first expression list as there is in the ORDER BY clause.

    The expression lists match by position so the data types must be compatible between the expressions in the first expression list as in the ORDER BY clause.

    Applies To

    • Oracle 11g, Oracle 10g, Oracle 9i

    For Example

    select rank(1000, 500) WITHIN GROUP (ORDER BY salary, bonus)
    from employees;

    The SQL statement above would return the rank of an employee with a salary of $1,000 and a bonus of $500 from within the employees table.

    Syntax #2 - Used as an Analytic Function

    As an Analytic function, the rank returns the rank of each row of a query with respective to the other rows.

    The syntax for the rank function when used as an Analytic function is:

    rank() OVER ( [ query_partition_clause] ORDER BY clause )

    Applies To

    • Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

    For Example

    select employee_name, salary,
    rank() OVER (PARTITION BY department ORDER BY salary)
    from employees
    where department = 'Marketing';

    The SQL statement above would return all employees who work in the Marketing department and then calculate a rank for each unique salary in the Marketing department. If two employees had the same salary, the rank function would return the same rank for both employees. However, this will cause a gap in the ranks (ie: non-consecutive ranks). This is quite different from the dense_rank function which generates consecutive rankings.

  • 相关阅读:
    【操作系统】之进程(线程)同步
    POJ1007:DNA排序
    中国炭黑增强轮胎行业盈利动态与供需趋势预测报告2022-2028年
    echarts自定义图例富文本使用
    1.4、计算机网络的定义和分类
    云原生大趋势下的容器化技术现状与发展
    Spring Boot 配置文件总结
    机器学习(三十四):可视化决策树的四个方法
    SAP 生产订单变更管理 OCM Order Changement Management
    bpmnjs开始的时间事件和中间事件的时间
  • 原文地址:https://blog.csdn.net/yuanlnet/article/details/125621874