统计指定列的近似直方图。此函数为MaxCompute 2.0扩展函数。
注意事项
升级到MaxCompute 2.0后,产品扩展了部分函数。如果您用到的函数涉及新数据类型(TINYINT、SMALLINT、INT、FLOAT、VARCHAR、TIMESTAMP或BINARY),在使用扩展函数时,需要执行如下语句开启新数据类型开关:
在同一条SQL语句中同时使用多个聚合函数时,如果项目资源不足,会出现内存溢出问题,请您根据实际业务情况优化SQL或购买计算资源。
命令格式
map<double key, double value> numeric_histogram(bigint <buckets>,
double <colname>
[, double <weight>])
参数说明
buckets:必填。BIGINT类型,表示返回的近似直方图列的最大个数。
colname:必填。DOUBLE类型,需要统计近似直方图的列。
weight:可选。权重值,可以指定每行数据对应的权重,值为DOUBLE类型的列。
返回值说明
map<double key, double value>
类型,返回值中key是近似直方图的X轴坐标点,value是近似直方图的Y轴的近似高度。返回规则如下:
buckets值为NULL时,返回NULL。
colname值为NULL时,该行不参与计算。
示例数据
为便于理解各函数的使用方法,本文为您提供源数据,基于源数据提供函数相关示例。创建表emp,并添加数据,命令示例如下:
create table if not exists emp
(empno bigint,
ename string,
job string,
mgr bigint,
hiredate datetime,
sal bigint,
comm bigint,
deptno bigint);
tunnel upload emp.txt emp;
emp.txt中的数据如下:
7369,SMITH,CLERK,7902,1980-12-17 00:00:00,800,,20
7499,ALLEN,SALESMAN,7698,1981-02-20 00:00:00,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-22 00:00:00,1250,500,30
7566,JONES,MANAGER,7839,1981-04-02 00:00:00,2975,,20
7654,MARTIN,SALESMAN,7698,1981-09-28 00:00:00,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01 00:00:00,2850,,30
7782,CLARK,MANAGER,7839,1981-06-09 00:00:00,2450,,10
7788,SCOTT,ANALYST,7566,1987-04-19 00:00:00,3000,,20
7839,KING,PRESIDENT,,1981-11-17 00:00:00,5000,,10
7844,TURNER,SALESMAN,7698,1981-09-08 00:00:00,1500,0,30
7876,ADAMS,CLERK,7788,1987-05-23 00:00:00,1100,,20
7900,JAMES,CLERK,7698,1981-12-03 00:00:00,950,,30
7902,FORD,ANALYST,7566,1981-12-03 00:00:00,3000,,20
7934,MILLER,CLERK,7782,1982-01-23 00:00:00,1300,,10
7948,JACCKA,CLERK,7782,1981-04-12 00:00:00,5000,,10
7956,WELAN,CLERK,7649,1982-07-20 00:00:00,2450,,10
7956,TEBAGE,CLERK,7748,1982-12-30 00:00:00,1300,,10
示例
统计薪资(sal)的近似直方图。命令示例如下:
select numeric_histogram(5, sal) from emp;
返回结果如下:
+------------+ | _c0 | +------------+ | {"1328.5714285714287":7.0,"2450.0":2.0,"5000.0":2.0,"875.0":2.0,"2956.25":4.0} | +------------+
统计薪资(sal)的近似直方图,其中每行数据中的
deptno
代表部门权重。命令示例如下:select numeric_histogram(5, sal, deptno) from emp;
返回结果如下:
+------------+ | _c0 | +------------+ | {"2944.4444444444443":90.0,"2450.0":20.0,"5000.0":20.0,"890.0":50.0,"1350.0":160.0} | +------------+
相关函数
NUMERIC_HISTOGRAM函数属于聚合函数,更多将多条输入记录进行求平均值、参数聚合的相关函数请参见聚合函数。
文档内容是否对您有帮助?