-1

基础数据:

 DEPTNO ENAME
---------- ----------
        20 SMITH
        30 ALLEN
        30 WARD
        20 JONES
        30 MARTIN
        30 BLAKE
        10 CLARK
        20 SCOTT
        10 KING
        30 TURNER
        20 ADAMS
        30 JAMES
        20 FORD
        10 MILLER

期望的输出:

    DEPTNO EMPLOYEES
---------- --------------------------------------------------
        10 CLARK,KING,MILLER
        20 SMITH,FORD,ADAMS,SCOTT,JONES
        30 ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD
4

2 回答 2

3

您没有指定 Oracle 的版本,但在 Oracle 11g 中,您可以使用LISTAGG

select DEPTNO,
  listagg(ENAME, ',') within group (order by ENAME) as ename
from yourtable
group by DEPTNO

请参阅带有演示的 SQL Fiddle

在 Oracle 10g 中,您可以使用wm_concat()

select DEPTNO,
  wm_concat(ENAME) as ename
from yourtable
group by DEPTNO

您还应该能够使用以下内容:

SELECT LRRID,
       LTRIM(MAX(SYS_CONNECT_BY_PATH(CDSID,','))
       KEEP (DENSE_RANK LAST ORDER BY curr),',') AS elements
FROM   
(
  SELECT LRRID,
    CDSID,
    ROW_NUMBER() OVER (PARTITION BY LRRID ORDER BY CDSID) AS curr,
    ROW_NUMBER() OVER (PARTITION BY LRRID ORDER BY CDSID) -1 AS prev
  FROM WLRR_LRR_NOTIFY_TABLE
)
GROUP BY LRRID
CONNECT BY prev = PRIOR curr AND LRRID = PRIOR LRRID
START WITH curr = 1;

请参阅带有演示的 SQL Fiddle

于 2013-03-25T10:11:25.873 回答
0

emmmm试试这个。。

SELECT DEPTNO, LISTAGG(ENAME, ' ') WITHIN GROUP (ORDER BY ENAME) AS ENAME
FROM   <TABLE-NAME>
GROUP BY DEPTNO;
于 2013-03-25T10:15:25.297 回答