开发避坑指南(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;
相关推荐
betazhou20 小时前
Apache Seatunnel 抽取Oracle数据库测试
数据库·oracle·apache·seatunnel
devilnumber1 天前
Oracle 与 MySQL substr 函数差异总结
java·数据库·mysql·oracle
疯狂打码的少年1 天前
【数据库技术】SQL数据查询(SELECT基本语法)
数据库·笔记·sql·oracle
Broccoli523026651 天前
FastAPI + SQLAlchemy 异步会话(AsyncSession)核心方法
数据库·oracle·fastapi
oradh1 天前
Oracle TM 锁 Mode 4(Share)问题排查总结
数据库·oracle·oracle tm 锁·tm 锁 mode 4
CodeStats1 天前
MySQL与Oracle数据类型底层设计原理对比(下)
数据库·mysql·oracle
oradh2 天前
Oracle TX 锁 Mode 4(Share)问题排查总结
数据库·oracle·tx 锁 mode 4·oracle tx 锁
yi.Ist2 天前
数据定义语言-DDL操作
数据库·学习·mysql·oracle·大海豚
Irene19912 天前
格式化掩码 FM999999999999999999999.00 是 Oracle 数据库中 TO_CHAR 函数的数字格式模型
oracle·数字格式
蓝鸟19742 天前
Oracle HIS业务视图优化实战|VI_BQ_YWTX11从高耗费到秒级响应
数据库·oracle·his数据库调优·复合索引踩坑·is null索引问题·ql视图优化