oralce逗號分割變多行

2021-08-17 21:20:09 字數 3730 閱讀 7844

方法一select  a.*  , 

regexp_substr(a.rolecode ,'[^,]+',1,l) as rolecode 

from p_user a,(select level l from dual connect by level<=100) b

where l <=length(a.rolecode) - length(replace(rolecode,','))+1

使用函式regexp_substr拆分字串:

5個引數

第乙個是輸入的字串

第二個是正規表示式

第三個是標識從第幾個字元開始正規表示式匹配。(預設為1)

第四個是標識第幾個匹配組。(預設為1)

第五個是是取值範圍:

i:大小寫不敏感;

c:大小寫敏感; 

n:點號 . 不匹配換行符號;

m:多行模式;

x:擴充套件模式,忽略正規表示式中的空白字元。

select a.*,regexp_substr(servicereqid ,'[^;]+',1,l) as servicereq

from sum_portal_satisfaction a,(select level l from dual connect by level<=100) b

where l <=length(servicereqid) - length(replace(servicereqid,';'))+1

order by 1,2;

----select level l from dual connect by level<=100; 生成1到100的資料行。

----l <=length(servicereqid) - length(replace(servicereqid,';'))+1,注意此處是『l』並非『1』,上面的regexp_substr的第四個引數也一樣。

---下面為拆分字串,再進行的行轉列

create or replace view v_sum_portal_satisfaction_sr as

select 

survey_type,

survey_time,

center_code,

center_name,

city_id,

city_name,

regexp_substr(servicereqid ,'[^;]+',1,l) as servicereqid,

regexp_substr(servicereqname ,'[^;]+',1,l) as servicereqname,

sum(decode(survey_value,0, sur_times,null)) giveup_times,--調查值 -1:未處理 0:使用者放棄 1:很滿意 2.滿意 3.對csr不滿意 4.對其它不滿意

sum(decode(survey_value,1, sur_times,null))vsatis_times,

sum(decode(survey_value,2, sur_times,null))satis_times,

sum(decode(survey_value,3, sur_times,null))ncsr_times,

sum(decode(survey_value,4, sur_times,null))nelse_times,

sum(sur_times) sur_times

from sum_portal_satisfaction a,(select level l from dual connect by level<=100) b

where l <=length(servicereqid) - length(replace(servicereqid,';'))+1

group by

subslevelid,

center_code,

center_name,

city_id,

city_name,

survey_type,

survey_time,

servicereqid,

servicereqname,l

方法二:

create table  testtable (

id  nvarchar2(200) primary key not null ,

content  nvarchar2(200) not null 

)insert into  testtable values ('4','館內idx_10館外idx_11總體idx_12');

select *  from table ( cast (fn_split(('館內idx_1$館外idx_2$總體idx_3$') ,'$') as ty_str_split  )  )

select * from testtable b left join  table (fn_split((content), '$')   ) a   on 1=1;

--實現split函式

create or replace type ty_str_split is table of varchar2 (4000);

create or replace function fn_split (p_str in varchar2, p_delimiter in varchar2)

return ty_str_split

isj int := 0;

i int := 1;

len int := 0;

len1 int := 0;

str varchar2 (4000);

str_split ty_str_split := ty_str_split ();

begin

len := length (p_str);

len1 := length (p_delimiter);

while j < len

loop

j := instr (p_str, p_delimiter, i);

if j = 0

then

j := len;

str := substr (p_str, i);

str_split.extend;

str_split (str_split.count) := str;

if i >= len

then

exit;

end if;

else

str := substr (p_str, i, j - i);

i := j + len1;

str_split.extend;

str_split (str_split.count) := str;

end if;

end loop;

return str_split;

end fn_split;

declare

cursor c

isselect *  from table ( cast (fn_split(('館內idx_1$館外idx_2$總體idx_3$') ,'$') as ty_str_split  )  );

r c%rowtype;

begin

open c;

loop

fetch c into r;

exit when c%notfound;

dbms_output.put_line (r.column_value);

end loop;

close c;

end;

一行變多行

xml declare tb table departmentid int,departmentcode varchar 100 insert into tb select 1,1 2 union all select 2,1 2 3 union all select 3,1 2 3 4 union...

oracle 逗號分割 統計

看別人帖子自己仿照寫了乙個 c1 c2 c3 01 01 a,b,c 轉化為c0 c1 c2 c3 a 01 01 a,b,c b 01 01 a,b,c c 01 01 a,b,c select substr routeidlist,instr routeidlist 1,t2.row num i...

資料一行變多行的SQL查詢

資料一行變多行的sql查詢 對於資料表中的某一資料列,要求顯示時資料不能超過5位,如果超過則多行顯示。例如,當該列值為100000時,需要將其顯示為兩行,其中第一行的該列值為99999,第二行的該列值為1,其他列值完全相同。例如 有一張表產量表t,有三列資料,年份 year 車間號 id 產量 qt...