oracle列值合併
來源:易賢網(wǎng) 閱讀:1318 次 日期:2014-10-21 14:13:16
溫馨提示:易賢網(wǎng)小編為您整理了“oracle列值合併”,方便廣大網(wǎng)友查閱!

合併列值最通用的方法就是寫一個自定義函數(shù)去實(shí)現(xiàn),這裏介紹的是其他方法。

在sql server中合併列值可以使用for xml path,在oracle中則可以使用wm_concat 或 listagg。

準(zhǔn)備數(shù)據(jù):

create table mytest(xtype number,city nvarchar2(200));

/

insert into mytest(xtype,city)

select 1,n'北京' from dual union all

select 1,n'上海' from dual union all

select 1,n'廣州' from dual union all

select 2,n'武漢' from dual union all

select 2,n'杭州' from dual union all

select 2,n'廈門' from dual

commit;

/

使用wm_concat:

select xtype,wmsys.wm_concat(to_char(city)) as xcity

from mytest

group by xtype

使用listagg:

select xtype,

listagg(to_char(city),',') within group(order by xtype) as xcity

from mytest

group by xtype

結(jié)果:

xtype xcity

---------- --------------------------------------

1 北京,廣州,上海

2 杭州,武漢,廈門

備註:

0、上面在city列前都加了to_char()函數(shù),是為了防止出現(xiàn)亂碼的情況;

1、wm_concat 在oracel的官方文檔中沒有,不能保證各版本的兼容性;

2、listagg是11g版本才出現(xiàn)的新的聚集函數(shù)。

更多信息請查看IT技術(shù)專欄

更多信息請查看數(shù)據(jù)庫
易賢網(wǎng)手機(jī)網(wǎng)站地址:oracle列值合併
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn)

版權(quán)所有:易賢網(wǎng)