|
1 | 1 | ---
|
2 | 2 | title: SUM
|
3 | 3 | ---
|
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
4 | 5 |
|
5 |
| -Aggregate function. |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.697"/> |
6 | 7 |
|
7 |
| -The SUM() function calculates the sum of a set of values. |
| 8 | +Calculates the sum of a set of values. |
8 | 9 |
|
9 |
| -:::caution |
10 |
| -NULL values are not counted. |
11 |
| -::: |
| 10 | +- NULL values are ignored. |
| 11 | +- Supports numeric and interval types. |
12 | 12 |
|
13 | 13 | ## Syntax
|
14 | 14 |
|
15 |
| -``` |
| 15 | +```sql |
16 | 16 | SUM(<expr>)
|
17 | 17 | ```
|
18 | 18 |
|
19 |
| -## Arguments |
20 |
| - |
21 |
| -| Arguments | Description | |
22 |
| -|-----------|--------------------------| |
23 |
| -| `<expr>` | Any numerical expression | |
24 |
| - |
25 | 19 | ## Return Type
|
26 | 20 |
|
27 |
| -A double if the input type is double, otherwise integer. |
| 21 | +Same as the input type. |
28 | 22 |
|
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: |
30 | 26 |
|
31 |
| -**Create a Table and Insert Sample Data** |
32 | 27 | ```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 |
37 | 34 | );
|
38 | 35 |
|
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; |
45 | 49 | ```
|
46 | 50 |
|
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: |
52 | 52 |
|
53 |
| -**Result** |
54 | 53 | ```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 | +│ 60 │ 86.5 │ 240:00:00 │ |
| 61 | +└──────────────────────────────────────────────────────────┘ |
| 62 | +``` |
0 commit comments