Skip to content

Commit 4811177

Browse files
authored
Update aggregate-sum.md (#1658)
1 parent 6158dff commit 4811177

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed
Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,62 @@
11
---
22
title: SUM
33
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
45

5-
Aggregate function.
6+
<FunctionDescription description="Introduced or updated: v1.2.697"/>
67

7-
The SUM() function calculates the sum of a set of values.
8+
Calculates the sum of a set of values.
89

9-
:::caution
10-
NULL values are not counted.
11-
:::
10+
- NULL values are ignored.
11+
- Supports numeric and interval types.
1212

1313
## Syntax
1414

15-
```
15+
```sql
1616
SUM(<expr>)
1717
```
1818

19-
## Arguments
20-
21-
| Arguments | Description |
22-
|-----------|--------------------------|
23-
| `<expr>` | Any numerical expression |
24-
2519
## Return Type
2620

27-
A double if the input type is double, otherwise integer.
21+
Same as the input type.
2822

29-
## Example
23+
## Examples
24+
25+
This example demonstrates how to create a table with INTEGER, DOUBLE, and INTERVAL columns, insert data, and use SUM to calculate the total for each column:
3026

31-
**Create a Table and Insert Sample Data**
3227
```sql
33-
CREATE TABLE sales_data (
34-
id INT,
35-
product_id INT,
36-
quantity INT
28+
-- Create a table with integer, double, and interval columns
29+
CREATE TABLE sum_example (
30+
id INT,
31+
int_col INTEGER,
32+
double_col DOUBLE,
33+
interval_col INTERVAL
3734
);
3835

39-
INSERT INTO sales_data (id, product_id, quantity)
40-
VALUES (1, 1, 10),
41-
(2, 2, 5),
42-
(3, 3, 8),
43-
(4, 4, 3),
44-
(5, 5, 15);
36+
-- Insert data
37+
INSERT INTO sum_example VALUES
38+
(1, 10, 15.5, INTERVAL '2 days'),
39+
(2, 20, 25.7, INTERVAL '3 days'),
40+
(3, NULL, 5.2, INTERVAL '1 day'),
41+
(4, 30, 40.1, INTERVAL '4 days');
42+
43+
-- Calculate the sum for each column
44+
SELECT
45+
SUM(int_col) AS total_integer,
46+
SUM(double_col) AS total_double,
47+
SUM(interval_col) AS total_interval
48+
FROM sum_example;
4549
```
4650

47-
**Query Demo: Calculate the Total Quantity of Products Sold**
48-
```sql
49-
SELECT SUM(quantity) AS total_quantity_sold
50-
FROM sales_data;
51-
```
51+
Expected Output:
5252

53-
**Result**
5453
```sql
55-
| total_quantity_sold |
56-
|---------------------|
57-
| 41 |
58-
```
54+
-- NULL values are ignored.
55+
-- SUM(interval_col) returns 240:00:00 (10 days).
56+
57+
┌──────────────────────────────────────────────────────────┐
58+
│ total_integer │ total_double │ total_interval │
59+
├─────────────────┼───────────────────┼────────────────────┤
60+
6086.5240:00:00
61+
└──────────────────────────────────────────────────────────┘
62+
```

0 commit comments

Comments
 (0)