Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
{
"title": "AGG_ARRAY_SUM",
"language": "en"
}
---

## Description

Calculate the sum of elements at each position in the input array and return a new array

## Syntax

```sql
AGG_ARRAY_SUM(<expr>)
```

## Parameters

| Parameter | Description |
| -- |----------------------------------------------------------------|
| `<expr>` | Expression to be summed, an Array with element type Numerical. |

## Return Value

Returns a new array containing the sum of elements at each position of the array.
If there is no valid data within the group, return an empty array.

## Example

```sql
-- setup
CREATE TABLE agg_array_sum_test (
`group_id` bigint(20) NOT NULL,
`array_column` array<bigint(20)> NULL
) ENGINE=OLAP
duplicate KEY(`group_id`)
DISTRIBUTED BY HASH(`group_id`) BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false"
);
insert into agg_array_sum_test values (1,[1,2,3]),(1,[4,5,6]),(2,[10,20]), (2,[40,50,60]), (2, NULL), (3,[30, 50]), (3,[20, null]), (4, null);
```

```sql
select group_id, agg_array_sum(array_column) as sum_array from agg_array_sum_test group by group_id;
```

```text
+----------+--------------+
| group_id | sum_array |
+----------+--------------+
| 2 | [50, 70, 60] |
| 3 | [50, 50] |
| 1 | [5, 7, 9] |
| 4 | [] |
+----------+--------------+
```

```sql
select agg_array_sum(array_column) as sum_array from agg_array_sum_test where array_column is null;
```

```text
+-----------+
| sum_array |
+-----------+
| [] |
+-----------+
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
{
"title": "AGG_ARRAY_SUM",
"language": "zh-CN"
}
---

## 描述

计算输入数组每一位置元素和,返回一个新的数组

## 语法

```sql
AGG_ARRAY_SUM(<expr>)
```

## 参数

| 参数 | 说明 |
| -- |---------------------------------------------|
| `<expr>` | 需要求和的表达式,元素类型为Numerical的Array。 |

## 返回值

返回一个包含数组每个位置元素和的新数组。
如果组内没有合法数据,则返回空数组。

## 举例

```sql
-- setup
CREATE TABLE agg_array_sum_test (
`group_id` bigint(20) NOT NULL,
`array_column` array<bigint(20)> NULL
) ENGINE=OLAP
duplicate KEY(`group_id`)
DISTRIBUTED BY HASH(`group_id`) BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false"
);
insert into agg_array_sum_test values (1,[1,2,3]),(1,[4,5,6]),(2,[10,20]), (2,[40,50,60]), (2, NULL), (3,[30, 50]), (3,[20, null]), (4, null);
```

```sql
select group_id, agg_array_sum(array_column) as sum_array from agg_array_sum_test group by group_id;
```

```text
+----------+--------------+
| group_id | sum_array |
+----------+--------------+
| 2 | [50, 70, 60] |
| 3 | [50, 50] |
| 1 | [5, 7, 9] |
| 4 | [] |
+----------+--------------+
```

```sql
select agg_array_sum(array_column) as sum_array from agg_array_sum_test where array_column is null;
```

```text
+-----------+
| sum_array |
+-----------+
| [] |
+-----------+
```
1 change: 1 addition & 0 deletions sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@
"sql-manual/sql-functions/aggregate-functions/count-by-enum",
"sql-manual/sql-functions/aggregate-functions/covar",
"sql-manual/sql-functions/aggregate-functions/covar-samp",
"sql-manual/sql-functions/aggregate-functions/agg-array-sum",
"sql-manual/sql-functions/aggregate-functions/group-array-intersect",
"sql-manual/sql-functions/aggregate-functions/group-array-union",
"sql-manual/sql-functions/aggregate-functions/group-bit-and",
Expand Down