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

> 返回指定 列中近似最常见值组成的数组。结果数组按值的近似出现频率降序排序 （而不是按值本身排序）。此外，还会将值的权重纳入考量。

# topKWeighted

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

引入版本：v1.1.0

返回一个数组，其中包含指定列中近似最常出现的值。
结果数组按值的近似出现频率降序排列 (而不是按值本身排序) 。
此外，还会将值的权重考虑在内。

**另请参见**

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

**语法**

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

**参数**

* `N` — 要返回的元素个数。默认值：10。[`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` — 要查找出现频率最高的值所在的列名。 - `weight` — 权重。在频率计算中，每个值会按 `weight` 次计入。[`UInt64`](/zh/reference/data-types/int-uint)

**返回值**

返回一个数组，其中包含近似权重总和最大的值。[`Array`](/zh/reference/data-types/array)

**示例**

**用法示例**

```sql title=Query theme={null}
SELECT topKWeighted(2)(k, w) FROM
VALUES('k Char, w UInt64', ('y', 1), ('y', 1), ('x', 5), ('y', 1), ('z', 10));
```

```response title=Response theme={null}
┌─topKWeighted(2)(k, w)──┐
│ ['z','x']              │
└────────────────────────┘
```

**使用 counts 参数**

```sql title=Query theme={null}
SELECT topKWeighted(2, 10, 'counts')(k, w)
FROM VALUES('k Char, w UInt64', ('y', 1), ('y', 1), ('x', 5), ('y', 1), ('z', 10));
```

```response title=Response theme={null}
┌─topKWeighted(2, 10, 'counts')(k, w)─┐
│ [('z',10,0),('x',5,0)]              │
└─────────────────────────────────────┘
```

**另请参阅**

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