Skip to main content

avgWeighted

Calculates the weighted arithmetic mean.

Syntax

avgWeighted(x, weight)

Arguments

  • x β€” Values.
  • weight β€” Weights of the values.

x and weight must both be Integer, floating-point, or Decimal, but may have different types.

Returned value

  • NaN if all the weights are equal to 0 or the supplied weights parameter is empty.
  • Weighted mean otherwise.

Return type is always Float64.

Example

Query:

SELECT avgWeighted(x, w)
FROM values('x Int8, w Int8', (4, 1), (1, 0), (10, 2))

Result:

β”Œβ”€avgWeighted(x, weight)─┐
β”‚ 8 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Example

Query:

SELECT avgWeighted(x, w)
FROM values('x Int8, w Float64', (4, 1), (1, 0), (10, 2))

Result:

β”Œβ”€avgWeighted(x, weight)─┐
β”‚ 8 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Example

Query:

SELECT avgWeighted(x, w)
FROM values('x Int8, w Int8', (0, 0), (1, 0), (10, 0))

Result:

β”Œβ”€avgWeighted(x, weight)─┐
β”‚ nan β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Example

Query:

CREATE table test (t UInt8) ENGINE = Memory;
SELECT avgWeighted(t) FROM test

Result:

β”Œβ”€avgWeighted(x, weight)─┐
β”‚ nan β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜