> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Calculates the weighted arithmetic mean.

# avgWeighted

<h2 id="avgWeighted">
  avgWeighted
</h2>

Introduced in: v20.1.0

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

**Syntax**

```sql theme={null}
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 theme={null}
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
```

```response title=Response theme={null}
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**Mixed integer and float weights**

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

```response title=Response theme={null}
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**All weights are zero returns NaN**

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

```response title=Response theme={null}
┌─avgWeighted(x, w)─┐
│               nan │
└───────────────────┘
```

**Empty table returns NaN**

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

```response title=Response theme={null}
┌─avgWeighted(t, t)─┐
│               nan │
└───────────────────┘
```
