- Categories:
Window functions (General)
RATIO_TO_REPORT¶
Returns the ratio of a value within a group to the sum of the values within the group. If expr1 evaluates
to null or the sum of expr1 within the group evaluates to 0, then RATIO_TO_REPORT returns null.
Syntax¶
Arguments¶
expr1This is an expression that evaluates to a numeric data type (INTEGER, FLOAT, DECIMAL, etc.).
expr2This is the optional expression to partition by.
expr3This is the optional expression to order by within each partition. Note that for this function, the order within the partition does not affect the output.
In this function, as in all window functions, this ORDER BY does not control the order of the entire query output.
Usage notes¶
RATIO_TO_REPORT is calculated as:
value of
expr1argument for the current row / sum ofexpr1argument for the partitionThe ORDER BY clause within the OVER clause is allowed in this function for syntactic consistency with other window functions but does not affect the calculation. Snowflake recommends not including the ORDER BY clause when using this function.
Examples¶
This simple example shows the percentage of a store chain’s profit that was generated by each individual store:
This example shows the percentage of profit within each province that was generated by each store in that province: