Skip to main content
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

ArgumentDescription
fieldThe 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, or OVER () for every row. It orders the rows itself, so its OVER takes no ORDER 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 orders
WHERE
KATER__IS_ONE_OF(orders.channel, 'email', 'paid', 'web')
GROUP BY
orders.currency,
orders.channel
ORDER BY
1,
4
CurrencyChannelRevenuePlace in Currency
EURemail€22,431.551
EURweb€20,581.292
EURpaid€20,213.323
USDemail$100,397.001
USDpaid$94,166.762
USDweb$80,943.993

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)

See also