其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型。括号里面的参数是列,而且可以是多个列的集合,也就是说在括号里面可以自由地用‘||’合并字符串。
select wm_concat(ename) ename from emp;
ENAME
--------------------------------------------------------------------------------
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER
select deptno,wm_concat(ename) ename from emp group by deptno;
DEPTNO ENAME
--------------------------------------------------------------------------------
10 CLARK,MILLER,KING
20 SMITH,FORD,ADAMS,SCOTT,JONES
30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
SQL> select deptno,ename,sal,wm_concat(ename) over(partition by deptno) name from emp;
DEPTNO ENAME SAL NAME
--------------------------------------------------------------------------------
10 CLARK 2450 CLARK,KING,MILLER
10 KING 5000 CLARK,KING,MILLER
10 MILLER 1300 CLARK,KING,MILLER
20 JONES 2975 JONES,FORD,ADAMS,SMITH,SCOTT
20 FORD 3000 JONES,FORD,ADAMS,SMITH,SCOTT
20 ADAMS 1100 JONES,FORD,ADAMS,SMITH,SCOTT
20 SMITH 800 JONES,FORD,ADAMS,SMITH,SCOTT
20 SCOTT 3000 JONES,FORD,ADAMS,SMITH,SCOTT
30 WARD 1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 TURNER 1500 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 ALLEN 1600 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 JAMES 950 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 BLAKE 2850 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 MARTIN 1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
已选择14行。
LISTAGG(列名,’ 分割符号’): oracle 11g 以上的版本才有的一个将指定列名的多行查询结果,用 指定的分割符号 合并成一行显示
对工资进行排序,用逗号进行拼接。
select listagg(ename,',')within group(order by sal)name from emp;
NAME
----------------------------------------------------------------------------------------------------
SMITH,JAMES,ADAMS,MARTIN,WARD,MILLER,TURNER,ALLEN,CLARK,BLAKE,JONES,FORD,SCOTT,KING
select deptno,listagg(ename,',')within group(order by sal)name from emp group by deptno;
DEPTNO NAME
---------- ----------------------------------------------------------------------------------------------------
10 MILLER,CLARK,KING
20 SMITH,ADAMS,JONES,FORD,SCOTT
30 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
select deptno,ename,sal,listagg(ename,',')within group(order by sal)over(partition by deptno)name from emp;
DEPTNO ENAME SAL NAME
---------- ---------- ---------- ----------------------------------------
10 MILLER 1300 MILLER,CLARK,KING
10 CLARK 2450 MILLER,CLARK,KING
10 KING 5000 MILLER,CLARK,KING
20 SMITH 800 SMITH,ADAMS,JONES,SCOTT,FORD
20 ADAMS 1100 SMITH,ADAMS,JONES,SCOTT,FORD
20 JONES 2975 SMITH,ADAMS,JONES,SCOTT,FORD
20 SCOTT 3000 SMITH,ADAMS,JONES,SCOTT,FORD
20 FORD 3000 SMITH,ADAMS,JONES,SCOTT,FORD
30 JAMES 950 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
30 MARTIN 1250 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
30 WARD 1250 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
30 TURNER 1500 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
30 ALLEN 1600 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE
30 BLAKE 2850 JAMES,MARTIN,WARD,TURNER,ALLEN,BLAKE