ClickHouse/docs/zh/sql-reference/aggregate-functions/reference/summap.md

53 lines
1.9 KiB
Markdown
Raw Normal View History

2021-02-02 12:00:41 +00:00
---
2022-04-10 23:08:18 +00:00
sidebar_position: 141
2021-02-02 12:00:41 +00:00
---
# sumMap {#agg_functions-summap}
2021-02-05 15:50:14 +00:00
**语法**
2021-02-02 12:00:41 +00:00
2021-02-05 15:50:14 +00:00
``` sql
2021-03-18 16:02:22 +00:00
sumMap(key, value)
2021-02-05 15:50:14 +00:00
2021-03-18 16:02:22 +00:00
sumMap(Tuple(key, value))
2021-02-05 15:50:14 +00:00
```
2021-02-02 12:00:41 +00:00
2021-02-05 15:50:14 +00:00
根据 `key` 数组中指定的键对 `value` 数组进行求和。
2021-02-02 12:00:41 +00:00
2021-02-09 12:47:52 +00:00
传递 `key``value` 数组的元组与传递 `key``value` 的两个数组是同义的。
2021-02-05 15:50:14 +00:00
要总计的每一行的 `key``value` (数组)元素的数量必须相同。
返回两个数组组成的一个元组: 排好序的 `key` 和对应 `key``value` 之和。
2021-02-02 12:00:41 +00:00
2021-02-05 15:50:14 +00:00
示例:
2021-02-02 12:00:41 +00:00
``` 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]) │
└─────────────────────┴──────────────────────────────────────────────┴────────────────────────────────┘
```