Skip to main content
On this page

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

ArgumentDescription
measureThe measure, bare or as EVAL(measure).
functionHow to combine the values when one row of the query holds more than one of them. One of 'sum', 'avg', 'min', 'max'.

Optional

ArgumentDescription
fieldsThe 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 MEASURE and a name of its own, as the field menu does, such as MEASURE('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 orders
WHERE
KATER__IS_EQUAL_TO(orders.region, 'EMEA')
GROUP BY
orders.region,
orders.channel
ORDER BY
2
RegionChannelOrdersRegion Orders
EMEAemail202777
EMEApaid204777
EMEAstore186777
EMEAweb185777

See also