-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3d4df4d
commit 14c1b34
Showing
1 changed file
with
37 additions
and
33 deletions.
There are no files selected for viewing
70 changes: 37 additions & 33 deletions
70
docs/cn/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-sum.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,62 @@ | ||
--- | ||
title: SUM | ||
--- | ||
import FunctionDescription from '@site/src/components/FunctionDescription'; | ||
|
||
聚合函数。 | ||
<FunctionDescription description="引入或更新于:v1.2.697"/> | ||
|
||
SUM() 函数计算一组值的总和。 | ||
计算一组值的总和。 | ||
|
||
:::caution | ||
NULL 值不计入总数。 | ||
::: | ||
- NULL 值会被忽略。 | ||
- 支持数值和间隔类型。 | ||
|
||
## 语法 | ||
|
||
``` | ||
```sql | ||
SUM(<expr>) | ||
``` | ||
|
||
## 参数 | ||
|
||
| 参数 | 描述 | | ||
|-----------|--------------------| | ||
| `<expr>` | 任何数值表达式 | | ||
|
||
## 返回类型 | ||
|
||
如果输入类型是双精度浮点数,则返回双精度浮点数;否则返回整数。 | ||
与输入类型相同。 | ||
|
||
## 示例 | ||
|
||
**创建表并插入示例数据** | ||
此示例演示了如何创建一个包含 INTEGER、DOUBLE 和 INTERVAL 列的表,插入数据,并使用 SUM 计算每列的总和: | ||
|
||
```sql | ||
CREATE TABLE sales_data ( | ||
id INT, | ||
product_id INT, | ||
quantity INT | ||
-- 创建一个包含整数、双精度和间隔列的表 | ||
CREATE TABLE sum_example ( | ||
id INT, | ||
int_col INTEGER, | ||
double_col DOUBLE, | ||
interval_col INTERVAL | ||
); | ||
|
||
INSERT INTO sales_data (id, product_id, quantity) | ||
VALUES (1, 1, 10), | ||
(2, 2, 5), | ||
(3, 3, 8), | ||
(4, 4, 3), | ||
(5, 5, 15); | ||
-- 插入数据 | ||
INSERT INTO sum_example VALUES | ||
(1, 10, 15.5, INTERVAL '2 days'), | ||
(2, 20, 25.7, INTERVAL '3 days'), | ||
(3, NULL, 5.2, INTERVAL '1 day'), | ||
(4, 30, 40.1, INTERVAL '4 days'); | ||
|
||
-- 计算每列的总和 | ||
SELECT | ||
SUM(int_col) AS total_integer, | ||
SUM(double_col) AS total_double, | ||
SUM(interval_col) AS total_interval | ||
FROM sum_example; | ||
``` | ||
|
||
**查询示例:计算产品销售总量** | ||
```sql | ||
SELECT SUM(quantity) AS total_quantity_sold | ||
FROM sales_data; | ||
``` | ||
预期输出: | ||
|
||
**结果** | ||
```sql | ||
| total_quantity_sold | | ||
|---------------------| | ||
| 41 | | ||
-- NULL 值被忽略。 | ||
-- SUM(interval_col) 返回 240:00:00(10 天)。 | ||
|
||
┌──────────────────────────────────────────────────────────┐ | ||
│ total_integer │ total_double │ total_interval │ | ||
├─────────────────┼───────────────────┼────────────────────┤ | ||
│ 60 │ 86.5 │ 240:00:00 │ | ||
└──────────────────────────────────────────────────────────┘ | ||
``` |