国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

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

2024-08-29 14:00:41
字體:
供稿:網(wǎng)友

工作和學(xué)習(xí)中常常會遇到一行要分割成多行數(shù)據(jù)的情況,在此整理一下做下對比。

單行拆分

如果表數(shù)據(jù)只有一行,則可以直接在原表上直接使用connect by+正則的方法,比如:

select regexp_substr('444.555.666', '[^.]+', 1, level) colfrom dualconnect by level <= regexp_count('444.555.666', '/.') + 1 

輸出結(jié)果:

COL----444555666

多行拆分

如果數(shù)據(jù)表存在多行數(shù)據(jù)需要拆分,也可以在原表上使用connect+正則的方法:

方法一

with t as(select '111.222.333' colfrom dualunion allselect '444.555.666' colfrom dual)select regexp_substr(col, '[^.]+', 1, level)from tconnect by level <= regexp_count(col, '/./') + 1and col = prior coland prior dbms_random.value > 0

結(jié)果:

---------111222333444555666

方法二

使用構(gòu)造的最大行數(shù)值關(guān)聯(lián)原表:

with t as(select '111.222.333' colfrom dualunion allselect '444.555.666' colfrom dual)select regexp_substr(col, '[^.]+', 1, lv)from t, (select level lv from dual connect by level < 10) bwhere 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' colfrom dualunion allselect '444.555.666' colfrom dual)select column_valuefrom t,table(cast(multiset(select regexp_substr(col, '[^.]+', 1, level) ddfrom dualconnect by level <= regexp_count(t.col, '/./') + 1) assys.odcivarchar2list)) a 

結(jié)果:

COLUMN_VALUE-------------111222333444555666

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

方法四

with t as(select '111.222.333' colfrom dualunion allselect '444.555.666' colfrom dual)select regexp_substr(col, '[^.]+', 1, trim(column_value))from t,xmltable(concat('1 to ',regexp_count(t.col, '/./') + 1)) a ;

注意:大數(shù)據(jù)量的拆分時,謹(jǐn)慎使用正則的方法去做,可以使用substr+instr的方式替換正則。

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

總結(jié)

以上就是本文關(guān)于Oracle數(shù)據(jù)行拆分多行方法示例的全部內(nèi)容,希望對大家有所幫助。有什么問題可以隨時留言,歡迎大家交流討論。


注:相關(guān)教程知識閱讀請移步到oracle教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛扎县| 博野县| 繁昌县| 屯门区| 巴彦淖尔市| 辽宁省| 三河市| 江达县| 余姚市| 宜黄县| 金堂县| 收藏| 宝丰县| 雷山县| 建瓯市| 和顺县| 宁阳县| 夹江县| 高平市| 山西省| 林口县| 远安县| 保靖县| 吴川市| 江都市| 杨浦区| 望江县| 深泽县| 商都县| 丰原市| 青神县| 新乐市| 老河口市| 潢川县| 朝阳市| 西畴县| 区。| 洛隆县| 什邡市| 永福县| 昭平县|