Oracle11.2新特性之listagg函式
Oracle11.2新增了LISTAGG函式,可以用於字串聚集,測試如下:
1,版本
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
2,測試資料
SQL>
SQL> select empno,ename,deptno from scott.emp;
EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected
3,作為聚集函式
SQL> SELECT deptno,
2 LISTAGG(ename, ',') WITHIN GROUP(ORDER BY ename) AS employees
3 FROM scott.emp
4 GROUP BY deptno;
DEPTNO EMPLOYEES
------ --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
SQL>
--更換排序列
SQL> SELECT deptno,
2 LISTAGG(ename, ',') WITHIN GROUP(ORDER BY hiredate) AS employees
3 FROM scott.emp
4 GROUP BY deptno;
DEPTNO EMPLOYEES
------ --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 SMITH,JONES,FORD,SCOTT,ADAMS
30 ALLEN,WARD,BLAKE,TURNER,MARTIN,JAMES
--order by必須存在
SQL> SELECT deptno,
2 LISTAGG(ename, ',') WITHIN GROUP() AS employees
3 FROM scott.emp
4 GROUP BY deptno;
SELECT deptno,
LISTAGG(ename, ',') WITHIN GROUP() AS employees
FROM scott.emp
GROUP BY deptno
ORA-30491: ORDER BY 子句缺失
SQL> SELECT deptno,
2 LISTAGG(ename, ',') WITHIN GROUP() AS employees
3 FROM scott.emp
4 GROUP BY deptno;
SELECT deptno,
LISTAGG(ename, ',') WITHIN GROUP() AS employees
FROM scott.emp
GROUP BY deptno
ORA-30491: ORDER BY 子句缺失
SQL> SELECT deptno,
2 LISTAGG(ename, ',') WITHIN GROUP(order by null) AS employees
3 FROM scott.emp
4 GROUP BY deptno;
DEPTNO EMPLOYEES
------ --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
==〉按字母順序排列
4,LISTAGG作為分析函式使用
SQL> SELECT empno,
2 ename,
3 deptno,
4 LISTAGG(ename, ',') WITHIN GROUP(ORDER BY ename) over(partition by deptno) AS employees
5 FROM scott.emp;
EMPNO ENAME DEPTNO EMPLOYEES
----- ---------- ------ --------------------------------------------------------------------------------
7782 CLARK 10 CLARK,KING,MILLER
7839 KING 10 CLARK,KING,MILLER
7934 MILLER 10 CLARK,KING,MILLER
7876 ADAMS 20 ADAMS,FORD,JONES,SCOTT,SMITH
7902 FORD 20 ADAMS,FORD,JONES,SCOTT,SMITH
7566 JONES 20 ADAMS,FORD,JONES,SCOTT,SMITH
7788 SCOTT 20 ADAMS,FORD,JONES,SCOTT,SMITH
7369 SMITH 20 ADAMS,FORD,JONES,SCOTT,SMITH
7499 ALLEN 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7698 BLAKE 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7900 JAMES 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7654 MARTIN 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7844 TURNER 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7521 WARD 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
14 rows selected
SQL> SELECT empno,
2 ename,
3 deptno,
4 LISTAGG(ename, ',') WITHIN GROUP(ORDER BY ename) over(partition by deptno) AS employees
5 FROM scott.emp;
EMPNO ENAME DEPTNO EMPLOYEES
----- ---------- ------ --------------------------------------------------------------------------------
7782 CLARK 10 CLARK,KING,MILLER
7839 KING 10 CLARK,KING,MILLER
7934 MILLER 10 CLARK,KING,MILLER
7876 ADAMS 20 ADAMS,FORD,JONES,SCOTT,SMITH
7902 FORD 20 ADAMS,FORD,JONES,SCOTT,SMITH
7566 JONES 20 ADAMS,FORD,JONES,SCOTT,SMITH
7788 SCOTT 20 ADAMS,FORD,JONES,SCOTT,SMITH
7369 SMITH 20 ADAMS,FORD,JONES,SCOTT,SMITH
7499 ALLEN 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7698 BLAKE 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7900 JAMES 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7654 MARTIN 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7844 TURNER 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
7521 WARD 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
14 rows selected
5,其他實現方法參考
--model
SQL> SELECT deptno, vals
2 FROM (SELECT deptno, RTRIM(vals, ',') AS vals, rn
3 FROM scott.emp MODEL PARTITION BY(deptno) DIMENSION BY(ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY ename) AS rn) MEASURES(CAST(ename AS VARCHAR2(4000)) AS vals) RULES(vals [ ANY ] ORDER BY rn DESC = vals [ CV() ] || ',' || vals [ CV() + 1 ]))
4 WHERE rn = 1
5 ORDER BY deptno;
DEPTNO VALS
---------- --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
--model
SQL> SELECT deptno, vals
2 FROM (SELECT deptno, RTRIM(vals, ',') AS vals, rn
3 FROM scott.emp MODEL PARTITION BY(deptno) DIMENSION BY(ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY ename) AS rn) MEASURES(CAST(ename AS VARCHAR2(4000)) AS vals) RULES(vals [ ANY ] ORDER BY rn DESC = vals [ CV() ] || ',' || vals [ CV() + 1 ]))
4 WHERE rn = 1
5 ORDER BY deptno;
DEPTNO VALS
---------- --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
--表函式:WMSYS.WM_CONCAT,10G已經提供該函式
SQL>
SQL> SELECT deptno, WMSYS.WM_CONCAT(ename) AS vals -- 2 FROM scott.emp
3 GROUP BY deptno;
DEPTNO VALS
------ --------------------------------------------------------------------------------
10 CLARK,MILLER,KING
20 SMITH,FORD,ADAMS,SCOTT,JONES
30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
SQL>
SQL> SELECT deptno, WMSYS.WM_CONCAT(ename) AS vals -- 2 FROM scott.emp
3 GROUP BY deptno;
DEPTNO VALS
------ --------------------------------------------------------------------------------
10 CLARK,MILLER,KING
20 SMITH,FORD,ADAMS,SCOTT,JONES
30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-754381/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle11.2新特性之儲存Oracle
- LISTAGG 函式函式
- java8 新特性之函式式介面Java函式
- JDK8新特性之函式式介面JDK函式
- PHP 7.4 新特性之箭頭函式PHP函式
- PHP新特性之閉包、匿名函式PHP函式
- Java8新特性探索之函式式介面Java函式
- Oracle11.2表分割槽新特性Oracle
- 【Mysql】MySQL 5.7新特性之Generated Column(函式索引)MySql函式索引
- oracle 21c 新特性之 CHECKSUM 分析函式Oracle函式
- Oracle 11g 新聚集函式listagg實現列轉行Oracle函式
- 【函式】Oracle12c 列轉行函式使用listagg函式Oracle
- Oracle11.2新特性之INSERT提示IGNORE_ROW_ON_DUPKEY_INDEXOracleIndex
- Oracle11.2新特性之使用DBMS_PARALLEL_EXECUTE包實現並行OracleParallel並行
- Java8的新特性--函式式介面Java函式
- Spring 5 新特性:函式式Web框架Spring函式Web框架
- Oracle 12c新特性之:APPROX_COUNT_DISTINCT 函式OracleAPP函式
- PHP 7.4 新特性 —— 箭頭函式 2.0PHP函式
- JDK 1.8 新特性之Lambda表示式JDK
- ?Java8新特性之Lambda表示式,函式式介面,方法引用和default關鍵字Java函式
- 用listagg函式分組實現列轉行函式
- ES6新特性總結之函式和擴充套件運算子...函式套件
- java8新特性之函式式介面、lambda表示式、介面的預設方法、方法和建構函式的引用Java函式
- Java8新特性-四大核心函式式介面Java函式
- JDK1.8新特性之Lambda表示式JDK
- JDK1.8新特性之Lambda表示式()->JDK
- PHP8新特性之match表示式PHP
- java8 新特性之Lambda 表示式Java
- Java8 新特性之 Lambda 表示式Java
- jdk1.8 新特性之 lambda表示式JDK
- C++11新特性之Lambda表示式C++
- 使用listagg函式完成行列轉換一例函式
- JDK1.8的新特性之Lambda表示式JDK
- java8新特性之lambda表示式(一)Java
- C++ 11 新特性之正規表示式C++
- JDK1.8新特性:Lambda表示式語法和內建函式式介面JDK函式
- jdk1.8 新特性之 如何寫lambda表示式JDK
- Oracle11gr2分析函式新特性(三)Oracle函式