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

63 lines
1.1 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/sum
sidebar_position: 195
---
2022-06-02 10:55:18 +00:00
# sum
Calculates the sum. Only works for numbers.
2023-01-26 14:33:30 +00:00
2024-04-27 16:27:36 +00:00
**Syntax**
```sql
sum(num)
```
**Parameters**
- `num`: Column of numeric values. [(U)Int*](../../data-types/int-uint.md), [Float*](../../data-types/float.md), [Decimal*](../../data-types/decimal.md).
**Returned value**
- The sum of the values. [(U)Int*](../../data-types/int-uint.md), [Float*](../../data-types/float.md), [Decimal*](../../data-types/decimal.md).
**Example**
First we create a table `employees` and insert some fictional employee data into it.
Query:
```sql
CREATE TABLE employees
(
`id` UInt32,
`name` String,
`salary` UInt32
)
ENGINE = Log
2023-01-26 14:33:30 +00:00
```
2024-04-27 16:27:36 +00:00
```sql
INSERT INTO employees VALUES
(87432, 'John Smith', 45680),
(59018, 'Jane Smith', 72350),
(20376, 'Ivan Ivanovich', 58900),
(71245, 'Anastasia Ivanovna', 89210);
```
We query for the total amount of the employee salaries using the `sum` function.
Query:
```sql
2023-01-26 14:33:30 +00:00
SELECT sum(salary) FROM employees;
```
2024-04-27 16:27:36 +00:00
Result:
```response
┌─sum(salary)─┐
1. │ 266140 │
└─────────────┘
```