开发避坑指南(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;
相关推荐
0_0梅伊阁诗人1 小时前
Django ORM 模型
开发语言·数据库·笔记·python·oracle·django
数巨小码人3 小时前
Oracle SQL调优技巧实战指南
数据库·sql·oracle
noravinsc3 小时前
在银河麒麟v10上安装达梦8数据库
服务器·数据库·oracle
lu9up1 天前
因表并行引发的血案【故障处理案例】
数据库·oracle·dba
代码的余温2 天前
Oracle RAC共享存储核心技术
数据库·oracle
float_六七2 天前
数据库物理外键与逻辑外键全解析
数据库·oracle
大白的编程日记.2 天前
【MySQL】数据库的基本操作
数据库·mysql·oracle
Jamie Chyi2 天前
【Oracle经验分享】字符串拼接过长问题的解决方案 —— 巧用 XMLAGG
数据库·oracle
代码的余温2 天前
Oracle高可用与容灾解决方案
数据库·oracle
kimble_xia@oracle2 天前
Oracle打补丁笔记
数据库·oracle