开发避坑指南(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;
相关推荐
dishugj6 小时前
Oracle物理体系结构
数据库·oracle
北亚数据恢复6 小时前
【数据库数据恢复】NDF文件变0KB、数据库损坏?超大数据库数据恢复案例
数据库·oracle
姚远Oracle ACE7 小时前
对比测试了 openGauss、PostgreSQL、Oracle、MySQL 的CPU 指令数
mysql·postgresql·oracle
jnrjian10 小时前
Oracle PG data type 数据类型转化
oracle
Lucifer三思而后行12 小时前
Oracle library cache 等待怎么查?Lock、Pin、Mutex 一次讲清楚
oracle·lock
茉莉玫瑰花茶15 小时前
GO [ context 原理解析 ]
数据库·oracle·iphone
小羊没烦恼!17 小时前
浅谈SQL Server中的事务日志(二)----事务日志在修改数据时的角色
前端·数据库·后端·python·web安全·oracle
大侠归来1 天前
事务能回滚吗?Pipeline 算事务吗?
java·数据库·oracle
夕除1 天前
PostgreSQL基础
数据库·oracle