oracle 逗號分割,列轉行,行轉列 轉載

https://www.cnblogs.com/gdzhong/p/4726315.html?tvd

 

SQL代碼 列轉行

複製代碼

select
REGEXP_SUBSTR(a.rolecode ,'[^,]+',1,l)
rolecode
from (
select 'a,aa,aaa' rolecode from dual
) a,
(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=100) b
WHERE l <=LENGTH(a.rolecode) - LENGTH(REPLACE(rolecode,','))+1  

複製代碼

 

或者

with a as (select 'ABC,AA,AD,ABD,JI,CC,ALSKD,ALDKDJ' id from dual)
select regexp_substr(id,'[^,]+',1,rownum) id from a
connect by rownum <= length(regexp_replace(id,'[^,]+'))

 

SQL代碼 行轉列

 

複製代碼

select name,coures,to_char(wmsys.wm_concat(xxx.score)) c 
 from 
 
 
  (select '小明' name,'語文' coures,90 score  from dual
 union all
 select '小明' name,'語文' coures,91 score  from dual
  union all
 select '小明' name,'數學' coures,90 score  from dual
 union  all
 select '小明' name,'數學' coures,91 score  from dual) xxx
 
  group by xxx.name,coures

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章