After splitting with the essays Oracle column data relation table query



----------------------- ------------------------- build the table
Table Test Create (int ID, the plist VARCHAR2 (30));

Create Table P (PID int, pname VARCHAR2 (10));

---------------------- - inserting test data ----------------------------
iNSERT INTO test values (. 1, '28345 | 39262 | 56214');
iNSERT INTO values Test (2, '28345 | 56214');
INSERT INTO Test values (. 3, '56214');


insert into p values (28345, 'product A');
INSERT INTO P values (39262, 'product B');
INSERT INTO P values (56214, 'product C');

 

----------------------------- break the statement and result ---------------- --------------------

 
select id, plist,level p_level, regexp_substr(plist , '[^|]+', 1, level) pid
from test
connect by level <= regexp_count(plist , '[^|]+')
and prior id = id
and prior dbms_random.value is not null

search result

 

 


 

------------------- association after the split and the stitched character processing statements -------------------

with m as (

--拆分列数据
select id, plist,level p_level, regexp_substr(plist , '[^|]+', 1, level) pid
from test
connect by level <= regexp_count(plist , '[^|]+')
and prior id = id
and prior dbms_random.value is not null  
)
select m.id , m.plist, listagg(p.pname,',') within group(order by p_level) rrr
from m inner join p on m.pid = p.pid
group by m.id, m.plist ;


 

 



DROP TABLE test;
DROP TABLE P;

Guess you like

Origin www.cnblogs.com/Bokeyan/p/11504921.html