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

> 指定された カラム内でおおよそ最も頻出する値の配列を返します。結果の配列は、値そのものではなく、値のおおよその出現頻度の降順でソートされます。

# topK

<div id="topK">
  ## topK
</div>

導入バージョン: v1.1.0

指定したカラム内で、概ね出現頻度が最も高い値の配列を返します。返される配列は、値そのものではなく、値のおおよその出現頻度の降順でソートされます。

TopK の分析には、[Parallel Space Saving](https://doi.org/10.1016/j.ins.2015.09.003) の reduce-and-combine アルゴリズムに基づく [Filtered Space-Saving](https://doi.org/10.1016/j.ins.2010.08.024) アルゴリズムを実装しています。

この関数は結果を保証しません。状況によっては誤差が生じ、最も頻出する値ではない頻出値を返すことがあります。

**関連項目**

* [topKWeighted](/ja/reference/functions/aggregate-functions/topKWeighted)
* [approx\_top\_k](/ja/reference/functions/aggregate-functions/approxtopk)
* [approx\_top\_sum](/ja/reference/functions/aggregate-functions/approxtopsum)

**構文**

```sql theme={null}
topK(N)(column)
topK(N, load_factor)(column)
topK(N, load_factor, 'counts')(column)
```

**パラメータ**

* `N` — 返す要素数。デフォルト値: 10。`N` の最大値は `65536` です。[`UInt64`](/ja/reference/data-types/int-uint)
* `load_factor` — 任意。値用に予約する cell の数を指定します。`uniq(column) > N * load_factor` の場合、topK 関数の結果は近似値になります。デフォルト値: 3。[`UInt64`](/ja/reference/data-types/int-uint)
* `counts` — 任意。結果に近似カウントと error 値を含めるかどうかを指定します。[`Bool`](/ja/reference/data-types/boolean)

**引数**

* `column` — 最も頻出する値を見つける対象のカラム名。[`String`](/ja/reference/data-types/string)

**戻り値**

おおよその出現頻度の高い値を、近似頻度の降順でソートした配列を返します。[`Array`](/ja/reference/data-types/array)

**例**

**使用例**

```sql title=Query theme={null}
SELECT topK(3)(AirlineID) AS res
FROM ontime;
```

```response title=Response theme={null}
┌─res─────────────────┐
│ [19393,19790,19805] │
└─────────────────────┘
```

**関連項目**

* [topKWeighted](/ja/reference/functions/aggregate-functions/topKWeighted)
* [approx\_top\_k](/ja/reference/functions/aggregate-functions/approxtopk)
* [approx\_top\_sum](/ja/reference/functions/aggregate-functions/approxtopsum)
