开发避坑指南(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;
相关推荐
一只fish7 小时前
优化器架构对比:Oracle vs PostgreSQL vs MySQL
mysql·postgresql·oracle
测试运维日常笔记9 小时前
Oracle 数据库连接认证方式详解
数据库·oracle
盗理者11 小时前
AI Agent 技能分享|从零实现 MCP Server,让 Agent 安全读取数据库
数据库·oracle
oradh16 小时前
Oracle ADRCI 诊断数据工具总结
数据库·oracle·adrci
xfhuangfu17 小时前
Oracle 19c 本地索引分区变为 UNUSABLE 后的空间占用验证
数据库·oracle
杨云龙UP17 小时前
Oracle ASH 报告详解:与 AWR 的区别、使用场景及生成方法
linux·运维·服务器·数据库·oracle
测试运维日常笔记17 小时前
PostgreSQL 数据库磁盘空间占用排查手顺
数据库·postgresql·oracle
️学习的小王18 小时前
MySQL常用函数知识点总结
数据库·mysql·oracle
东方护航数据恢复(深圳)2 天前
MySQL_Oracle数据库崩溃修复全攻略_东方护航数据恢复深圳店
数据库·mysql·oracle
梓沂2 天前
CentOS 7.9 静默安装 Oracle 11gR2 企业版全记录
linux·oracle·centos