

 从补丁 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/)。

# SUM 窗口函数
<a name="r_WF_SUM"></a>

 SUM 窗口函数返回输入列值或表达式值的和。SUM 函数使用数值并忽略 NULL 值。

## 语法
<a name="r_WF_SUM-synopsis"></a>

```
SUM ( [ ALL ] expression ) OVER
(
[ PARTITION BY expr_list ]
[ ORDER BY order_list 
                        frame_clause ]
)
```

## 参数
<a name="r_WF_SUM-arguments"></a>

 *expression*   
对其执行函数的目标列或表达式。

ALL   
利用参数 ALL，该函数可保留表达式中的所有重复值。ALL 是默认值。DISTINCT 不受支持。

OVER   
指定聚合函数的窗口子句。OVER 子句将窗口聚合函数与普通集合聚合函数区分开来。

PARTITION BY *expr\$1list*   
依据一个或多个表达式定义 SUM 函数的窗口。

ORDER BY *order\$1list*   
对每个分区中的行进行排序。如果未指定 PARTITION BY，则 ORDER BY 使用整个表。

 *frame\$1clause*   
如果 ORDER BY 子句用于聚合函数，则需要显式框架子句。框架子句优化函数窗口中的行集，包含或排除已排序结果中的行集。框架子句包括 ROWS 关键字和关联的说明符。请参阅 [窗口函数语法摘要](c_Window_functions.md#r_Window_function_synopsis)。

## 数据类型
<a name="c_Supported_data_types_wf_sum"></a>

SUM 函数支持的参数类型为 SMALLINT、INTEGER、BIGINT、NUMERIC、DECIMAL、REAL 和 DOUBLE PRECISION。

SUM 函数支持的返回类型为：
+ 适用于 SMALLINT 或 INTEGER 参数的 BIGINT
+ 适用于 BIGINT 参数的 NUMERIC
+ 适用于浮点参数的 DOUBLE PRECISION

## 示例
<a name="r_WF_SUM-examples"></a>

以下示例创建按日期和销售 ID 排序的销售数量的累积（移动）和：

```
select salesid, dateid, sellerid, qty,
sum(qty) over (order by dateid, salesid rows unbounded preceding) as sum
from winsales
order by 2,1;

salesid |   dateid   | sellerid | qty | sum
---------+------------+----------+-----+-----
30001 | 2003-08-02 |        3 |  10 |  10
10001 | 2003-12-24 |        1 |  10 |  20
10005 | 2003-12-24 |        1 |  30 |  50
40001 | 2004-01-09 |        4 |  40 |  90
10006 | 2004-01-18 |        1 |  10 | 100
20001 | 2004-02-12 |        2 |  20 | 120
40005 | 2004-02-12 |        4 |  10 | 130
20002 | 2004-02-16 |        2 |  20 | 150
30003 | 2004-04-18 |        3 |  15 | 165
30004 | 2004-04-18 |        3 |  20 | 185
30007 | 2004-09-07 |        3 |  30 | 215
(11 rows)
```

 有关 WINSALES 表的说明，请参阅[窗口函数示例的示例表](c_Window_functions.md#r_Window_function_example)。

以下示例按日期创建销售数量的累积（移动）和，按卖家 ID 对结果进行分区，并按日期和销售 ID 对该分区中的结果进行排序：

```
select salesid, dateid, sellerid, qty,
sum(qty) over (partition by sellerid
order by dateid, salesid rows unbounded preceding) as sum
from winsales
order by 2,1;

salesid |   dateid   | sellerid | qty | sum
---------+------------+----------+-----+-----
30001 | 2003-08-02 |        3 |  10 |  10
10001 | 2003-12-24 |        1 |  10 |  10
10005 | 2003-12-24 |        1 |  30 |  40
40001 | 2004-01-09 |        4 |  40 |  40
10006 | 2004-01-18 |        1 |  10 |  50
20001 | 2004-02-12 |        2 |  20 |  20
40005 | 2004-02-12 |        4 |  10 |  50
20002 | 2004-02-16 |        2 |  20 |  40
30003 | 2004-04-18 |        3 |  15 |  25
30004 | 2004-04-18 |        3 |  20 |  45
30007 | 2004-09-07 |        3 |  30 |  75
(11 rows)
```

对结果集中的所有行进行编号（按 SELLERID 和 SALESID 列进行排序）：

```
select salesid, sellerid, qty,
sum(1) over (order by sellerid, salesid rows unbounded preceding) as rownum
from winsales
order by 2,1;

salesid | sellerid |  qty | rownum
--------+----------+------+--------
10001 |        1 |   10 |     1
10005 |        1 |   30 |     2
10006 |        1 |   10 |     3
20001 |        2 |   20 |     4
20002 |        2 |   20 |     5
30001 |        3 |   10 |     6
30003 |        3 |   15 |     7
30004 |        3 |   20 |     8
30007 |        3 |   30 |     9
40001 |        4 |   40 |    10
40005 |        4 |   10 |    11
(11 rows)
```

有关 WINSALES 表的说明，请参阅[窗口函数示例的示例表](c_Window_functions.md#r_Window_function_example)。

以下示例对结果集中的所有行按顺序进行编号，按 SELLERID 对结果进行分区，并按 SELLERID 和 SALESID 对该分区中的结果进行排序：

```
select salesid, sellerid, qty,
sum(1) over (partition by sellerid
order by sellerid, salesid rows unbounded preceding) as rownum
from winsales
order by 2,1;

salesid | sellerid | qty | rownum
---------+----------+-----+--------
10001 |        1 |  10 |      1
10005 |        1 |  30 |      2
10006 |        1 |  10 |      3
20001 |        2 |  20 |      1
20002 |        2 |  20 |      2
30001 |        3 |  10 |      1
30003 |        3 |  15 |      2
30004 |        3 |  20 |      3
30007 |        3 |  30 |      4
40001 |        4 |  40 |      1
40005 |        4 |  10 |      2
(11 rows)
```