简体   繁体   English

ORACLE中的LISTAGG

[英]LISTAGG in ORACLE

I am trying to use LISTAGG() to fetch more than two columns. 我试图使用LISTAGG()来获取两列以上。

SELECT deptname, deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees 
FROM emp 
GROUP BY deptno;

But it is throwing this error: 但它抛出了这个错误:

: FROM keyword not found where expected
 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
Error at Line: 3 Column: 12

Can please somebody explain why it is? 可以请有人解释它为什么?

The LISTAGG analytic function was introduced in Oracle 11g Release 2 . LISTAGG分析功能是在Oracle 11g第2版中引入的。 So, if you are on older version, you won't be able to use it. 因此,如果您使用的是旧版本,则无法使用它。

The error seems strange. 这个错误似乎很奇怪。 You should actually get ORA-00904: "DEPTNAME": invalid identifier as the standard EMP table in SCOTT schema doesn't have DEPTNAME column. 实际上你应该得到ORA-00904: "DEPTNAME": invalid identifier因为SCOTT模式中的标准EMP表没有DEPTNAME列。 Also, you should get ORA-00979: not a GROUP BY expression as you did not mention the SELECT ed columns in the GROUP BY expression. 此外,你应该得到ORA-00979: not a GROUP BY expression ,你没有提到的 SELECT编列BY表达。

Using the standard EMP table in SCOTT schema: 使用SCOTT模式中的标准EMP表:

SQL> SELECT deptno,
  2    job,
  3    LISTAGG(ename, ',') WITHIN GROUP (
  4  ORDER BY ename) AS employees
  5  FROM emp
  6  GROUP BY deptno,
  7    job;

    DEPTNO JOB       EMPLOYEES
---------- --------- ------------------------
        10 CLERK     MILLER
        10 MANAGER   CLARK
        10 PRESIDENT KING
        20 CLERK     ADAMS,SMITH
        20 ANALYST   FORD,SCOTT
        20 MANAGER   JONES
        30 CLERK     JAMES
        30 MANAGER   BLAKE
        30 SALESMAN  ALLEN,MARTIN,TURNER,WARD

9 rows selected.

SQL>

Try: 尝试:

SELECT deptname, deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees 
FROM emp 
GROUP BY deptno,deptname;

Oracle 11g: Oracle 11g:

SELECT deptname, deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY deptno,job) AS employees 
FROM emp 
GROUP BY deptno,job;

Oracle 10g: Oracle 10g:

SELECT deptname, deptno, WMSYS.WM_CONCAT(ename) AS employees 
FROM emp 
GROUP BY deptno,job;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM