开发避坑指南(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;
相关推荐
吠品13 小时前
Docker Desktop部署Weaviate向量数据库:从配置到生产环境全流程
数据库·oracle·eureka
码云数智-园园14 小时前
关系型与非关系型数据库:核心区别与业务场景解析
数据库·oracle
ClouGence14 小时前
数据迁移同步工具 CloudCanal-v5.5.0.0 发布,支持 RETL(定时扫描同步)
数据库·mysql·postgresql·oracle·sqlserver·kafka·etl
DomDanrtsey14 小时前
oracle与tidb时间格式化函数不兼容简述
数据库·oracle·tidb
杨云龙UP14 小时前
Linux环境下Oracle RMAN全量、增量备份与定时任务实践_20260331
linux·运维·服务器·数据库·oracle
小陈工14 小时前
Python Web开发入门(三):配置文件管理与环境变量最佳实践
开发语言·jvm·数据库·python·oracle·性能优化·开源
数据库小组14 小时前
Oracle 到 PostgreSQL 迁移,2026 年如何实现平滑切换?
数据库·postgresql·oracle·数据同步·数据库迁移·oracle迁移·postgresql迁移
last demo1 天前
mysql
运维·数据库·mysql·oracle
kevin_cat1 天前
oracle 扩展表空间
数据库·oracle
高梦轩1 天前
MySQL 数据库备份与恢复
数据库·oracle