开发避坑指南(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;
相关推荐
云和数据.ChenGuang18 分钟前
python对接mysql和模型类的故障
数据库·python·mysql·oracle·conda·virtualenv
xuekai200809012 小时前
Oracle 19C 最简单快速安装方式
数据库·oracle
DomDanrtsey2 小时前
oracle查询某数据库用户下哪些表的索引没有被使用到?
数据库·oracle
鸽芷咕3 小时前
告别 Oracle 迁移痛点:金仓数据库的技术赋能与落地实效
数据库·oracle·金仓数据库
胖头鱼的鱼缸(尹海文)3 小时前
数据库管理-第404期 Oracle AI DB 23.26.1新特性一览(20260128)
数据库·人工智能·oracle
姚远Oracle ACE3 小时前
使用RPM包安装 Oracle 26ai软件并建库
数据库·oracle
pengdott3 小时前
Oracle RAC内存融合技术深度解析:PCM与非PCM资源的集群交响曲
数据库·oracle·pcm
twcc_come3 小时前
渗透第二次作业
数据库·oracle
踢足球092912 小时前
寒假打卡:2026-01-28
数据库·oracle
山茶花.16 小时前
SQL注入总结
数据库·sql·oracle