14:41:12 scott@ORCL> create table tb
14:41:37 2 as
14:41:38 3 select dbms_random.string('x', 4) random_string
14:41:38 4 from dual
14:41:38 5 connect by level <= 26;
Table created.
14:41:38 scott@ORCL> select * from tb;
RANDOM_STRING
--------------
AXMU
6R5D
ASIE
A3Y0
VP82
VIVM
G8RC
ANB6
37V2
GQRG
6QM2
F2BX
4Z37
PG9Q
5E6H
FHKD
0EHR
UPG2
FV4A
K7JK
7VJZ
4LX9
M2KV
7BU6
44T9
RSZJ
-----要求查询结果如下显示,st的值五个分一组合并为一行显示:
-----显示结果如下:
ST
--------------------------------------------------
AXMU,6R5D,ASIE,VP82,A3Y0
VIVM,G8RC,ANB6,GQRG,37V2
6QM2,5E6H,PG9Q,F2BX,4Z37
FHKD,UPG2,K7JK,FV4A,0EHR
7VJZ,44T9,7BU6,M2KV,4LX9
RSZJ
语句如下:
14:45:34 scott@ORCL> select WMSYS.WM_CONCAT(random_string) st
14:45:50 2 from (select rownum-1 sn ,random_string from tb)
14:45:50 3 group by trunc(sn/5);
ST
--------------------------------------------------
AXMU,6R5D,ASIE,VP82,A3Y0
VIVM,G8RC,ANB6,GQRG,37V2
6QM2,5E6H,PG9Q,F2BX,4Z37
FHKD,UPG2,K7JK,FV4A,0EHR
7VJZ,44T9,7BU6,M2KV,4LX9
RSZJ
6 rows selected.
如果分隔符不希望使用逗号',',可以使用replace函数置换成其他字符,例如:
14:52:25 scott@ORCL> select replace(WMSYS.WM_CONCAT(random_string),',','|') st
14:52:36 2 from (select rownum-1 sn ,random_string from tb)
14:52:36 3 group by trunc(sn/5);
ST
--------------------------------------------------
AXMU|6R5D|ASIE|VP82|A3Y0
VIVM |G8RC|ANB6|GQRG|37V2
6QM2|5E6H|PG9Q|F2BX|4Z37
FHKD|UPG2|K7JK|FV4A|0EHR
7VJZ |44T9|7BU6|M2KV|4LX9
RSZJ