mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-15 03:53:41 +00:00
53 lines
1.9 KiB
Markdown
53 lines
1.9 KiB
Markdown
---
|
|
toc_priority: 141
|
|
---
|
|
|
|
# sumMap {#agg_functions-summap}
|
|
|
|
**语法**
|
|
|
|
``` sql
|
|
sumMap(key, value)
|
|
或
|
|
sumMap(Tuple(key, value))
|
|
```
|
|
|
|
根据 `key` 数组中指定的键对 `value` 数组进行求和。
|
|
|
|
传递 `key` 和 `value` 数组的元组与传递 `key` 和 `value` 的两个数组是同义的。
|
|
要总计的每一行的 `key` 和 `value` (数组)元素的数量必须相同。
|
|
返回两个数组组成的一个元组: 排好序的 `key` 和对应 `key` 的 `value` 之和。
|
|
|
|
示例:
|
|
|
|
``` sql
|
|
CREATE TABLE sum_map(
|
|
date Date,
|
|
timeslot DateTime,
|
|
statusMap Nested(
|
|
status UInt16,
|
|
requests UInt64
|
|
),
|
|
statusMapTuple Tuple(Array(Int32), Array(Int32))
|
|
) ENGINE = Log;
|
|
INSERT INTO sum_map VALUES
|
|
('2000-01-01', '2000-01-01 00:00:00', [1, 2, 3], [10, 10, 10], ([1, 2, 3], [10, 10, 10])),
|
|
('2000-01-01', '2000-01-01 00:00:00', [3, 4, 5], [10, 10, 10], ([3, 4, 5], [10, 10, 10])),
|
|
('2000-01-01', '2000-01-01 00:01:00', [4, 5, 6], [10, 10, 10], ([4, 5, 6], [10, 10, 10])),
|
|
('2000-01-01', '2000-01-01 00:01:00', [6, 7, 8], [10, 10, 10], ([6, 7, 8], [10, 10, 10]));
|
|
|
|
SELECT
|
|
timeslot,
|
|
sumMap(statusMap.status, statusMap.requests),
|
|
sumMap(statusMapTuple)
|
|
FROM sum_map
|
|
GROUP BY timeslot
|
|
```
|
|
|
|
``` text
|
|
┌────────────timeslot─┬─sumMap(statusMap.status, statusMap.requests)─┬─sumMap(statusMapTuple)─────────┐
|
|
│ 2000-01-01 00:00:00 │ ([1,2,3,4,5],[10,10,20,10,10]) │ ([1,2,3,4,5],[10,10,20,10,10]) │
|
|
│ 2000-01-01 00:01:00 │ ([4,5,6,7,8],[10,10,20,10,10]) │ ([4,5,6,7,8],[10,10,20,10,10]) │
|
|
└─────────────────────┴──────────────────────────────────────────────┴────────────────────────────────┘
|
|
```
|