开发避坑指南(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;
相关推荐
文档搬运工12 分钟前
Oracle 19.29
数据库·oracle
张毫洁2 小时前
mysql、oracle的JDBC操作
数据库·mysql·oracle
Boop_wu5 小时前
[MySQL] 数据库设计
java·数据库·oracle
xiaoye37085 小时前
达梦数据库连接配置yaml 文件配置
服务器·数据库·oracle
野犬寒鸦1 天前
从零起步学习MySQL || 第七章:初识索引底层运用及性能优化(结合底层数据结构讲解)
java·数据库·后端·mysql·oracle
IDOlaoluo1 天前
win64_11gR2_client.zip 怎么安装?Oracle 11g 客户端详细安装步骤
数据库·oracle
彦偈1 天前
Centos7 oracle 11G 搭建ADG
数据库·oracle
fen_fen2 天前
人大金仓数据库kingbase8创建表示例
数据库·oracle
Oracle NetSuite2 天前
制造行业ERP如何选型?拥抱云端预算新时代
科技·oracle·制造·系统·erp
JuneXcy2 天前
第2章 数据库系统的核心--数据模型
数据库·mysql·oracle