Kater SQL Functions Field declarations
CUSTOM_CALCULATION
Declares a custom calculation: a value worked out over the rows of the query's result, such as a rank or a running total.
Family: Field declarations
It reads the rows the query returns, after it groups them.
Syntax
CUSTOM_CALCULATION('name', formula) AS "Label"Arguments
| Argument | Description |
|---|---|
name | The field’s name, in quotes. No other field in this SELECT, or in the models it reads, can have it, in any letter case. |
formula | A window over the result rows, such as RANK() OVER (ORDER BY EVAL(orders.sum__quantity) DESC), an EXCEL formula, or an expression over another custom calculation. |
Returns
A value for each row of the result.
Usage notes
- Write it only in a query’s final SELECT, as a whole column. Queries, tests and the Sandbox have them. A shared model does not.
- Its value depends on the other rows of the result. A reader can select it, filter by it and group by it, but cannot
EVALit. - A
SUMorAVGof a custom calculation combines finished results. It is not worked out again. - A formula that neither runs a window nor reads a custom calculation is published as a dimension.
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('units_rank', RANK() OVER (ORDER BY EVAL(orders.sum__quantity) DESC)) AS "Units Rank"FROM kater_dataset.orders AS ordersGROUP BY orders.channelORDER BY 1| Channel | Units | Units Rank |
|---|---|---|
| 2,383 | 1 | |
| paid | 2,292 | 3 |
| store | 2,330 | 2 |
| web | 2,149 | 4 |
See also
- Fields
- Other functions in Field declarations:
DIMENSION,MEASURE,NOT_ADDITIVE_ACROSS,EXCEL