Kater SQL Functions Calculate at
KATER__ALWAYS_BY
The measure worked out by exactly the fields you name, whatever the query groups by.
Family: Calculate at
Syntax
KATER__ALWAYS_BY(measure, function [, field, ...])Arguments
Required
| Argument | Description |
|---|---|
measure | The measure, bare or as EVAL(measure). |
function | How to combine the values when one row of the query holds more than one of them. One of 'sum', 'avg', 'min', 'max'. |
Optional
| Argument | Description |
|---|---|
fields | The fields to work the measure out by. Left out, it is the total of every row the query reads. 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
- Grouped by a field you name and by others, each row gets the value of its group. So each channel row of a region shows the region’s orders.
- The query’s WHERE filters still apply.
- Its menu name is By. It compiles to
CALCULATE_BY(measure, fields, summarize => function). - Declare it with
MEASUREand a name of its own, as the field menu does, such asMEASURE('by_region_sum__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", orders.channel AS "Channel", EVAL(orders.count_distinct__order_id) AS "Orders", MEASURE( 'by_region_sum__count_distinct__order_id', KATER__ALWAYS_BY(orders.count_distinct__order_id, 'sum', orders.region) ) AS "Region Orders"FROM kater_dataset.orders AS ordersWHERE KATER__IS_EQUAL_TO(orders.region, 'EMEA')GROUP BY orders.region, orders.channelORDER BY 2| Region | Channel | Orders | Region Orders |
|---|---|---|---|
| EMEA | 202 | 777 | |
| EMEA | paid | 204 | 777 |
| EMEA | store | 186 | 777 |
| EMEA | web | 185 | 777 |
See also
- Calculation context
- Other functions in Calculate at:
KATER__CALCULATE_PER,KATER__IGNORE_GROUPING_BY