rank
rank, function
def process rank(): number
def process rank(n: number, g: _typ, s: number): number
Returns ranks starting at 1. The overload without arguments relies on a scan
order, while the three-argument overload produces a prioritized ordering based
on n (priority), g (group), and s (within-group order).
Examples
table T = with
[| as N |]
[| 10 |]
[| 5 |]
[| 7 |]
T.Rk = rank() scan T.N
show table "Rank by scan" with
T.N
T.Rk
This outputs the following table:
| N | Rk |
|---|---|
| 10 | 3 |
| 5 | 1 |
| 7 | 2 |
table U = with
[| as N, as G |]
[| 4, "a" |]
[| 1, "a" |]
[| 3, "b" |]
[| 2, "b" |]
U.Rk = rank() by U.G scan U.N
show table "Rank by group" with
U.G
U.N
U.Rk
This outputs the following table:
| G | N | Rk |
|---|---|---|
| a | 4 | 2 |
| a | 1 | 1 |
| b | 3 | 2 |
| b | 2 | 1 |
table V = with
[| as N, as G, as S |]
[| 3, "a", 1 |]
[| 1, "a", 0 |]
[| 4, "b", 2 |]
[| 2, "b", 0 |]
V.Rk = rank(V.N, V.G, V.S)
show table "Prioritized rank" with
V.G
V.N
V.S
V.Rk
This outputs the following table:
| G | N | S | Rk |
|---|---|---|---|
| a | 3 | 1 | 2 |
| a | 1 | 0 | 4 |
| b | 4 | 2 | 1 |
| b | 2 | 0 | 3 |
Remarks
When values are tied, tie-breaks are arbitrary.