Outdated Version

You are viewing an older version of this section. View current production version.

RANK

Ranking function. Returns the rank of the current row within the partition as specified by the order by clause. If two or more rows tie they have the same ranking.

Syntax

RANK () OVER (
    [PARTITION BY (col | expr), ...] 
    [ORDER BY (col | expr), ...]
)

Return Type

An integer

Example

The following example demonstrates how the partition clause groups results and then rank is applied to those groups. Order by orders the results.

memsql> create table t (a int, b int);

memsql> insert into t values(1,1), (1,2), (3,2), (3,3), (3,-4);

memsql> select a,b, rank() over (partition by a order by b) from t;
+------+------+-----------------------------------------+
| a    | b    | rank() over (partition by a order by b) |
+------+------+-----------------------------------------+
|    1 |    1 |                                       1 |
|    1 |    2 |                                       2 |
|    3 |   -4 |                                       1 |
|    3 |    2 |                                       2 |
|    3 |    3 |                                       3 |
+------+------+-----------------------------------------+
5 rows in set (0.00 sec)