閫氬父澶у閮借涓鴻繖綾婚棶棰樻棤娉曠敤涓鍙QL瑙e喅錛屾湰鏉ユ垜涔熻繖涔堣涓猴紝鍙槸浠婂ぉ鏃犳剰涓獊鐒舵湁浜嗙伒鎰燂紝鍘熸潵鏄彲浠ヨ繖涔堝仛鐨勶細 鍓嶅嚑澶╂湁浜烘彁鍒拌繃sys_connect_by_path鐨勭敤娉曪紝鎴戞兂榪欓噷鏄笉鏄篃鑳界敤鍒拌繖涓柟娉曪紝濡傛灉鑳藉仛鍒扮殑璇濓紝涓嶇敤鍑芥暟鎴栧瓨璐繃紼嬩篃鍙互鍋氬埌浜嗭紱瑕佺敤鍒皊ys_connect_by_path錛岄鍏堣鑷繁鏋勫緩鏍戝瀷鐨勭粨鏋勶紝騫朵笖鏍戠殑姣忎釜鍒嗘敮閮芥槸鍗曟牴鐨勶紝渚嬪1-銆?-銆?-銆?錛屼笉浼氬瓨鍦?-銆?錛?-銆?鐨勬儏鍐碉紱 鎴戞槸榪欎箞鏋勫緩鏍戯紝寰堢畝鍗曠殑錛岀湅涓嬮潰鐨勭粨鏋滃氨浼氱煡閬撲簡錛?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 /
鏈変簡榪欎釜鏍戝瀷鐨勭粨鏋勶紝鎺ヤ笅鏉ョ殑浜嬪氨濂藉姙浜嗭紝鍙鍙栧嚭鎷ユ湁鍏ㄨ礬寰勭殑閭d釜path錛岄棶棰樺氨瑙e喅浜嗭紝鍏堢湅no=‘001’鐨勫垎緇勶細 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
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