Skip to main content

avgWeighted

Introduced in: v20.1.0 Calculates the weighted arithmetic mean. Syntax
avgWeighted(x, weight)
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
Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
Response
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
Mixed integer and float weights
Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Float64', (4, 1), (1, 0), (10, 2))
Response
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
All weights are zero returns NaN
Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (0, 0), (1, 0), (10, 0))
Response
┌─avgWeighted(x, w)─┐
│               nan │
└───────────────────┘
Empty table returns NaN
Query
CREATE TABLE test (t UInt8) ENGINE = Memory;
SELECT avgWeighted(t, t) FROM test
Response
┌─avgWeighted(t, t)─┐
│               nan │
└───────────────────┘
Last modified on June 8, 2026