SUM [analytic]
计算
窗口内一组行的表达式总和。它将返回浮点表达式的 DOUBLE PRECISION
值。否则,返回值与表达式数据类型相同。
行为类型
不可变语法
SUM ( expression ) OVER (
[ window-partition-clause ]
[ window-order-clause ]
[ window-frame-clause ] )
参数
- 表达式
- 任何
NUMERIC
数据类型或可隐式转换为数字数据类型的任何非数字数据类型。函数返回的数据类型与参数的数字数据类型相同。 OVER()
- 请参阅分析函数
溢出处理
如果在使用 SUM
时遇到数据溢出问题,请使用
SUM_FLOAT
将数据转换为浮点。
默认情况下,当您对数值数据类型调用此函数时,Vertica 允许静默数值溢出。有关此行为以及如何更改它的更多信息,请参阅SUM、SUM_FLOAT 和 AVG 的数字数据类型溢出。
示例
以下查询返回店面 1 月份所有收益的累计和:
=> SELECT calendar_month_name AS month, transaction_type, sales_quantity,
SUM(sales_quantity)
OVER (PARTITION BY calendar_month_name ORDER BY date_dimension.date_key) AS SUM
FROM store.store_sales_fact JOIN date_dimension
USING(date_key) WHERE calendar_month_name IN ('January')
AND transaction_type= 'return';
month | transaction_type | sales_quantity | SUM
---------+------------------+----------------+------
January | return | 7 | 651
January | return | 3 | 651
January | return | 7 | 651
January | return | 7 | 651
January | return | 7 | 651
January | return | 3 | 651
January | return | 7 | 651
January | return | 5 | 651
January | return | 1 | 651
January | return | 6 | 651
January | return | 6 | 651
January | return | 3 | 651
January | return | 9 | 651
January | return | 7 | 651
January | return | 6 | 651
January | return | 8 | 651
January | return | 7 | 651
January | return | 2 | 651
January | return | 4 | 651
January | return | 5 | 651
January | return | 7 | 651
January | return | 8 | 651
January | return | 4 | 651
January | return | 10 | 651
January | return | 6 | 651
...