> ## 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.

> 返回指定列中近似最常见的值组成的数组。结果数组按值的近似出现频率降序排列（而非按值本身排序）。

# topK

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

Introduced in: v1.1.0

返回指定列中近似最常见的值组成的数组。结果数组按值出现频率的近似值降序排序 (而不是按值本身排序) 。

该函数实现了用于分析 TopK 的 [Filtered Space-Saving](https://doi.org/10.1016/j.ins.2010.08.024) 算法，该算法基于 [Parallel Space Saving](https://doi.org/10.1016/j.ins.2015.09.003) 中的 reduce-and-combine 算法。

此函数不保证结果的准确性。在某些情况下，可能会出现误差，并返回出现频率较高但并非最高频的值。

**See Also**

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

**Syntax**

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

**参数**

* `N` — 要返回的元素个数。默认值：10。`N` 的最大值为 `65536`。[`UInt64`](/zh/reference/data-types/int-uint)
* `load_factor` — 可选。定义为这些值预留多少个单元。如果 `uniq(column) > N * load_factor`，则 topK 函数的结果将为近似值。默认值：3。[`UInt64`](/zh/reference/data-types/int-uint)
* `counts` — 可选。定义结果是否应包含近似计数和误差值。[`Bool`](/zh/reference/data-types/boolean)

**参数**

* `column` — 要查找其最常见值的列名。[`String`](/zh/reference/data-types/string)

**返回值**

返回一个由近似最常见的值组成的数组，并按近似频率降序排序。[`Array`](/zh/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](/zh/reference/functions/aggregate-functions/topKWeighted)
* [approx\_top\_k](/zh/reference/functions/aggregate-functions/approxtopk)
* [approx\_top\_sum](/zh/reference/functions/aggregate-functions/approxtopsum)
