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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.3 KiB
Markdown
Raw Normal View History

2021-05-27 07:55:24 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/sumcount
sidebar_position: 196
2022-08-29 16:19:50 +00:00
title: sumCount
2021-05-27 07:55:24 +00:00
---
2021-06-11 03:24:34 +00:00
Calculates the sum of the numbers and counts the number of rows at the same time. The function is used by ClickHouse query optimizer: if there are multiple `sum`, `count` or `avg` functions in a query, they can be replaced to single `sumCount` function to reuse the calculations. The function is rarely needed to use explicitly.
2021-05-24 17:44:53 +00:00
**Syntax**
``` sql
sumCount(x)
```
2021-07-29 15:20:55 +00:00
**Arguments**
2021-05-24 17:44:53 +00:00
- `x` — Input value, 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).
2021-05-24 17:44:53 +00:00
**Returned value**
- Tuple `(sum, count)`, where `sum` is the sum of numbers and `count` is the number of rows with not-NULL values.
2021-05-24 17:44:53 +00:00
Type: [Tuple](../../../sql-reference/data-types/tuple.md).
**Example**
Query:
``` sql
2021-05-27 09:37:27 +00:00
CREATE TABLE s_table (x Int8) Engine = Log;
INSERT INTO s_table SELECT number FROM numbers(0, 20);
INSERT INTO s_table VALUES (NULL);
SELECT sumCount(x) from s_table;
2021-05-24 17:44:53 +00:00
```
Result:
``` text
2021-05-27 09:37:27 +00:00
┌─sumCount(x)─┐
│ (190,20) │
2021-05-25 13:05:08 +00:00
└─────────────┘
```
**See also**
- [optimize_syntax_fuse_functions](../../../operations/settings/settings.md#optimize_syntax_fuse_functions) setting.