锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
澶ф鏄被浼間笅鍒楃殑鎯呭艦錛?nbsp;
SQL> select no,q from test
2 /
NO Q
---------- ------------------------------
001 n1
001 n2
001 n3
001 n4
001 n5
002 m1
003 t1
003 t2
003 t3
003 t4
003 t5
003 t6
12 rows selected
鏈鍚庤寰楀埌綾諱技浜庡涓嬬殑緇撴灉錛?nbsp;
001 n1;n2;n3;n4;n5
002 m1
003 t1;t2;t3;t4;t5;t6
閫氬父澶у閮借涓鴻繖綾婚棶棰樻棤娉曠敤涓鍙QL瑙e喅錛屾湰鏉ユ垜涔熻繖涔堣涓猴紝鍙槸浠婂ぉ鏃犳剰涓獊鐒舵湁浜?jiǎn)鐏垫劅锛屽師鏉ユ槸鍙互杩欎箞鍋氱殑锛?xì)
鍓嶅嚑澶╂湁浜烘彁鍒拌繃sys_connect_by_path鐨勭敤娉曪紝鎴戞兂榪欓噷鏄笉鏄篃鑳界敤鍒拌繖涓柟娉曪紝濡傛灉鑳藉仛鍒扮殑璇濓紝涓嶇敤鍑芥暟鎴栧瓨璐繃紼嬩篃鍙互鍋氬埌浜?jiǎn)锛涜鐢ㄥ埌sys_connect_by_path錛岄鍏堣鑷繁鏋勫緩鏍?wèi)鍨嬬殑缁撴瀯锛岒q朵笖鏍?wèi)鐨勬瘡涓垎鏀兘鏄崟鏍圭殑锛屼緥濡?-銆?-銆?-銆?錛屼笉浼?xì)瀛樺?-銆?錛?-銆?鐨勬儏鍐碉紱
鎴戞槸榪欎箞鏋勫緩鏍?wèi)锛屽緢绠鍗曠殑錛岀湅涓嬮潰鐨勭粨鏋滃氨浼?xì)鐭ラ亾浜?jiǎn)錛?nbsp;
SQL> select no,q,rn,lead(rn) over(partition by no order by rn) rn1
2 from (select no,q,row_number() over(order by no,q desc) rn from test)
3 /
NO Q RN RN1
---------- ------------------------------ ---------- ----------
001 n5 1 2
001 n4 2 3
001 n3 3 4
001 n2 4 5
001 n1 5
002 m1 6
003 t6 7 8
003 t5 8 9
003 t4 9 10
003 t3 10 11
003 t2 11 12
003 t1 12
12 rows selected
鏈変簡(jiǎn)榪欎釜鏍?wèi)鍨嬬殑缁撴瀯锛屾帴涓嬫潵鐨勪簨灏卞ソ鍔炰簡(jiǎn)锛屽彧瑕佸彇鍑烘嫢鏈夊叏璧\寰勭殑閭d釜path錛岄棶棰樺氨瑙e喅浜?jiǎn)锛屽厛鐪媙o=‘001’鐨勫垎緇勶細(xì)
select no,sys_connect_by_path(q,';') result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = '001' and rn1 is null connect by rn1 = prior rn
SQL>
6 /
NO RESULT
---------- --------------------------------------------------------------------------------
001 ;n1
001 ;n1;n2
001 ;n1;n2;n3
001 ;n1;n2;n3;n4
001 ;n1;n2;n3;n4;n5
涓婇潰緇撴灉鐨勬渶鍚?鏉″氨鏄垜浠寰楃粨鏋滀簡(jiǎn)
瑕佸緱鍒版瘡緇勭殑緇撴灉錛屽彲浠ヤ笅闈㈣繖鏍?nbsp;
select t.*,
(
select max(sys_connect_by_path(q,';')) result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = t.no and rn1 is null connect by rn1 = prior rn
) value
from (select distinct no from test) t
SQL>
10 /
NO VALUE
---------- --------------------------------------------------------------------------------
001 ;n1;n2;n3;n4;n5
002 ;m1
003 ;t1;t2;t3;t4;t5;t6
瀵逛笂闈㈢粨鏋滅◢鍔犲鐞嗗氨鍙互浜?jiǎn)锛屽笇鏈涘澶у鏈夊府鍔╁Q氾級(jí)
]]>