用指定的separator做分隔符,连接colname中的值。
注意事项
升级到MaxCompute 2.0后,产品扩展了部分函数。如果您用到的函数涉及新数据类型(TINYINT、SMALLINT、INT、FLOAT、VARCHAR、TIMESTAMP或BINARY),在使用扩展函数时,需要执行如下语句开启新数据类型开关:
在同一条SQL语句中同时使用多个聚合函数时,如果项目资源不足,会出现内存溢出问题,请您根据实际业务情况优化SQL或购买计算资源。
命令格式
string wm_concat(string <separator>, string <colname>)
参数说明
separator:必填。STRING类型常量,分隔符。
colname:必填。STRING类型。如果输入为BIGINT、DOUBLE或DATETIME类型,会隐式转换为STRING类型后参与运算。
当WM_CONCAT函数同时携带within group (order by <col1>[,<col2>…])
表达式和DISTINCT关键字时,需注意语法的使用,详情请参见聚合函数语法。
返回值说明
使用group by
分组,组内返回值不排序。
返回STRING类型。返回规则如下:
separator非STRING类型常量时,返回报错。
colname非STRING、BIGINT、DOUBLE或DATETIME类型时,返回报错。
colname值为NULL时,该行不会参与计算。
select wm_concat(',', name) from table_name;
语句中,如果table_name
为空集合,该语句返回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; --请根据您上传数据文件的实际path(路径以及名称)替换emp.txt
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
示例
示例1:对所有职工的姓名(ename)进行合并。命令示例如下:
select wm_concat(',', ename) from emp;
返回结果如下:
+------------+ | _c0 | +------------+ | SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER,JACCKA,WELAN,TEBAGE | +------------+
示例2:与
group by
配合使用,对所有职工按照部门(deptno)进行分组,并将同组的职工姓名(ename)进行合并。命令示例如下:select deptno, wm_concat(',', ename) from emp group by deptno order by deptno;
返回结果如下:
+------------+------------+ | deptno | _c1 | +------------+------------+ | 10 | CLARK,KING,MILLER,JACCKA,WELAN,TEBAGE | | 20 | SMITH,JONES,SCOTT,ADAMS,FORD | | 30 | ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES | +------------+------------+
示例3:与
group by
配合使用,对所有职工按照部门(deptno)进行分组,并将同组的薪资(sal)去重后进行合并。命令示例如下:select deptno, wm_concat(distinct ',', sal) from emp group by deptno order by deptno;
返回结果如下:
+------------+------------+ | deptno | _c1 | +------------+------------+ | 10 | 1300,2450,5000 | | 20 | 1100,2975,3000,800 | | 30 | 1250,1500,1600,2850,950 | +------------+------------+
示例4:与
group by
、order by
配合使用,对所有职工按照部门(deptno)进行分组,并将同组的薪资(sal)进行合并排序。命令示例如下:select deptno, wm_concat(',',sal) within group (order by sal) from emp group by deptno order by deptno;
返回结果如下:
+------------+------------+ |deptno|_c1| +------------+------------+ |10|1300,1300,2450,2450,5000,5000| |20|800,1100,2975,3000,3000| |30|950,1250,1250,1500,1600,2850| +------------+------------+
示例5:与
group by
、order by
配合使用,对指定列进行排序。命令示例如下:SELECT g, wm_concat ('#', t) WITHIN GROUP (ORDER BY h DESC) AS t_t FROM VALUES ("24",1,10), ("234",2,100), ("555",1,20), ("123", 2,30), ("7",1,11), ("13",3,50) AS tmp (t,g,h) GROUP BY g;
返回结果如下:
+------+-----+ | g | t_t | +------+-----+ | 1 | 555#7#24 | | 2 | 234#123 | | 3 | 13 | +------+-----+
相关函数
WM_CONCAT函数属于聚合函数,更多将多条输入记录进行求平均值、参数聚合的相关函数请参见聚合函数。