2021-02-02 12:00:41 +00:00
|
|
|
|
---
|
2022-08-26 19:07:59 +00:00
|
|
|
|
slug: /zh/sql-reference/aggregate-functions/reference/topkweighted
|
2022-04-10 23:08:18 +00:00
|
|
|
|
sidebar_position: 109
|
2021-02-02 12:00:41 +00:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# topKWeighted {#topkweighted}
|
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
类似于 `topK` 但需要一个整数类型的附加参数 - `weight`。 每个输入都被记入 `weight` 次频率计算。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
**语法**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
topKWeighted(N)(x, weight)
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
**参数**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-07 14:18:15 +00:00
|
|
|
|
- `N` — 要返回的元素数。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
**参数**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-07 14:18:15 +00:00
|
|
|
|
- `x` – (要计算频次的)值。
|
2021-03-02 11:33:37 +00:00
|
|
|
|
- `weight` — 权重。 [UInt8](../../../sql-reference/data-types/int-uint.md)类型。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
**返回值**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
返回具有最大近似权重总和的值数组。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
**示例**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT topKWeighted(10)(number, number) FROM numbers(1000)
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-02 11:33:37 +00:00
|
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
|
|
``` text
|
|
|
|
|
┌─topKWeighted(10)(number, number)──────────┐
|
|
|
|
|
│ [999,998,997,996,995,994,993,992,991,990] │
|
|
|
|
|
└───────────────────────────────────────────┘
|
|
|
|
|
```
|