数据如下:
name id
张三 1,2,3
要求实现:
name id
张三 1
张三 2
张三 3
--创建临时表 create table tmp as(select ‘张三‘ name, ‘1,2,3‘ id from dual); --写法1 select name,regexp_substr(id,‘[^,]+‘,1,level) as id from tmp connect by level <= regexp_count(id,‘,‘)+1 order by id --写法2: select name,trim(regexp_substr(id,‘[^,]+‘,1,level)) as id from tmp connect by name = prior name and prior dbms_random.value is not null and level <= length(regexp_replace(id, ‘[^,]‘))+1; --写法3 select name, --regexp_replace(id,‘(\w+)\,(\w+)\,(\w+)‘,level) id regexp_replace(id,‘(\w+)\,(\w+)\,(\w+)‘,‘\‘||to_char(level)) id from tmp connect by level <= regexp_count(id,‘,‘)+1; --写法4: select name, substr(‘,‘||id||‘,‘,instr(‘,‘||id||‘,‘, ‘,‘, 1, level) + 1, instr(‘,‘||id||‘,‘, ‘,‘, 1, level + 1) -( instr(‘,‘||id||‘,‘, ‘,‘, 1, level) + 1)) from tmp connect by level <= regexp_count(id,‘,‘)+1 order by level
此外,列转行还可以使用union all和unpivot(oracle 11g新特性)等,待后续补充
时间: 2024-10-08 19:39:51