APPROX_HISTOGRAM
功能描述
approx_histogramcolnSTRUCTminmaxcount参数说明
colcol:需要生成直方图的数值型列。nn:直方图的桶数,必须是一个大于 0 的整数常量。
返回结果
函数返回一个结构体数组,每个结构体(代表一个桶)包含三个字段:
minmin:直方图桶的最小值。maxmax:直方图桶的最大值。countcount:落在该桶内的数据数量。
使用示例
以下示例展示了如何使用
approx_histogram示例 1:生成 2 个桶的直方图
SELECT approx_histogram(col, 2) FROM VALUES (0), (1), (2), (3), (4) AS t(col); +-------------------------------------------------------------------+ | approx_histogram(col, 2) | +-------------------------------------------------------------------+ | [{"min":0.0,"max":0.5,"count":"1"},{"min":0.5,"max":4.0,"count":"4"}] | +-------------------------------------------------------------------+
示例 2:生成 5 个桶的直方图
SELECT approx_histogram(col, 5) FROM VALUES (0), (1), (2), (3), (4) AS t(col); +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | approx_histogram(col, 5) | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [{"min":0.0,"max":0.0,"count":"1"},{"min":0.0,"max":1.0,"count":"1"},{"min":1.0,"max":2.0,"count":"1"},{"min":2.0,"max":3.0,"count":"1"},{"min":3.0,"max":4.0,"count":"2"}] | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
示例 3:生成 10 个桶的直方图
SELECT approx_histogram(col, 10) FROM VALUES (0), (0.1), (0.2), (0.3), (0.4), (0.5), (0.6), (0.7), (0.8), (0.9) AS t(col); +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | approx_histogram(col, 10) | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [{"min":0.0,"max":0.0,"count":"1"},{"min":0.0,"max":0.1,"count":"1"},{"min":0.1,"max":0.2,"count":"1"},{"min":0.2,"max":0.3,"count":"1"},{"min":0.3,"max":0.4,"count":"1"},{"min":0.4,"max":0.5,"count":"1"},{"min":0.5,"max":0.65,"count":"2"},{"min":0.65,"max":0.8,"count":"2"},{"min":0.8,"max":0.9,"count":"2"}] | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
