On this page
Kater SQL Functions Windows
KATER__SHARE_OF_TOTAL
The value as a share of the total of all the rows in its partition.
Family: Windows
Syntax
KATER__SHARE_OF_TOTAL(field) OVER ([PARTITION BY ...])Arguments
| Argument | Description |
|---|---|
field | The value to divide by the total, such as orders.sum__amount. |
Returns
A fraction, such as 0.25 for 25%. The field menu writes @format percent(1) beside it, so it shows as a percentage. NULL where the total is 0.
Usage notes
- Write
OVER (PARTITION BY ...)to work within each group, orOVER ()for every row. It orders the rows itself, so itsOVERtakes noORDER BY. - It adds up the rows of the result. A measure that cannot be added up across a dimension, such as Revenue across Currency, needs that dimension in
PARTITION BY. - A negative value gives a negative share.
Example
This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.
SELECT orders.channel AS "Channel", EVAL(orders.sum__quantity) AS "Units", CUSTOM_CALCULATION( 'share_of_total__sum__quantity', KATER__SHARE_OF_TOTAL(orders.sum__quantity) OVER () ) AS "Share of Units" /* @format percent(1) */FROM kater_dataset.orders AS ordersGROUP BY orders.channelORDER BY 1| Channel | Units | Share of Units |
|---|---|---|
| 2,383 | 26.0% | |
| paid | 2,292 | 25.0% |
| store | 2,330 | 25.5% |
| web | 2,149 | 23.5% |
Compiles to
The call compiles to the same SQL on DuckDB and on Snowflake.
CAST(orders.sum__quantity AS DOUBLE) / NULLIF(SUM(orders.sum__quantity) OVER (), 0)