RANK

2024-12-02 03:48:26  Updated

Purpose

RANK() returns the rank of a value in a group of values. Rows with equal values for the ranking criteria receive the same rank and are considered tied rows. OceanBase Database adds the number of tied rows to the tied rank to calculate the next rank. You can use it as an aggregate or analytic function.

Note

  • As an aggregate function, the RANK() function calculates the rank of a row based on the assumed row identified by the expr parameter in relation to the specified rank specification. The expr expression matches to the expression in the ORDER BY aggregate clause by position. Therefore, the number of arguments must be the same and their types must be compatible.
  • As an analytic function, RANK() calculates the rank of each row returned from the query relative to other rows returned by the query, based on order_by_clause in value_exprs.

Syntax

/*Aggregate syntax*/
RANK(expr [, expr ]...) WITHIN GROUP
  ( ORDER BY
            expr_col [ DESC | ASC ][ NULLS { FIRST | LAST } ]
            [, expr_col [ DESC | ASC ][ NULLS { FIRST | LAST } ]]...
  )

/*Analytic syntax*/
RANK() OVER ([ query_partition_clause ] order_by_clause)

Parameters

Parameter Description
expr The value to query.
expr_col The column name of the value to query.
DESC | ASC Optional. The sorting method of the list.
  • ASC specifies to sort in ascending order. It is the default value.
  • DESC specifies to sort in descending order.
NULLS { FIRST | LAST } Optional. The position of NULL in expr_col after sorting.
  • NULLS FIRST indicates that NULLs are sorted before non-NULL values.
  • NULLS LAST indicates that NULLs are sorted after non-NULL values.
OVER You can use the OVER clause to define a window over the data on which the function operates. For more information, see Analytic functions.

Return type

The return type is NUMBER.

Examples

The table emp_msg has been created.

obclient> SELECT * FROM emp_msg;
+--------+--------+------+------+
| DEPTNO | ENAME  | SAL  | MGR  |
+--------+--------+------+------+
|     10 | CLARK  | 2750 | 7839 |
|     10 | KING   | 5300 | NULL |
|     10 | MILLER | 1600 | 7782 |
|     20 | ADAMS  | 1400 | 7788 |
|     20 | FORD   | 3300 | 7566 |
|     20 | JONES  | 3275 | 7839 |
|     20 | SCOTT  | 3300 | 7566 |
|     20 | SMITH  | 1100 | 7902 |
|     30 | ALLEN  | 1900 | 7698 |
|     30 | BLAKE  | 3150 | 7839 |
|     30 | JAMES  | 1250 | 7698 |
|     30 | MARTIN | 1550 | 7698 |
|     30 | TURNER | 1800 | 7698 |
|     30 | WARD   | 1550 | 7698 |
|     30 | SCLARK | 1750 | 7839 |
+--------+--------+------+------+
15 rows in set

Example of an aggregate function

Return the rank of 3300 in the sal column.

obclient> SELECT RANK(3300) WITHIN GROUP (ORDER BY sal) FROM emp_msg;
+-----------------------------------+
| RANK(3300)WITHINGROUP(ORDERBYSAL) |
+-----------------------------------+
|                                13 |
+-----------------------------------+
1 row in set

Example of an analytic function

Group the data records in the table by the deptno column and sort the data records by the sal column in descending order. Return the rank of each value in the sal column.

obclient> SELECT deptno,ename,sal,
                   RANK() OVER (PARTITION BY deptno ORDER BY sal DESC) "RANK"
          FROM emp_msg;
+--------+--------+------+------+
| DEPTNO | ENAME  | SAL  | RANK |
+--------+--------+------+------+
|     10 | KING   | 5300 |    1 |
|     10 | CLARK  | 2750 |    2 |
|     10 | MILLER | 1600 |    3 |
|     20 | FORD   | 3300 |    1 |
|     20 | SCOTT  | 3300 |    1 |
|     20 | JONES  | 3275 |    3 |
|     20 | ADAMS  | 1400 |    4 |
|     20 | SMITH  | 1100 |    5 |
|     30 | BLAKE  | 3150 |    1 |
|     30 | ALLEN  | 1900 |    2 |
|     30 | TURNER | 1800 |    3 |
|     30 | SCLARK | 1750 |    4 |
|     30 | MARTIN | 1550 |    5 |
|     30 | WARD   | 1550 |    5 |
|     30 | JAMES  | 1250 |    7 |
+--------+--------+------+------+
15 rows in set

Contact Us