On this page
Kater SQL Functions Windows
KATER__ROW_NUMBER
The row's place when the rows are sorted by the value, from 1.
Family: Windows
Syntax
KATER__ROW_NUMBER(field, order_direction) OVER ([PARTITION BY ...])Arguments
| Argument | Description |
|---|---|
field | The value to sort by. |
order_direction | 'desc' gives number 1 to the largest value, and 'asc' to the smallest. One of 'asc', 'desc'. |
Returns
A whole number. Each row gets its own number. 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. - Rows that tie have no fixed order, so their numbers can change from one run to the next.
- Empty values come last.
Example
This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.
SELECT orders.currency AS "Currency", orders.channel AS "Channel", EVAL(orders.sum__amount) AS "Revenue", CUSTOM_CALCULATION( 'row_number__sum__amount', KATER__ROW_NUMBER(orders.sum__amount, 'desc') OVER (PARTITION BY orders.currency) ) AS "Place in Currency"FROM kater_dataset.orders AS ordersWHERE KATER__IS_ONE_OF(orders.channel, 'email', 'paid', 'web')GROUP BY orders.currency, orders.channelORDER BY 1, 4| Currency | Channel | Revenue | Place in Currency |
|---|---|---|---|
| EUR | €22,431.55 | 1 | |
| EUR | web | €20,581.29 | 2 |
| EUR | paid | €20,213.32 | 3 |
| USD | $100,397.00 | 1 | |
| USD | paid | $94,166.76 | 2 |
| USD | web | $80,943.99 | 3 |
Compiles to
ROW_NUMBER() OVER (PARTITION BY orders.currency ORDER BY orders.sum__amount DESC)ROW_NUMBER() OVER (PARTITION BY orders.currency ORDER BY orders.sum__amount DESC NULLS LAST)