Calculates the weighted arithmetic mean.
Syntax
Arguments
x — Values.
weight — Weights of the values.
x and weight must both be
Integer or floating-point,
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 │
└────────────────────────┘
avgWeighted
Introduced in: v20.1
Calculates the weighted arithmetic mean.
Syntax
Arguments
Returned value
Returns NaN if all the weights are equal to 0 or the supplied weights parameter is empty, or the weighted mean otherwise. Float64
Examples
Usage example
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
┌─avgWeighted(x, w)─┐
│ 8 │
└───────────────────┘
Mixed integer and float weights
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Float64', (4, 1), (1, 0), (10, 2))
┌─avgWeighted(x, w)─┐
│ 8 │
└───────────────────┘
All weights are zero returns NaN
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (0, 0), (1, 0), (10, 0))
┌─avgWeighted(x, w)─┐
│ nan │
└───────────────────┘
Empty table returns NaN
CREATE TABLE test (t UInt8) ENGINE = Memory;
SELECT avgWeighted(t, t) FROM test
┌─avgWeighted(t, t)─┐
│ nan │
└───────────────────┘