2020-06-18 08:24:31 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/summap
2022-04-09 13:29:05 +00:00
sidebar_position: 141
2020-06-18 08:24:31 +00:00
---
2022-06-02 10:55:18 +00:00
# sumMap
2020-06-18 08:24:31 +00:00
2024-04-27 18:58:34 +00:00
Totals a `value` array according to the keys specified in the `key` array. Returns a tuple of two arrays: keys in sorted order, and values summed for the corresponding keys without overflow.
2023-06-04 20:19:07 +00:00
2024-04-27 15:45:32 +00:00
**Syntax**
- `sumMap(key <Array>, value <Array>)` [Array type ](../../data-types/array.md ).
- `sumMap(Tuple(key <Array>, value <Array>))` [Tuple type ](../../data-types/tuple.md ).
2023-06-04 19:18:53 +00:00
Alias: `sumMappedArrays` .
2020-06-18 08:24:31 +00:00
2024-04-27 15:45:32 +00:00
**Arguments**
2024-04-28 17:30:00 +00:00
- `key` : [Array ](../../data-types/array.md ) of keys.
- `value` : [Array ](../../data-types/array.md ) of values.
2020-06-18 08:24:31 +00:00
2024-04-29 09:27:44 +00:00
Passing a tuple of key and value arrays is a synonym to passing separately an array of keys and an array of values.
2020-06-18 08:24:31 +00:00
2024-04-27 15:45:32 +00:00
:::note
2020-06-18 08:24:31 +00:00
The number of elements in `key` and `value` must be the same for each row that is totaled.
2024-04-27 15:45:32 +00:00
:::
2024-04-27 18:58:34 +00:00
**Returned Value**
- Returns a tuple of two arrays: keys in sorted order, and values summed for the corresponding keys.
2024-04-27 15:45:32 +00:00
**Example**
2020-06-18 08:24:31 +00:00
2024-04-27 15:45:32 +00:00
First we create a table called `sum_map` , and insert some data into it. Arrays of keys and values are stored separately as a column called `statusMap` of [Nested ](../../data-types/nested-data-structures/index.md ) type, and together as a column called `statusMapTuple` of [tuple ](../../data-types/tuple.md ) type to illustrate the use of the two different syntaxes of this function described above.
2020-06-18 08:24:31 +00:00
2024-04-27 15:45:32 +00:00
Query:
2020-06-18 08:24:31 +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;
2024-04-27 15:45:32 +00:00
```
```sql
2020-06-18 08:24:31 +00:00
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]));
2024-04-27 15:45:32 +00:00
```
Next, we query the table using the `sumMap` function, making use of both array and tuple type syntaxes:
Query:
2020-06-18 08:24:31 +00:00
2024-04-27 15:45:32 +00:00
``` sql
2020-06-18 08:24:31 +00:00
SELECT
timeslot,
sumMap(statusMap.status, statusMap.requests),
sumMap(statusMapTuple)
FROM sum_map
GROUP BY timeslot
```
2024-04-27 15:45:32 +00:00
Result:
2020-06-18 08:24:31 +00:00
``` 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]) │
└─────────────────────┴──────────────────────────────────────────────┴────────────────────────────────┘
```
2023-06-04 19:18:53 +00:00
**See Also**
2024-04-27 18:58:34 +00:00
2024-04-27 15:45:32 +00:00
- [Map combinator for Map datatype ](../combinators.md#-map )
2024-04-27 18:58:34 +00:00
- [sumMapWithOverflow ](../reference/summapwithoverflow.md )