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

ArgumentDescription
fieldThe 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, or OVER () for every row. It orders the rows itself, so its OVER takes no ORDER 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 orders
GROUP BY
orders.channel
ORDER BY
1
ChannelUnitsShare of Units
email2,38326.0%
paid2,29225.0%
store2,33025.5%
web2,14923.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)

See also