ClickHouse/docs/en/sql-reference/aggregate-functions/reference/avgweighted.md

70 lines
1.4 KiB
Markdown
Raw Normal View History

---
toc_priority: 107
---
# avgWeighted {#avgweighted}
Calculates the [weighted arithmetic mean](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean).
**Syntax**
``` sql
avgWeighted(x, weight)
```
**Parameters**
2020-10-19 15:23:35 +00:00
- `x` — Values.
- `weight` — Weights of the values.
2020-10-19 15:23:35 +00:00
`x` and `weight` must both be
[Integer](../../../sql-reference/data-types/int-uint.md),
[floating-point](../../../sql-reference/data-types/float.md), or
[Decimal](../../../sql-reference/data-types/decimal.md),
but may have different types.
**Returned value**
- `NaN`. If all the weights are equal to 0.
2020-10-19 15:23:35 +00:00
- Weighted mean otherwise.
2020-10-19 15:23:35 +00:00
**Return type**
2020-10-22 15:47:21 +00:00
- `Decimal` if both types are [Decimal](../../../sql-reference/data-types/decimal.md)
or if one type is Decimal and other is Integer.
- [Float64](../../../sql-reference/data-types/float.md) otherwise.
**Example**
Query:
``` sql
SELECT avgWeighted(x, w)
FROM values('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
```
Result:
``` text
┌─avgWeighted(x, weight)─┐
│ 8 │
└────────────────────────┘
```
2020-10-15 10:36:00 +00:00
**Example**
Query:
``` sql
SELECT avgWeighted(x, w)
FROM values('x Int8, w Float64', (4, 1), (1, 0), (10, 2))
```
Result:
``` text
┌─avgWeighted(x, weight)─┐
│ 8 │
└────────────────────────┘
```