开发避坑指南(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;
相关推荐
cpsvps_net1 小时前
VPS服务器锁等待超时处理,如何有效解决数据库性能瓶颈
服务器·数据库·oracle
lagelangri66613 小时前
数据库连接池以及HikariCP使用
数据库·oracle
semantist@语校16 小时前
语校网500所里程碑:日本语言学校数据库的标准化与可追溯机制
大数据·数据库·人工智能·百度·语言模型·oracle·github
落日漫游20 小时前
MySQL常用命令全攻略
数据库·sql·oracle
南方者1 天前
【JAVA】【BUG】经常出现的典型 bug 及解决办法
java·oracle·bug
Albert Edison1 天前
【MySQL】表的操作
数据库·mysql·oracle
jackletter3 天前
五大关系数据库(sqlserver、mysql、oracle、pgsql、sqlite)的对象名称和转义字符
mysql·postgresql·oracle·sqlserver·sqlite
AWS官方合作商3 天前
打破数据枷锁:在AWS上解锁Oracle数据库的无限潜能
数据库·oracle·aws
瑶总迷弟3 天前
在 CentOS 7.6 上安装 Oracle WebLogic Server 12c 详细教程
linux·oracle·centos
花北城3 天前
【MySQL】Oracle与MySQL,跨库数据转储
数据库·mysql·oracle