rank
AQL Window Function: A window function that returns the rank of rows within a partition of a table. Tie values are assigned the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...
Knowledge Checkpoint
Readings that will help you understand this documentation better:
Definition
A window function that returns the rank of rows within a partition of a table. Tie values are assigned the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...
To get consecutive rank, use dense_rank.
Syntax
rank(order: order_expr)
rank(order: order_expr, ..., partition: partition_expr, ...)rank(order: count(orders.id) | desc())
rank(order: count(orders.id), order: average(users.age))
// with partition
rank(order: count(orders.id), partition: orders.status)
// Axis-aware examples
rank(order: 'rows') // Rank by row/x-axis order
rank(order: 'columns' | desc()) // Rank by column/legend order (descending)
rank(order: revenue | desc(), partition: 'rows') // Rank within each rowInput
order(required, repeatable): A field that is used for ordering. The order defaults to ascending. The order can be set explicitly withasc()ordesc(). You can also use axis references:'rows'or'x_axis': Order by dimensions mapped to rows/x-axis'columns'or'legend': Order by dimensions mapped to columns/legend- E.g.
rank(order: count(orders.id) | desc())orrank(order: 'rows')
partition(repeatable, optional): A field that is used for partitioning the table. You can use axis references like'rows','columns','x_axis', or'legend'. E.g.rank(order: count(orders.id), partition: orders.status)orrank(order: revenue, partition: 'rows')
Output
Rank of the current row within its partition (if no partition is specified, the whole table is considered as a single partition). If two rows are tied for a rank, each tied rows receives the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...
Sample Usages
explore {
dimensions {
users.gender, users.age_group
}
measures {
count(orders.id),
_rank: rank(order: count(orders.id) | desc())
}
}| users->gender | users->age_group | count_orders->id | _rank |
|---|---|---|---|
| f | Young Adult | 743 | 1 |
| f | Teen | 640 | 2 |
| m | Young Adult | 356 | 3 |
| m | Teen | 286 | 4 |
| f | Mature | 50 | 5 |
| m | Mature | 24 | 6 |
explore {
dimensions {
users.gender, users.age_group
}
measures {
count(orders.id),
_rank: rank(order: count(orders.id) | desc(), partition: users.gender)
}
}| users->gender | users->age_group | count_orders->id | _rank |
|---|---|---|---|
| f | Young Adult | 743 | 1 |
| f | Teen | 640 | 2 |
| f | Mature | 50 | 3 |
| m | Young Adult | 356 | 1 |
| m | Teen | 286 | 2 |
| m | Mature | 24 | 3 |
Using rank in a dimension definition is straight-forward. Remember that the rank is calculated against all rows in the model.
dimension ranking_by_age {
label: 'Rank By Age'
type: 'number'
hidden: false
description: ''
definition: @aql rank(order: users.age | desc()) ;;
}You can see that users with the same age are assigned the same rank
explore {
dimensions {
users.age,
users.ranking_by_age
}
}| users->age | users->ranking_by_age |
|---|---|
| 23 | 399 |
| 39 | 2 |
| 26 | 201 |
| 34 | 22 |
| 22 | 505 |
| 19 | 963 |
| 37 | 6 |
| 33 | 27 |
| 31 | 51 |
| 27 | 160 |
| 20 | 775 |
| 21 | 623 |
| 18 | 1165 |
| 24 | 310 |
| 16 | 1634 |
| 36 | 10 |
| 38 | 4 |
| 32 | 38 |
| 25 | 247 |
| 17 | 1398 |
| 40 | 1 |
| 28 | 114 |
| 35 | 15 |
| 29 | 83 |
| 30 | 68 |
You can also use it in filter() to filter out the top 10 users by age:
users
| select(users.full_name, _rank: rank(order: users.age))
| filter(_rank == 1)| users->full_name | _rank |
|---|---|
| Bacolor Hong | 1 |
| Vea Maybelle | 1 |
| Beattie Chana | 1 |
| Tudman Kelly | 1 |
| Paplow Dino | 1 |
| Schepp Guan | 1 |
| Sinkovich Tyron | 1 |
| Westler Mark | 1 |
| Goldberg Hermila | 1 |
| Chatagnier Sondra | 1 |
| Stepney Vuong | 1 |
| Balcerzak Syed | 1 |
| Madsen Gomez | 1 |
| Vankammen Tressa | 1 |
| Floras Milissa | 1 |
| Olkowski Easter | 1 |
| Gunsolus Sabra | 1 |
| Leas Deb | 1 |
| Carlone Saito | 1 |
| Baptista Cherie | 1 |
| Lieser Seki | 1 |
| Miss Brice | 1 |
| Doyer Bishop | 1 |
| Nevala Hang | 1 |
| Muszynski Sheilah | 1 |
| Malatesta Kubo | 1 |
| Villenas Willard | 1 |
| Danser Raguel | 1 |
| Houseman Verda | 1 |
| Kuney Jerilyn | 1 |
| Anecelle Roland | 1 |
| Bauknight Mai | 1 |
| Frericks Takagi | 1 |
| Noonkester Pacheco | 1 |
| Friess Euna | 1 |
| Zakutney Hermelinda | 1 |
| Burchill Elliott | 1 |
| Dooney Odelia | 1 |
| Zawislak Narayan | 1 |
| Kernighan Angel | 1 |
| Muschaweck Hannelore | 1 |
| Castellaneta Arnold | 1 |
| Shannonhouse Renae | 1 |
| Plott Rose | 1 |
| Kuechler Nelle | 1 |
| Gurrero Kawashima | 1 |
| Corigliano Josphine | 1 |
| Fauske Matsuura | 1 |
| Borsh Carmella | 1 |
| Rout Pablo | 1 |
| Berray Darrel | 1 |
| Treglia Melissia | 1 |
| Lanter Gilbert | 1 |
| Venskoske Julietta | 1 |
| Gulisano Brady | 1 |
| Leighty Kelley | 1 |
| Vandale Maggie | 1 |
| Werderman Florene | 1 |
| Dolinsky Eugena | 1 |
| Ped Emmett | 1 |
| Whitehill Mui | 1 |
| Ruple Evelia | 1 |
| Sulloway Cammie | 1 |
| Prieur Nick | 1 |
| Goetze Allison | 1 |
| Bonnet Otilia | 1 |
| Drey Elfreda | 1 |
| Coggeshall Prom | 1 |
| Birdsey Choudhry | 1 |
| Aldape Courtney | 1 |
| Raisley Petrina | 1 |
| Pentek Reina | 1 |
| Mizwicki Genevie | 1 |
| Kunimitsu Daniel | 1 |
| Shollenbarger Cruz | 1 |
| Liebler Chawla | 1 |
| Lehrer Margarito | 1 |
See also
dense_rank(): same value gets same rank, no gapspercent_rank()ntile()- Window Functions
Window functions
AQL window functions provide ways to do secondary calculations like navigating (previous, next), ranking, or aggregating (sum, avg, etc.) across a subset of rows of a table. These are analogous to SQL window functions.
dense_rank
AQL Window Function: A window function that returns the rank of rows within a partition of a table. Tie values are assigned the same rank. The next rank in the sequence is consecutive. E.g. 1, 1, 2, 3, 3, 4, ...