rank

rank, function

def process rank(): number
def call rank(score: any, groups: grouping, sequence: ordering): 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 score (priority), groups, and sequence (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
table Items[id] = with
  [| as id, as Score, as Category |]
  [| "1", 10, "a" |]
  [| "2", 20, "a" |]
  [| "3", 30, "b" |]
  [| "4", 15, "b" |]

Items.Rank = rank(Items.Score, Items.Category, id)

show table "Ranks" with
  id
  Items.Score
  Items.Category
  Items.Rank

This outputs the following table:

id Score Category Rank
1 10 a 4
2 20 a 1
3 30 b 3
4 15 b 2

Remarks

When values are tied, tie-breaks are arbitrary. The sequence argument can be a primary dimension such as id.

User Contributed Notes
0 notes + add a note