开发避坑指南(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;
相关推荐
小马哥编程8 小时前
【软考架构】第6章 数据库基本概念
数据库·oracle·架构
ALLSectorSorft15 小时前
搭子交友 app 动态分享与打卡系统设计实现
java·服务器·数据库·人工智能·oracle·交友
码农阿豪2 天前
KingbaseES数据库增删改查操作分享
数据库·oracle
AwhiteV2 天前
利用图数据库高效解决 Text2sql 任务中表结构复杂时占用过多大模型上下文的问题
数据库·人工智能·自然语言处理·oracle·大模型·text2sql
不羁。。3 天前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
杨云龙UP4 天前
CentOS Linux 7 (Core)上部署Oracle 11g、19C RAC详细图文教程
数据库·oracle
我科绝伦(Huanhuan Zhou)4 天前
银河麒麟V10一键安装Oracle 11g脚本分享
数据库·oracle
kura_tsuki4 天前
[Oracle数据库] Oracle 常用函数
数据库·oracle
码出财富5 天前
SQL语法大全指南
数据库·mysql·oracle