【问题标题】:oracle sql listagg [duplicate]oracle sql listagg [重复]
【发布时间】:2018-11-30 12:54:35
【问题描述】:
SELECT deptno, 
       LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
  FROM emp
 GROUP BY deptno;

错误:- ORA-00923: FROM 关键字未在预期位置找到 00923. 00000 - “在预期的地方找不到 FROM 关键字” *原因:
*操作:行错误:1 列:42

Oracle 数据库 11g 企业版版本 11.1.0.7.0 - 64 位生产

【问题讨论】:

标签: sql oracle oracle11g oracle10g listagg


【解决方案1】:

对于 Oracle 的 10gR211gR1 版本,您可以使用 sys_connect_by_path 贡献的分层查询:

with emp( ename, deptno ) as
(
 select 'CLARK',10 from dual union all 
 select 'MILLER',10 from dual union all
 select 'KING',10 from dual union all
 select 'FORD',20 from dual union all
 select 'SCOTT',20 from dual union all
 select 'JONES',20 from dual union all
 select 'SMITH',20 from dual union all
 select 'ADAMS',20 from dual union all
 select 'WARD',30 from dual union all
 select 'MARTIN',30 from dual union all
 select 'TURNER',30 from dual union all
 select 'JAMES',30 from dual union all
 select 'ALLEN',30 from dual union all
 select 'BLAKE',30 from dual
)
select deptno, ltrim(sys_connect_by_path(ename, ','), ',') as enames
  from (select deptno,
               ename,
               row_number() over(partition by deptno order by ename) as rn
          from emp)
 where connect_by_isleaf = 1
connect by deptno = prior deptno
       and rn = prior rn + 1
 start with rn = 1;

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

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 2021-07-25
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多