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, ...
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 consecutive. E.g. 1, 1, 2, 3, 3, 4, ...
To return non-consecutive rank, use rank.
Syntax
dense_rank(order: order_expr)
dense_rank(order: order_expr, ..., partition: partition_expr, ...)dense_rank(order: count(orders.id) | desc())
dense_rank(order: count(orders.id), order: average(users.age))
// with partition
dense_rank(order: count(orders.id), partition: orders.status)
// Axis-aware examples
dense_rank(order: 'rows') // Dense rank by row order
dense_rank(order: 'columns' | desc()) // Dense rank by column order (descending)
dense_rank(order: revenue | desc(), partition: 'rows') // Dense 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.
dense_rank(order: count(orders.id) | desc())ordense_rank(order: 'rows')
partition(repeatable, optional): A field that is used for partitioning the table. You can also use axis references like'rows','columns','x_axis', or'legend'. E.g.dense_rank(order: count(orders.id), partition: orders.status)ordense_rank(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 consecutive. E.g. 1, 1, 2, 3, 3, 4, ...
Sample Usages
explore {
dimensions {
users.gender, users.age_group
}
measures {
count(orders.id),
_rank: dense_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: dense_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 dense_rank(order: users.age | desc()) ;;
}explore {
dimensions {
users.age,
users.ranking_by_age
}
}| users->age | users->ranking_by_age |
|---|---|
| 21 | 20 |
| 39 | 2 |
| 36 | 5 |
| 24 | 17 |
| 31 | 10 |
| 20 | 21 |
| 37 | 4 |
| 23 | 18 |
| 25 | 16 |
| 19 | 22 |
| 38 | 3 |
| 17 | 24 |
| 35 | 6 |
| 16 | 25 |
| 33 | 8 |
| 22 | 19 |
| 18 | 23 |
| 32 | 9 |
| 28 | 13 |
| 34 | 7 |
| 26 | 15 |
| 27 | 14 |
| 29 | 12 |
| 40 | 1 |
| 30 | 11 |
You can also use it in filter() to filter out the top 10 users by age:
users
| select(users.full_name, _rank: dense_rank(order: users.age))
| filter(_rank == 1)| users->full_name | _rank |
|---|---|
| Fauske Matsuura | 1 |
| Treglia Melissia | 1 |
| Miss Brice | 1 |
| Danser Raguel | 1 |
| Balcerzak Syed | 1 |
| Corigliano Josphine | 1 |
| Burchill Elliott | 1 |
| Plott Rose | 1 |
| Anecelle Roland | 1 |
| Westler Mark | 1 |
| Friess Euna | 1 |
| Leas Deb | 1 |
| Houseman Verda | 1 |
| Sinkovich Tyron | 1 |
| Malatesta Kubo | 1 |
| Zawislak Narayan | 1 |
| Whitehill Mui | 1 |
| Lanter Gilbert | 1 |
| Dooney Odelia | 1 |
| Stepney Vuong | 1 |
| Kuney Jerilyn | 1 |
| Venskoske Julietta | 1 |
| Muszynski Sheilah | 1 |
| Borsh Carmella | 1 |
| Birdsey Choudhry | 1 |
| Aldape Courtney | 1 |
| Ruple Evelia | 1 |
| Schepp Guan | 1 |
| Gunsolus Sabra | 1 |
| Dolinsky Eugena | 1 |
| Berray Darrel | 1 |
| Drey Elfreda | 1 |
| Liebler Chawla | 1 |
| Vankammen Tressa | 1 |
| Castellaneta Arnold | 1 |
| Madsen Gomez | 1 |
| Vea Maybelle | 1 |
| Pentek Reina | 1 |
| Paplow Dino | 1 |
| Gulisano Brady | 1 |
| Shollenbarger Cruz | 1 |
| Lieser Seki | 1 |
| Zakutney Hermelinda | 1 |
| Goldberg Hermila | 1 |
| Chatagnier Sondra | 1 |
| Floras Milissa | 1 |
| Kunimitsu Daniel | 1 |
| Prieur Nick | 1 |
| Carlone Saito | 1 |
| Villenas Willard | 1 |
| Bacolor Hong | 1 |
| Leighty Kelley | 1 |
| Noonkester Pacheco | 1 |
| Rout Pablo | 1 |
| Werderman Florene | 1 |
| Goetze Allison | 1 |
| Bauknight Mai | 1 |
| Gurrero Kawashima | 1 |
| Mizwicki Genevie | 1 |
| Doyer Bishop | 1 |
| Nevala Hang | 1 |
| Kuechler Nelle | 1 |
| Sulloway Cammie | 1 |
| Baptista Cherie | 1 |
| Olkowski Easter | 1 |
| Vandale Maggie | 1 |
| Raisley Petrina | 1 |
| Coggeshall Prom | 1 |
| Beattie Chana | 1 |
| Lehrer Margarito | 1 |
| Bonnet Otilia | 1 |
| Muschaweck Hannelore | 1 |
| Frericks Takagi | 1 |
| Shannonhouse Renae | 1 |
| Ped Emmett | 1 |
| Tudman Kelly | 1 |
| Kernighan Angel | 1 |
See also
rank(): same value gets same rank, but next rank skips aheadpercent_rank()ntile()- Window Functions
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, ...
percent_rank
AQL Window Function: Returns the relative percentile of a row within a partition of a table. The value is between 0 and 1, inclusive.