(查找函数时找到. copy过来记录一下. 源地址:http://blog.csdn.net/yy_mm_dd/article/details/3182953)
wmsys.wm_concat这个函数很有用,它的作用是以',' 链接字符
示例如下. 创建数据表.
create table idtable (id number,name varchar2(30));
insert into idtable values(10,'ab');
insert into idtable values(10,'bc');
insert into idtable values(10,'cd');
insert into idtable values(20,'hi');
insert into idtable values(20,'ij');
insert into idtable values(20,'mn');
insert into idtable values(10,'ab');
insert into idtable values(10,'bc');
insert into idtable values(10,'cd');
insert into idtable values(20,'hi');
insert into idtable values(20,'ij');
insert into idtable values(20,'mn');
查询如下
1.
select * from idtable;
ID NAME
---------- ------------------------------
10 ab
10 bc
10 cd
20 hi
20 ij
20 mn
2.
select id,wmsys.wm_concat(name) name from idtable
ID NAME
---------- --------------------------------------------------------------------------------
10 ab,bc,cd
10 ab,bc,cd
10 ab,bc,cd
20 ab,bc,cd,hi,ij,mn
20 ab,bc,cd,hi,ij,mn
20 ab,bc,cd,hi,ij,mn
3.---------- --------------------------------------------------------------------------------
10 ab,bc,cd
10 ab,bc,cd
10 ab,bc,cd
20 ab,bc,cd,hi,ij,mn
20 ab,bc,cd,hi,ij,mn
20 ab,bc,cd,hi,ij,mn
select id,wmsys.wm_concat(name) over (order by id,name) name from idtable;
ID NAME
---------- --------------------------------------------------------------------------------
10 ab
10 ab,bc
10 ab,bc,cd
20 ab,bc,cd,hi
20 ab,bc,cd,hi,ij
20 ab,bc,cd,hi,ij,mn
4.---------- --------------------------------------------------------------------------------
10 ab
10 ab,bc
10 ab,bc,cd
20 ab,bc,cd,hi
20 ab,bc,cd,hi,ij
20 ab,bc,cd,hi,ij,mn
select id,wmsys.wm_concat(name) over (partition by id) name from idtable;
ID NAME
---------- --------------------------------------------------------------------------------
10 ab,bc,cd
10 ab,bc,cd
10 ab,bc,cd
20 hi,ij,mn
20 hi,ij,mn
20 hi,ij,mn
5.
select id,wmsys.wm_concat(name) over (partition by id,name) name from idtable;
---------- --------------------------------------------------------------------------------
10 ab,bc,cd
10 ab,bc,cd
10 ab,bc,cd
20 hi,ij,mn
20 hi,ij,mn
20 hi,ij,mn
5.
select id,wmsys.wm_concat(name) over (partition by id,name) name from idtable;
ID NAME
---------- --------------------------------------------------------------------------------
10 ab
10 bc
10 cd
20 hi
20 ij
20 mn
---------- --------------------------------------------------------------------------------
10 ab
10 bc
10 cd
20 hi
20 ij
20 mn