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

# avgMap

> avgMap コンビネータ の使用例

<div id="description">
  ## 説明
</div>

[`Map`](/ja/reference/functions/aggregate-functions/combinators#-map) コンビネータは [`avg`](/ja/reference/functions/aggregate-functions/avg)
関数に適用でき、`avgMap`
集約コンビネータ関数を使用して、Map 内の値の算術平均をキーごとに計算します。

<div id="example-usage">
  ## 使用例
</div>

この例では、異なるタイムスロットにおけるステータスコードとその件数を格納するテーブルを作成します。
各行には、ステータスコードをキー、対応する件数を値とする Map が含まれます。`avgMap` を使用して、
各タイムスロットにおける各ステータスコードの平均件数を計算します。

```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;
```

`avgMap` 関数は、各タイムスロット内の各ステータスコードの平均件数を計算します。たとえば:

* タイムスロット '2000-01-01 00:00:00' では:
  * ステータス 'a': 15
  * ステータス 'b': 25
  * ステータス 'c': (35 + 45) / 2 = 40
  * ステータス 'd': 55
  * ステータス 'e': 65
* タイムスロット '2000-01-01 00:01:00' では:
  * ステータス 'd': 75
  * ステータス 'e': 85
  * ステータス 'f': (95 + 105) / 2 = 100
  * ステータス '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} │
   └─────────────────────┴──────────────────────────────────────┘
```

<div id="see-also">
  ## 関連項目
</div>

* [`avg`](/ja/reference/functions/aggregate-functions/avg)
* [`Map コンビネータ`](/ja/reference/functions/aggregate-functions/combinators#-map)
