Oracle 正則 一行轉多行

2022-01-21 20:46:53 字數 3700 閱讀 9363

select regexp_substr(bjdm||',

', '

[^,]+

', 1, level, '

i') as

str,bjdm from

valueweekinfo

connect

bylevel

<= length(bjdm) - length(regexp_replace(bjdm, '

,', '')) +

1;

可以將 bjdm 換成 '01,02,03,04' , valueweekinfo 換成dual 檢視結果

select regexp_substr(zzjs, '

[^,]+

', 1, rownum, '

i') str,zzjs from

(select zzjs from bjpjxxb group

byzzjs )

connect

by rownum <= length(zzjs) - length(regexp_replace(zzjs, '

,', ''))+

1

有時用rownum會出現 一些問題,結合上面的level使用

with test as

(select

1 id1, '

aaa,bbb,ccc,ddd

'id2

from

dual

union

allselect

2 id1, '

eee,fff,ggg,hhh,kkkk,ivwshjj

' id2 from

dual)

select

id1,

substr(t.ca,

instr(t.ca, ',

', 1, d.lv) +1,

instr(t.ca, ',

', 1, d.lv +

1) -

(instr(t.ca, ',

', 1, d.lv) +

1)) as

d

from (select

id1,

','|| id2 ||',

'asca, length(id2 ||'

,') - nvl(length(replace(id2, '

,')), 0) as

cnt

from

test) t,

(select

level lv from dual connect by

level

<=

100) d

where d.lv <=

t.cnt

order

byid1

select substr(t.classid,instr(t.classid, '

,', 1, d.lv) +1,

instr(t.classid, ',

', 1, d.lv +

1) -

(instr(t.classid, ',

', 1, d.lv) +

1)) str,classid,vid from

(select vid,','

|| classid ||',

'classid

,length(classid ||'

,') - nvl(length(replace(classid, '

,')), 0) as cnt from

classvedio

where createtime between

'2015-09-01

'and

'2015-12-31

'group

byclassid,vid) t,

(select

level lv from dual connect by

level

<=

100) d

where d.lv <=

t.cnt

order

by vid,str

desc

select regexp_substr(classid, '

[^,]+

', 1, lv, '

i') as

str,classid,vid,lv from

(select

*from

(select vid,','

|| classid ||',

'classid

,length(classid ||'

,') - nvl(length(replace(classid, '

,')), 0) as

cntfrom

classvedio) c,

(select

level lv from dual connect by

level

<= (select

max(length(classid)) from

classvedio)) d

where d.lv <=

c.cnt

order

by vid desc,lv asc

)

最終完成版本

///

///將某錶的某列拆分成多行資料,返回的結構為

///str,拆分出來的每乙個字段

///lv,拆分行數

///primarycols

///key

/// ///

表名 ///

表主鍵列

///要拆分的列

///分隔字串

///public

static

string getcommand(string tablename, string primarycols, string key,string

splitchar)

, '[^]+', 1, lv, 'i') as str,,,lv from (

", key, primarycols,splitchar));

string.format("

select * from (select ,'' || || ''

", primarycols, key,splitchar));

string.format( "

,length( || '') - nvl(length(replace(, '')), 0) as cnt

",key,splitchar));

string.format( "

from ) c,

",tablename));

string.format("

(select level lv from dual connect by level <= (select max(length()) from )) d

", key, tablename));

string.format( "

where d.lv <= c.cnt order by ,lv asc

",primarycols));")

");return

sb.tostring();

}

ORACLE 一行轉多行

解決方法的核心是 產生出1 10的乙個列,作為 輔助列 select level l from dual connect by level 10 問題 兩個表 a b a 表 id pid a1 1 a2 2 a3 3 b 表 pid pnumber 1 22 3 3 5要根據pnumber的數量生...

hive使用技巧(五) 一行轉多行,多行轉一行

hive使用技巧 一 自動化動態分配表分割槽及修改hive表字段名稱 hive使用技巧 二 共享中間結果集 hive使用技巧 三 巧用group by實現去重統計 hive使用技巧 四 巧用mapjoin解決資料傾斜問題 hive使用技巧 五 一行轉多行,多行轉一行 在資料處理過程中,經常需要使用一...

一行轉多行效果 一

一行轉多行效果顯示 sql with test as select a 4,10 from dual union select b 2,8 from dual select from test a 4 10 a 4 10 b 2 8 需求 現有資料表中的資料形式為 名稱 數量 單價 a 4 10 b...