开发避坑指南(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;
相关推荐
x***440116 小时前
保姆级教程 !SQL Server数据库的备份和还原
数据库·oracle
N***H48616 小时前
MySQL常用的关键字(详细)
数据库·mysql·oracle
添加shujuqudong1如果未回复16 小时前
多策略改进的鲸鱼优化算法(MWOA),与其他三种变体和几种2024最新算法比较,策略都是很新颖的策略
oracle
热心市民小刘050517 小时前
数据库基础知识点总结
数据库·oracle
k***38818 小时前
oracle 12c查看执行过的sql及当前正在执行的sql
java·sql·oracle
n***F87518 小时前
【MySQL】视图
数据库·mysql·oracle
0***863318 小时前
MySQL:基础操作(增删查改)
数据库·mysql·oracle
DarkAthena18 小时前
【Oracle/GaussDB/MogDB】统一权限查询
数据库·oracle·gaussdb
我科绝伦(Huanhuan Zhou)19 小时前
SCN与CHECKPOINT核心机制解析:Oracle数据一致性与恢复的基石
数据库·oracle
k***825119 小时前
Redis-配置文件
数据库·redis·oracle