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

> Calcula a média aritmética ponderada.

# avgWeighted

<div id="avgWeighted">
  ## avgWeighted
</div>

Introduzido em: v20.1.0

Calcula a [média aritmética ponderada](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean).

**Sintaxe**

```sql theme={null}
avgWeighted(x, weight)
```

**Argumentos**

* `x` — Valores. [`(U)Int*`](/pt-BR/reference/data-types/int-uint) ou [`Float*`](/pt-BR/reference/data-types/float)
* `weight` — Pesos dos valores. [`(U)Int*`](/pt-BR/reference/data-types/int-uint) ou [`Float*`](/pt-BR/reference/data-types/float)

**Valor retornado**

Retorna `NaN` se todos os pesos forem iguais a 0 ou se o parâmetro de pesos fornecido estiver vazio; caso contrário, retorna a média ponderada. [`Float64`](/pt-BR/reference/data-types/float)

**Exemplos**

**Exemplo de uso**

```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 │
└───────────────────┘
```

**Pesos mistos: inteiros e de ponto flutuante**

```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 │
└───────────────────┘
```

**Se todos os pesos forem zero, retorna 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 │
└───────────────────┘
```

**Uma tabela vazia retorna 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 │
└───────────────────┘
```
