欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Oracle數(shù)據(jù)行拆分多行方法示例

 更新時間:2017年10月11日 12:09:17   作者:mellowsmile  
oracle數(shù)據(jù)庫使用過程中,怎樣將一行或者多行數(shù)據(jù)分割成需要的多行數(shù)據(jù),本文我們就來看看具體方法,需要的朋友可以參考。

工作和學(xué)習(xí)中常常會遇到一行要分割成多行數(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è)置第二個數(shù)據(jù)集的時候要小于可能的最大值,然后兩數(shù)據(jù)集做關(guān)聯(lián),在做大數(shù)據(jù)量拆分的時候,這個數(shù)值設(shè)置得當(dāng),拆分行數(shù)相對一致的情況下,效率比方法一直接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

這個方法輸出的列名是固定的,column_value依賴于sys.odcivarchar2list這個類型的輸出,該方法對于大數(shù)據(jù)量的拆分效率比第二個方法好。

方法四

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ù)量的拆分時,謹慎使用正則的方法去做,可以使用substr+instr的方式替換正則。

如果以上方法的效率仍然不理想,可考慮使用plsql塊。

總結(jié)

以上就是本文關(guān)于Oracle數(shù)據(jù)行拆分多行方法示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:PLSQL Developer登錄的默認密碼介紹、ORACLE SQL語句優(yōu)化技術(shù)要點解析、淺談oracle rac和分布式數(shù)據(jù)庫的區(qū)別oracle 數(shù)據(jù)庫啟動階段分析等。有什么問題可以隨時留言,歡迎大家交流討論。

相關(guān)文章

最新評論