Skip to main content
Skip to main content

groupArrayMovingAvg

groupArrayMovingAvg

Introduced in: v20.1

Calculates the moving average of input values.

Note

The function uses rounding towards zero. It truncates the decimal places insignificant for the resulting data type.

Syntax

groupArrayMovingAvg(numbers_for_summing)
groupArrayMovingAvg(window_size)(numbers_for_summing)

Parameters

  • window_size — Size of the calculation window. If left unspecified, the function takes the window size equal to the number of rows in the column. UInt64

Arguments

  • numbers_for_summing — Expression resulting in a numeric data type value. (U)Int* or Float* or Decimal

Returned value

Returns an array of the same size and type as the input data. Array

Examples

Usage example

CREATE TABLE t
(
    `int` UInt8,
    `float` Float32,
    `dec` Decimal32(2)
)
ENGINE = Memory;

INSERT INTO t VALUES (1, 1.1, 1.10), (2, 2.2, 2.20), (4, 4.4, 4.40), (7, 7.77, 7.77);

SELECT
    groupArrayMovingAvg(int) AS I,
    groupArrayMovingAvg(float) AS F,
    groupArrayMovingAvg(dec) AS D
FROM t;
┌─I─────────┬─F───────────────────────────────────┬─D─────────────────────┐
│ [0,0,1,3] │ [0.275,0.82500005,1.9250001,3.8675] │ [0.27,0.82,1.92,3.86] │
└───────────┴─────────────────────────────────────┴───────────────────────┘

With window size

SELECT
    groupArrayMovingAvg(2)(int) AS I,
    groupArrayMovingAvg(2)(float) AS F,
    groupArrayMovingAvg(2)(dec) AS D
FROM t;
┌─I─────────┬─F────────────────────────────────┬─D─────────────────────┐
│ [0,1,3,5] │ [0.55,1.6500001,3.3000002,6.085] │ [0.55,1.65,3.30,6.08] │
└───────────┴──────────────────────────────────┴───────────────────────┘