您好,登錄后才能下訂單哦!
小編給大家分享一下Oracle數(shù)據(jù)行拆分多行的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
單行拆分
如果表數(shù)據(jù)只有一行,則可以直接在原表上直接使用connect by+正則的方法,比如:
select regexp_substr('444.555.666', '[^.]+', 1, level) col from dual connect by level <= regexp_count('444.555.666', '\.') + 1
輸出結(jié)果:
COL ---- 444 555 666
多行拆分
如果數(shù)據(jù)表存在多行數(shù)據(jù)需要拆分,也可以在原表上使用connect+正則的方法:
方法一
with t as (select '111.222.333' col from dual union all select '444.555.666' col from dual) select regexp_substr(col, '[^.]+', 1, level) from t connect by level <= regexp_count(col, '\.\') + 1 and col = prior col and prior dbms_random.value > 0
結(jié)果:
--------- 111 222 333 444 555 666
方法二
使用構(gòu)造的最大行數(shù)值關(guān)聯(lián)原表:
with t as (select '111.222.333' col from dual union all select '444.555.666' col from dual) select regexp_substr(col, '[^.]+', 1, lv) from t, (select level lv from dual connect by level < 10) b where b.lv <= regexp_count(t.col, '\.\') + 1
這種方法設(shè)置第二個(gè)數(shù)據(jù)集的時(shí)候要小于可能的最大值,然后兩數(shù)據(jù)集做關(guān)聯(lián),在做大數(shù)據(jù)量拆分的時(shí)候,這個(gè)數(shù)值設(shè)置得當(dāng),拆分行數(shù)相對(duì)一致的情況下,效率比方法一直接connect by要高。
方法三
使用table函數(shù):
with t as (select '111.222.333' col from dual union all select '444.555.666' col from dual) select column_value from t, table(cast(multiset (select regexp_substr(col, '[^.]+', 1, level) dd from dual connect by level <= regexp_count(t.col, '\.\') + 1) as sys.odcivarchar2list)) a
結(jié)果:
COLUMN_VALUE ------------- 111 222 333 444 555 666
這個(gè)方法輸出的列名是固定的,column_value依賴(lài)于sys.odcivarchar2list這個(gè)類(lèi)型的輸出,該方法對(duì)于大數(shù)據(jù)量的拆分效率比第二個(gè)方法好。
方法四
with t as (select '111.222.333' col from dual union all select '444.555.666' col from dual) select regexp_substr(col, '[^.]+', 1, trim(column_value)) from t, xmltable(concat('1 to ',regexp_count(t.col, '\.\') + 1)) a ;
注意:大數(shù)據(jù)量的拆分時(shí),謹(jǐn)慎使用正則的方法去做,可以使用substr+instr的方式替換正則。
如果以上方法的效率仍然不理想,可考慮使用plsql塊。
以上是“Oracle數(shù)據(jù)行拆分多行的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。