Oracle SQL Error: ORA-00923: From Keyword not found where expected

Oracle SQL Error: ORA-00923: From Keyword not found where expected

当我对 Oracle 使用以下 sql 命令时:

SELECT CATEGORY,ANTIGENNAME,LATINCOMPOSITION,HYCORCODE,FDACLEARANCE, LISTAGG(ORCHARDCODE, ';')
    WITHIN GROUP (ORDER BY ORCHARDCODE) as code
from tablename
group by HYCORCODE

我似乎遇到了 ORA-00923 错误:

ORA-00923: FROM keyword not found where expected

是什么原因导致出现错误?

可能与标识符有关,请尝试使用 "code"

SELECT CATEGORY,ANTIGENNAME,LATINCOMPOSITION,HYCORCODE,FDACLEARANCE,
    LISTAGG(ORCHARDCODE, ';')
     WITHIN GROUP (ORDER BY ORCHARDCODE) as "code"
 from tablename
 group by HYCORCODE

code不是保留字,所以我觉得不需要加引号(见here)。

但是,您有聚合函数,因此您需要 group by:

中的正确列
SELECT CATEGORY, ANTIGENNAME, LATINCOMPOSITION, HYCORCODE, FDACLEARANCE, 
       LISTAGG(ORCHARDCODE, ';') WITHIN GROUP (ORDER BY ORCHARDCODE) as code
from tablename
group by CATEGORY, ANTIGENNAME, LATINCOMPOSITION, HYCORCODE, FDACLEARANCE;

这肯定解决了您的查询问题。我不确定它是否会解决您的特定错误。当我从 group by 中省略列时,我得到 "ORA-00979 (not a GROUP BY expression)."

LISTAGG 在您的 Oracle 版本中不可用。使用此语句检查您的版本:

select * from v$version;

LISTAGG 仅适用于版本 >= 11.2.x

来自"Oracle Database 11g Release 2 (11.2) New Features in Data Warehousing"

Analytic Functions

New SQL analytic functions have been introduced that enable you to list (or concatenate) measure values within a group (LISTAGG).