开发避坑指南(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;
相关推荐
一心赚狗粮的宇叔5 小时前
中级软件开发工程师2025年度总结
java·大数据·oracle·c#
yangminlei6 小时前
集成Camunda到Spring Boot项目
数据库·oracle
爱潜水的小L12 小时前
自学嵌入式day43,商城网页
数据库·oracle
8号看台14 小时前
ORA-01017: 用户名/口令无效; 登录被拒绝
数据库·oracle
龙亘川14 小时前
【课程5.3】功能设计:城管核心指标与设施分布(处置效率、违建数量等指标定义)
数据库·oracle·智慧城市·一网统管ai平台
memgLIFE15 小时前
mybatis数据库查询
数据库·oracle·mybatis
oMcLin17 小时前
如何在Oracle Linux 8.5上配置并优化Oracle RAC集群,确保企业级数据库的高可用性与负载均衡?
linux·数据库·oracle
jnrjian18 小时前
Oracle username 集成 AD
数据库·oracle
施嘉伟19 小时前
Oracle重建控制文件技术总结
数据库·oracle