RATIO_TO_REPORT 开窗函数 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

RATIO_TO_REPORT 开窗函数

计算某个窗口或分区中的某个值与所有值的和的比率。使用以下公式确定报表值的比率:

value of ratio_expression argument for the current row / sum of ratio_expression argument for the window or partition

以下数据集说明了此公式的使用:

Row# Value Calculation RATIO_TO_REPORT 1 2500 (2500)/(13900) 0.1798 2 2600 (2600)/(13900) 0.1870 3 2800 (2800)/(13900) 0.2014 4 2900 (2900)/(13900) 0.2086 5 3100 (3100)/(13900) 0.2230

返回值范围介于 0 和 1(含 1)之间。如果 ratio_expression 为 NULL,则返回值为 NULL。如果 partition_expression 中的值是唯一的,则函数将为该值返回 1

语法

RATIO_TO_REPORT ( ratio_expression ) OVER ( [ PARTITION BY partition_expression ] )

参数

ratio_expression

一个提供要为其确定比率的值的表达式(例如列名)。该表达式必须具有数字数据类型或可隐式转换为 1。

您无法在 ratio_expression 中使用任何其他分析函数。

OVER

一个指定窗口分区的子句。OVER 子句不能包含窗口排序或窗口框架规范。

PARTITION BY partition_expression

可选。一个设置 OVER 子句中每个组的记录范围的表达式。

返回类型

FLOAT8

示例

以下各示例使用 WINSALES 表。有关如何创建 WINSALES 表的信息,请参阅窗口函数示例的示例表

以下示例计算每行卖家数量占所有卖家总数量的比率值。

select sellerid, qty, ratio_to_report(qty) over() from winsales order by sellerid; sellerid qty ratio_to_report -------------------------------------- 1 30 0.13953488372093023 1 10 0.046511627906976744 1 10 0.046511627906976744 2 20 0.09302325581395349 2 20 0.09302325581395349 3 30 0.13953488372093023 3 20 0.09302325581395349 3 15 0.06976744186046512 3 10 0.046511627906976744 4 10 0.046511627906976744 4 40 0.18604651162790697

以下示例按分区计算每个卖家的销售数量的比率。

select sellerid, qty, ratio_to_report(qty) over(partition by sellerid) from winsales; sellerid qty ratio_to_report ------------------------------------------- 2 20 0.5 2 20 0.5 4 40 0.8 4 10 0.2 1 10 0.2 1 30 0.6 1 10 0.2 3 10 0.13333333333333333 3 15 0.2 3 20 0.26666666666666666 3 30 0.4