Oracle9i中使用SYS_CONNECT_BY_PATH進行行列轉換-
有表:
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
想輸出為:
DEPTNO ENAME
------ ----------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
除了使用聚集函式或者儲存過程之外,9i中可以:
SQL> SELECT deptno
2 , LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
3 KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concatenated
4 FROM ( SELECT deptno
5 , ename
6 , ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr
7 , ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
8 FROM emp )
9 GROUP BY deptno
10 CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
11 START WITH curr = 1;
DEPTNO
----------
CONCATENATED
----------------------------------------------------------------------------------------------------
10
CLARK,KING,MILLER
20
ADAMS,FORD,JONES,SCOTT,SMITH
30
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
想輸出為:
DEPTNO ENAME
------ ----------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
除了使用聚集函式或者儲存過程之外,9i中可以:
SQL> SELECT deptno
2 , LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
3 KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concatenated
4 FROM ( SELECT deptno
5 , ename
6 , ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr
7 , ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
8 FROM emp )
9 GROUP BY deptno
10 CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
11 START WITH curr = 1;
DEPTNO
----------
CONCATENATED
----------------------------------------------------------------------------------------------------
10
CLARK,KING,MILLER
20
ADAMS,FORD,JONES,SCOTT,SMITH
30
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742223/viewspace-219148/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle行列轉換-行轉列Oracle
- oracle行列轉換-列轉行Oracle
- 行列轉換,列行轉換統計
- 在Excel中怎麼快速進行行列轉換?Excel
- 行列轉換
- 【轉】Oracle: wmsys.wm_concat、sys_connect_by_path、自定義函式實現行列轉換Oracle函式
- wmsys.wm_concat、sys_connect_by_path、自定義函式實現行列轉換函式
- Kettle行列轉換
- 偽行列轉換!
- 行列轉換sqlSQL
- 使用sed做特殊的行列轉換
- 行列轉換 交叉表 (轉)
- Oracle-行列轉換Oracle
- MySQL行列轉換拼接MySql
- 行列轉換之大全~~~
- sql server 行列轉換SQLServer
- oracle行列轉換-多行轉換成字串Oracle字串
- 試驗Oracle中實現行列轉換的方法(轉)Oracle
- oracle 11g 使用 pivot/unpivot 行列轉換Oracle
- mysql行列轉換詳解MySql
- sql server行列轉換案例SQLServer
- Oracle 行列轉換 經典Oracle
- Oracle 行列轉換總結Oracle
- Oracle 行列轉換小結Oracle
- 【SQL 學習】行列轉換SQL
- 【SQL】行列轉換方法示例SQL
- Oracle行列轉換總結Oracle
- Shell練習 行列轉換
- 複雜的行列轉換
- oracle行列轉換-字串轉換成多列Oracle字串
- oracle行列轉換-多列轉換成字串Oracle字串
- 在Word中實現表格的行列互換 (轉)
- 使用vue實現行列轉換的一種方法。Vue
- 記錄一個行列轉換
- 通用的行列轉換的方法
- 行列轉換問題總結
- oracle和mysql的行列轉換OracleMySql
- 行列轉換的一個例子