怎樣Oracle把多條記錄的相同欄位拼成一個字串

zenzuguo發表於2009-09-16
select 別名 from(
select row_number()over(order by lv desc) id,name from
(
select level lv,replace(sys_connect_by_path(欄位,','),',','') 別名from
(
select name,row_number()over(order by 欄位) id from 查詢結果
)
connect by prior id = id-1
)
)where id=1;

*************
測試log:
*************
[TEST@ORA1] SQL>select * from test2;

NAM ID
--- ----------
aaa 234
bbb 786
ccc 879

[TEST@ORA1] SQL>select name from(
2 select row_number()over(order by lv desc) id,name from
3 (
4 select level lv,replace(sys_connect_by_path(name,','),',','') name from
5 (
6 select name,row_number()over(order by name) id from test2
7 )
8 connect by prior id = id-1
9 )
10 )where id=1;

NAME
----------------------------------
aaabbbccc
*************************
10g以上(含10g):
select replace(wm_concat(欄位),',','') 別名 from 查詢結果;
測試log:
[TEST@ORA1] SQL>select * from test2;
NAM ID
--- ----------
aaa 234
bbb 786
ccc 879

[TEST@ORA1] SQL>select replace(wm_concat(name),',','') name from test2;

NAME
-----------------------------------------
aaabbbccc[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/106285/viewspace-1027020/,如需轉載,請註明出處,否則將追究法律責任。

相關文章