RATIO_ 到 _ REPORT 視窗功能 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

RATIO_ 到 _ 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 (含)。如果比例表達式是NULL,則返回值為。NULL如果 partition_expression 中的值是唯一的,則函數將傳回該值。

語法

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

引數

ratio_expression

此表達式 (例如欄名) 提供要決定比率的值。表達式必須為數值資料類型,或可隱含地轉換為數值資料類型。

您不能在 ratio_expression 中使用其他任何分析函數。

OVER

用於指定視窗分割的子句。該OVER條款不能包含窗口順序或窗框規格。

PARTITIONBY 分區表達式

選用。設定OVER子句中每個群組之記錄範圍的運算式。

傳回類型

FLOAT8

範例

下列範例使用WINSALES表格。如需有關如何建立WINSALES表格的資訊,請參閱範圍函數範例的範例資料表

下列範例會計算賣家數量每一列的 ratio-to-report 值,以及所有賣家數量的總計。

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