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
| Argument | Description |
|---|---|
measure | The measure, bare or as EVAL(measure). |
function | How 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'. |
fields | The 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
MEASUREand a name of its own, as the field menu does, such asMEASURE('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 ordersWHERE KATER__IS_NOT_NULL(orders.region)GROUP BY orders.regionORDER BY 1| Region | Orders | Orders per Month |
|---|---|---|
| AMER | 728 | 34.7 |
| APAC | 671 | 32.0 |
| EMEA | 777 | 37.0 |
| LATAM | 760 | 36.2 |
See also
- Calculation context
- Other functions in Calculate at:
KATER__ALWAYS_BY,KATER__IGNORE_GROUPING_BY