groupArrayMovingAvg

Calculates the moving average of input values.

  1. groupArrayMovingAvg(numbers_for_summing)
  2. groupArrayMovingAvg(window_size)(numbers_for_summing)

The function can take the window size as a parameter. If left unspecified, the function takes the window size equal to the number of rows in the column.

Parameters

  • numbers_for_summingExpression resulting in a numeric data type value.
  • window_size — Size of the calculation window.

Returned values

  • Array of the same size and type as the input data.

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

Example

The sample table b:

  1. CREATE TABLE t
  2. (
  3. `int` UInt8,
  4. `float` Float32,
  5. `dec` Decimal32(2)
  6. )
  7. ENGINE = TinyLog
  1. ┌─int─┬─float─┬──dec─┐
  2. 1 1.1 1.10
  3. 2 2.2 2.20
  4. 4 4.4 4.40
  5. 7 7.77 7.77
  6. └─────┴───────┴──────┘

The queries:

  1. SELECT
  2. groupArrayMovingAvg(int) AS I,
  3. groupArrayMovingAvg(float) AS F,
  4. groupArrayMovingAvg(dec) AS D
  5. FROM t
  1. ┌─I─────────┬─F───────────────────────────────────┬─D─────────────────────┐
  2. [0,0,1,3] [0.275,0.82500005,1.9250001,3.8675] [0.27,0.82,1.92,3.86]
  3. └───────────┴─────────────────────────────────────┴───────────────────────┘
  1. SELECT
  2. groupArrayMovingAvg(2)(int) AS I,
  3. groupArrayMovingAvg(2)(float) AS F,
  4. groupArrayMovingAvg(2)(dec) AS D
  5. FROM t
  1. ┌─I─────────┬─F────────────────────────────────┬─D─────────────────────┐
  2. [0,1,3,5] [0.55,1.6500001,3.3000002,6.085] [0.55,1.65,3.30,6.08]
  3. └───────────┴──────────────────────────────────┴───────────────────────┘