錯誤信息
Error querying database. Cause: java.sql.SQLSyntaxErrorException: ORA-02000: 缺失 WITHIN 關鍵字
查詢語句
使用LISTAGG函數將多行數據合并為單行字符串,如下:
selectt.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(字段,‘,’)。
解決辦法
將查詢語句修改為如下:
selectt.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 payernamefrom t_order_info t
where 1=1
and t.order_no is not null;