日期:2014-05-17  浏览次数:20960 次

如何将查询结果连接成字符串????
select   name   from   table1;
我想将查询出来的name用逗号分割,连接成一个字符串,怎样做???

------解决方案--------------------
从上面的地址中取的:

有表:
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

============================================================================

SELECT deptno,
LTRIM(MAX(SYS_CONNECT_BY_PATH(ename, ', ')) KEEP (DENSE_RANK LAST ORDER BY curr), ', ') AS concatenated
FROM (SELECT deptno, ename,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
FROM emp)
GROUP BY deptno
CONNECT BY prev = PRIOR curr
AND deptno = PRIOR deptno
START WITH curr = 1;