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

68 lines
1.1 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/avg
sidebar_position: 5
---
2022-06-02 10:55:18 +00:00
# avg
2020-10-30 16:17:57 +00:00
Calculates the arithmetic mean.
**Syntax**
``` sql
2021-02-02 12:26:36 +00:00
avg(x)
2020-10-30 16:17:57 +00:00
```
**Arguments**
2020-10-30 16:17:57 +00:00
- `x` — input values, must be [Integer](../../../sql-reference/data-types/int-uint.md), [Float](../../../sql-reference/data-types/float.md), or [Decimal](../../../sql-reference/data-types/decimal.md).
2020-10-30 16:17:57 +00:00
**Returned value**
- The arithmetic mean, always as [Float64](../../../sql-reference/data-types/float.md).
- `NaN` if the input parameter `x` is empty.
2020-10-30 16:17:57 +00:00
**Example**
Query:
``` sql
SELECT avg(x) FROM values('x Int8', 0, 1, 2, 3, 4, 5);
2020-10-30 16:17:57 +00:00
```
Result:
``` text
┌─avg(x)─┐
│ 2.5 │
└────────┘
```
**Example**
Create a temp table:
2020-10-30 16:17:57 +00:00
Query:
``` sql
CREATE table test (t UInt8) ENGINE = Memory;
```
2021-07-29 15:20:55 +00:00
Get the arithmetic mean:
Query:
```
SELECT avg(t) FROM test;
2020-10-30 16:17:57 +00:00
```
Result:
``` text
┌─avg(x)─┐
2020-10-30 18:08:33 +00:00
│ nan │
2020-10-30 16:17:57 +00:00
└────────┘
```
[Original article](https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/avg/) <!--hide-->