TSRunningAvg 函数

TSRunningAvg 函数计算 SMALLFLOAT 或 DOUBLE PRECISION 值的运行平均值。

语法

TSRunningAvg(value      double precision, 
              num_values integer) 
returns double precision;

TSRunningAvg(value      real, 
              num_values integer) 
returns double precision;
value
在运行平均值中包括的值。
num_values
在运行平均值内包括的值的数量,k

描述

Apply 函数内使用 TSRunningAvg 函数。

运行平均值是最后 k 个值的平均值,其中 k 由用户提供。如果值是 NULL,会使用上一个值。前 k-1 个值的运行平均值是 NULL

TSRunningAvg 函数可以采用作为时间系列的列的参数。使用 Apply 函数接受的相同参数格式。

此函数是运行固定数量的元素,而非运行固定时间长度;因此,它可能不适用于非常规时间系列。

返回结果

最后 k 个值的 SMALLFLOAT 或 DOUBLE PRECISION 运行平均值。

示例

示例基于以下行类型:
create row type if not exists stock_bar (
    timestamp datetime year to fraction(5), 
    high real, 
    low      real,
final      real,
vol real);

示例使用以下输入数据:

2011-01-03 00:00:00.00000          3        2      1        3
2011-01-04 00:00:00.00000          2        2      2        3
2011-01-05 00:00:00.00000          2        2      3        3
2011-01-06 00:00:00.00000          2        2               3

请注意,2011-01-06 对应的 final 列为空值。

以下示例中的 SELECT 查询会从 final 列返回收盘价以及在时间系列中股票的 4 日移动均价:

select stock_name, Apply('TSRunningAvg($final,4)',
    '2011-01-03 00:00:00.00000'::datetime year to fraction(5),
    '2011-01-06 00:00:00.00000'::datetime year to fraction(5),
    stock_data::TimeSeries(stock_bar))::TimeSeries(one_real)
from first_stocks;

查询将返回以下结果:

stock_name    GBase 8s
(expression)  origin(2011-01-03 00:00:00.00000), calendar(daycal), container(),
              threshold(20), regular, [(1.000000000000), (1.500000000000), (2.
              000000000000), (2.000000000000)]

第四个结果与第三个结果相同,因为 final 列中的第四个值为空。