Skip to main content
On this page

Kater SQL Functions Calculate at

KATER__CALCULATE_PER

The measure worked out by the query's grouping and the fields you name, then combined back to the query's grouping.

Family: Calculate at

Syntax

KATER__CALCULATE_PER(measure, function, field [, ...])

Arguments

ArgumentDescription
measureThe measure, bare or as EVAL(measure).
functionHow to combine the values of the added fields for each row, such as 'avg' for the average per month. One of 'sum', 'avg', 'min', 'max'.
fieldsThe fields to add to the grouping. One or more fields.

Returns

A number. The field menu writes the @format of the field it reads beside it, or @format number(2) where that field has none and can have decimals.

Usage notes

  • Only combinations with rows count, so a month with no orders is not in the average.
  • Its menu name is Per. It compiles to CALCULATE_PER(measure, fields, summarize => function).
  • Declare it with MEASURE and a name of its own, as the field menu does, such as MEASURE('per_order_month_avg__count_distinct__order_id', ...).
  • The query reads one model with no joins, and has no window in its SELECT.
  • Another query cannot read a field made with it. Write it in the query that needs it.

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",
MEASURE(
'per_order_month_avg__count_distinct__order_id',
KATER__CALCULATE_PER(orders.count_distinct__order_id, 'avg', orders.order_month)
) AS "Orders per Month" /* @format number(1) */
FROM kater_dataset.orders AS orders
WHERE
KATER__IS_NOT_NULL(orders.region)
GROUP BY
orders.region
ORDER BY
1
RegionOrdersOrders per Month
AMER72834.7
APAC67132.0
EMEA77737.0
LATAM76036.2

See also