

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# RATIO\$1TO\$1REPORT 开窗函数
RATIO\$1TO\$1REPORT

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

`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（含 1）之间。如果 *ratio\$1expression* 为 NULL，则返回值为 `NULL`。如果 *partition\$1expression* 中的值是唯一的，则函数将为该值返回 `1`。

## 语法
语法

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

## 参数
参数

*ratio\$1expression*   
一个提供要为其确定比率的值的表达式（例如列名）。该表达式必须具有数字数据类型或可隐式转换为 1。  
您无法在 *ratio\$1expression* 中使用任何其他分析函数。

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

PARTITION BY *partition\$1expression*   
可选。一个设置 OVER 子句中每个组的记录范围的表达式。

## 返回类型
返回类型

FLOAT8

## 示例
示例

以下各示例使用 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
```