160519、Oracle中將查詢出的多條記錄的某個字段拼接成一個字符串的方法


with temp as( 
select 'China' nation ,'Guangzhou' city from dual union all 
select 'China' nation ,'Shanghai' city from dual union all 
select 'China' nation ,'Beijing' city from dual union all 
select 'USA' nation ,'New York' city from dual union all 
select 'USA' nation ,'Bostom' city from dual union all 
select 'Japan' nation ,'Tokyo' city from dual 

select nation,listagg(city,',') within GROUP (order by city) 
from temp 
group by nation 

這是最基礎的用法:
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)

用法就像聚合函數一樣,通過Group by語句,把每個Group的一個字段,拼接起來。

實戰sql:

select listagg(t.SPURIOUS_DESC, ';') within GROUP(order by t.spurious_desc) SPURIOUS_DESC
from zc_tr_appraise t
where t.banknote_id = '1';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM