

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

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

# RATIO\$1TO\$1REPORT 範圍函數
<a name="r_WF_RATIO_TO_REPORT"></a>

計算視窗或分割區內值與值總和的比率。使用下列公式決定報告值的比率：

`value of `*ratio\$1expression* `argument for the current row / sum of` *ratio\$1expression* `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 (含)。如果 *ratio\$1expressio*n 為 NULL，則傳回值為 `NULL`。如果 *partition\$1expression* 中的值是唯一的，則函數將傳回 `1` 代表該值。

## 語法
<a name="r_WF_RATIO_TO_REPORT-synopsis"></a>

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

## 引數
<a name="r_WF_RATIO_TO_REPORT-arguments"></a>

*ratio\$1expression*   
此表達式 (例如欄名) 提供要決定比率的值。表達式必須為數值資料類型，或可隱含地轉換為數值資料類型。  
您不能在 *ratio\$1expression* 中使用其他任何分析函數。

OVER  
用於指定視窗分割的子句。OVER 子句不能包含視窗排序或視窗框規格。

PARTITION BY *partition\$1expression*   
選用。此表達式針對 OVER 子句中的每一個群組，設定記錄範圍。

## 傳回類型
<a name="r_WF_RATIO_TO_REPORT-return-type"></a>

FLOAT8

## 範例
<a name="r_WF_RATIO_TO_REPORT-examples"></a>

下列範例使用 WINSALES 資料表。如需有關如何建立 WINSALES 資料表的資訊，請參閱[範圍函數範例的範例資料表](c_Window_functions.md#r_Window_function_example)。

下列範例會計算賣家數量各資料列的報表比率，以及所有賣家數量的總計。

```
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
```