# avgWeighted (/docs/reference/sql/aggregate-functions/avgweighted)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

Calculates the [weighted arithmetic mean](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean).

**Syntax**

```sql
avgWeighted(x, weight)
```

**Arguments**

* `x` — Values. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)
* `weight` — Weights of the values. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**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`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
```

```text title=Response
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**Mixed integer and float weights**

```sql title=Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Float64', (4, 1), (1, 0), (10, 2))
```

```text title=Response
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**All weights are zero returns NaN**

```sql title=Query
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (0, 0), (1, 0), (10, 0))
```

```text title=Response
┌─avgWeighted(x, w)─┐
│               nan │
└───────────────────┘
```

**Empty table returns NaN**

```sql title=Query
CREATE TABLE test (t UInt8) ENGINE = Memory;
SELECT avgWeighted(t, t) FROM test
```

```text title=Response
┌─avgWeighted(t, t)─┐
│               nan │
└───────────────────┘
```

*Introduced in version 20.1.0.*
