On this page
Kater SQL Functions Windows
KATER__RANK
The value's rank among the rows, with 1 for the largest or the smallest value.
Family: Windows
Syntax
KATER__RANK(field, order_direction) OVER ([PARTITION BY ...])Arguments
| Argument | Description |
|---|---|
field | The value to rank by, such as orders.sum__amount. |
order_direction | 'desc' gives rank 1 to the largest value, and 'asc' to the smallest. One of 'asc', 'desc'. |
Returns
A whole number. Rows that tie share a rank, and the next rank is skipped: 1, 2, 2, 4. The field menu writes @format number(0) beside it.
Usage notes
- Write
OVER (PARTITION BY ...)to work within each group, orOVER ()for every row. It orders the rows itself, so itsOVERtakes noORDER BY. - Empty values rank last.
Example
This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.
SELECT orders.region AS "Region", EVAL(orders.count_distinct__order_id) AS "Orders", CUSTOM_CALCULATION( 'rank__count_distinct__order_id', KATER__RANK(orders.count_distinct__order_id, 'desc') OVER () ) AS "Rank"FROM kater_dataset.orders AS ordersGROUP BY orders.regionORDER BY 3| Region | Orders | Rank |
|---|---|---|
| EMEA | 777 | 1 |
| LATAM | 760 | 2 |
| AMER | 728 | 3 |
| APAC | 671 | 4 |
| null | 64 | 5 |
Compiles to
RANK() OVER (ORDER BY orders.count_distinct__order_id DESC)RANK() OVER (ORDER BY orders.count_distinct__order_id DESC NULLS LAST)