2021-02-02 12:00:41 +00:00
|
|
|
---
|
2022-08-26 19:07:59 +00:00
|
|
|
slug: /zh/sql-reference/aggregate-functions/reference/avgweighted
|
2022-04-10 23:08:18 +00:00
|
|
|
sidebar_position: 107
|
2021-02-02 12:00:41 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# avgWeighted {#avgweighted}
|
|
|
|
|
|
|
|
|
2021-03-17 13:06:35 +00:00
|
|
|
计算 [加权算术平均值](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean)。
|
2021-02-03 15:22:18 +00:00
|
|
|
|
|
|
|
**语法**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
avgWeighted(x, weight)
|
|
|
|
```
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
**参数**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-07-29 15:20:55 +00:00
|
|
|
- `x` — 值。
|
|
|
|
- `weight` — 值的加权。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
`x` 和 `weight` 的类型必须是
|
|
|
|
[整数](../../../sql-reference/data-types/int-uint.md), 或
|
|
|
|
[浮点数](../../../sql-reference/data-types/float.md), 或
|
|
|
|
[定点数](../../../sql-reference/data-types/decimal.md),
|
|
|
|
但是可以不一样。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
**返回值**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
- `NaN`。 如果所有的权重都等于0 或所提供的权重参数是空。
|
|
|
|
- 加权平均值。 其他。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-05 04:14:07 +00:00
|
|
|
类型: 总是[Float64](../../../sql-reference/data-types/float.md).
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
**示例**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT avgWeighted(x, w)
|
|
|
|
FROM values('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
|
|
|
|
```
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─avgWeighted(x, weight)─┐
|
|
|
|
│ 8 │
|
|
|
|
└────────────────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
**示例**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT avgWeighted(x, w)
|
|
|
|
FROM values('x Int8, w Int8', (0, 0), (1, 0), (10, 0))
|
|
|
|
```
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─avgWeighted(x, weight)─┐
|
|
|
|
│ nan │
|
|
|
|
└────────────────────────┘
|
|
|
|
```
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
**示例**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
CREATE table test (t UInt8) ENGINE = Memory;
|
|
|
|
SELECT avgWeighted(t) FROM test
|
|
|
|
```
|
|
|
|
|
2021-02-03 15:22:18 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─avgWeighted(x, weight)─┐
|
|
|
|
│ nan │
|
|
|
|
└────────────────────────┘
|
|
|
|
```
|