开发避坑指南(31):Oracle 11g LISTAGG函数使用陷阱,缺失WITHIN子句解决方案

错误信息

java 复制代码
Error querying database. Cause: java.sql.SQLSyntaxErrorException: ORA-02000: 缺失 WITHIN 关键字

查询语句

使用LISTAGG函数将多行数据合并为单行字符串,如下:

sql 复制代码
select
    t.order_no as orderNo,
    t.account_no,
    (select listagg(a.bank_name,',')  from t_account_info a 
        where t.account_no = a.account_no and a.account_type = '01') as bankName,
    t.payer_name as payername
from t_order_info t
where 1=1
and t.order_no is not null;

错误分析

报以上错误的问题是oracle的版本和listagg的关键字使用不对应,当前用的是oracle 11g ,该版本的listagg使用需要在后面加上within group(order by 字段),如果是oracle 19c的话可以直接使用listagg(字段,',')。

解决办法

将查询语句修改为如下:

sql 复制代码
select
		t.order_no as orderNo,
		t.account_no,
		(select listagg(a.bank_name,',') within group (order by a.bank_name) from t_account_info a 
			where t.account_no = a.account_no and a.account_type = '01') as bankName,
		t.payer_name as payername
		
from t_order_info t
where 1=1
and t.order_no is not null;
相关推荐
_ku_ku_7 小时前
数据库系统原理 · SQL 数据定义、更新及数据库编程 · 自学总结
数据库·oracle
南极企鹅8 小时前
事务&@Transactional注解
java·数据库·spring·oracle·mybatis
Yushan Bai9 小时前
ORACLE Enterprise Manager Cloud Control 系列测试3-Data Masking
数据库·oracle
Yushan Bai10 小时前
ORACLE Enterprise Manager Cloud Control 系列测试2- 日常管理和SQL优化
数据库·oracle
AI技术控10 小时前
RAG 怎么做 Query 改写?从工程实践看检索增强生成的第一道关键关卡
人工智能·语言模型·自然语言处理·oracle·nlp
杨云龙UP10 小时前
Oracle RAC/ODA环境下如何准确查询PDB表空间已分配大小?一次说清Oracle表空间逻辑大小和ASM三副本实际占用_2026-05-19
linux·运维·数据库·sql·oracle·ffmpeg
晨曦中的暮雨10 小时前
3.20字节云部门一面|面经
数据库·oracle
Yushan Bai12 小时前
ORACLE Enterprise Manager Cloud Control 系列测试1-安装配置
oracle
guygg8814 小时前
C# 监听数据库数据变化(SqlDependency 实现)
数据库·oracle·c#
网管NO.116 小时前
MySQL、Oracle、PostgreSQL 深度对比,数据库怎么选?
数据库·mysql·oracle