跳转到主要内容

topKWeighted

引入版本:v1.1.0 返回一个数组,其中包含指定列中近似最常出现的值。 结果数组按值的近似出现频率降序排列 (而不是按值本身排序) 。 此外,还会将值的权重考虑在内。 另请参见 语法
topKWeighted(N)(column, weight)
topKWeighted(N, load_factor)(column, weight)
topKWeighted(N, load_factor, 'counts')(column, weight)
参数
  • N — 要返回的元素个数。默认值:10。UInt64
  • load_factor — 可选。定义为这些值预留多少个单元。如果 uniq(column) > N * load_factor,则 topK 函数的结果将为近似值。默认值:3。UInt64
  • counts — 可选。定义结果是否应包含近似计数和误差值。Bool
参数
  • column — 要查找出现频率最高的值所在的列名。 - weight — 权重。在频率计算中,每个值会按 weight 次计入。UInt64
返回值 返回一个数组,其中包含近似权重总和最大的值。Array 示例 用法示例
Query
SELECT topKWeighted(2)(k, w) FROM
VALUES('k Char, w UInt64', ('y', 1), ('y', 1), ('x', 5), ('y', 1), ('z', 10));
Response
┌─topKWeighted(2)(k, w)──┐
│ ['z','x']              │
└────────────────────────┘
使用 counts 参数
Query
SELECT topKWeighted(2, 10, 'counts')(k, w)
FROM VALUES('k Char, w UInt64', ('y', 1), ('y', 1), ('x', 5), ('y', 1), ('z', 10));
Response
┌─topKWeighted(2, 10, 'counts')(k, w)─┐
│ [('z',10,0),('x',5,0)]              │
└─────────────────────────────────────┘
另请参阅
最后修改于 2026年6月10日