> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# avgMap

> Example of using the avgMap combinator

<h2 id="description">
  Description
</h2>

The [`Map`](/reference/functions/aggregate-functions/combinators#-map) combinator can be applied to the [`avg`](/reference/functions/aggregate-functions/avg)
function to calculate the arithmetic mean of values in a Map according to each key, using the `avgMap`
aggregate combinator function.

<h2 id="example-usage">
  Example usage
</h2>

In this example, we'll create a table that stores status codes and their counts for different timeslots,
where each row contains a Map of status codes to their corresponding counts. We'll use
`avgMap` to calculate the average count for each status code within each timeslot.

```sql title="Query" theme={null}
CREATE TABLE metrics(
    date Date,
    timeslot DateTime,
    status Map(String, UInt64)
) ENGINE = MergeTree
ORDER BY ();

INSERT INTO metrics VALUES
    ('2000-01-01', '2000-01-01 00:00:00', (['a', 'b', 'c'], [15, 25, 35])),
    ('2000-01-01', '2000-01-01 00:00:00', (['c', 'd', 'e'], [45, 55, 65])),
    ('2000-01-01', '2000-01-01 00:01:00', (['d', 'e', 'f'], [75, 85, 95])),
    ('2000-01-01', '2000-01-01 00:01:00', (['f', 'g', 'g'], [105, 115, 125]));

SELECT
    timeslot,
    avgMap(status),
FROM metrics
GROUP BY timeslot;
```

The `avgMap` function will calculate the average count for each status code within each timeslot. For example:

* In timeslot '2000-01-01 00:00:00':
  * Status 'a': 15
  * Status 'b': 25
  * Status 'c': (35 + 45) / 2 = 40
  * Status 'd': 55
  * Status 'e': 65
* In timeslot '2000-01-01 00:01:00':
  * Status 'd': 75
  * Status 'e': 85
  * Status 'f': (95 + 105) / 2 = 100
  * Status 'g': (115 + 125) / 2 = 120

```response title="Response" theme={null}
   ┌────────────timeslot─┬─avgMap(status)───────────────────────┐
1. │ 2000-01-01 00:01:00 │ {'d':75,'e':85,'f':100,'g':120}      │
2. │ 2000-01-01 00:00:00 │ {'a':15,'b':25,'c':40,'d':55,'e':65} │
   └─────────────────────┴──────────────────────────────────────┘
```

<h2 id="see-also">
  See also
</h2>

* [`avg`](/reference/functions/aggregate-functions/avg)
* [`Map combinator`](/reference/functions/aggregate-functions/combinators#-map)
