10g的wm_concat
11g的listagg,但是有4000字符的限制。
wm_concat用法舉例
with tt as (select 'tom' as name, '24' as age from dual
union all
select 'tom','22' from dual
union all
select 'bob','11' from dual
union all
select 'bob','09' from dual)
select tt.name,to_char(wm_concat(tt.age)) from tt group by tt.name;
結果如下
listagg用法舉例
with tt as (select 'tom' as name, '24' as age from dual
union all
select 'tom','22' from dual
union all
select 'bob','11' from dual
union all
select 'bob','09' from dual)
select tt.name,listagg(tt.age,',') within group(order by tt.age) as aa
from tt
group by tt.name;
結果如下
下方高能
用這兩個函數的時候發現一個問題,就是如果被轉換的列的類型是 NVARCHAR2,返回的結果就是空。
這個問題還沒有解決,懷疑是字符集的問題,但是改爲utf-8之後,還是有此情況。
有大神知曉其中原因的話,請留言,不勝感激