--下面的精妙sql!
with test1 as (
select 1 as a,
'1,2,3,4,5,6,7,8,9,9,8,8,8,8,7,6,5,4,5,6,7,8,9,0,-,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2' as label from dual
)
SELECT *
FROM
(SELECT t1.a,
trim(SUBSTR(x2.label ,instr(x2.label, ',', 1, x2.rn) + 1, instr(x2.label, ',', 1, x2.rn + 1) - instr(x2.label, ',', 1, x2.rn) - 1)) label
FROM test1 t1,
(--笛卡尔积
SELECT ','||t1.label||',' AS label,
x1.rn
FROM test1 t1, ---!修改表名
(--这里构成一个序列
SELECT rownum rn
FROM
(SELECT MAX(LENGTH(t1.label) - LENGTH(REPLACE(t1.label, ','))) + 2 split_count --逗号的数量
FROM test1 t1 ---!修改表名
)
CONNECT BY rownum <= split_count
) x1
) x2
) x3
WHERE x3.label IS NOT NULL
;
//====方法二
with hour_temp as
(
select '01,11,12' as col_hour
from dual
)
select decode(a, 0, substr(hour, b), substr(hour, b, a - b)) as label
from
(
select hour, instr(hour, ',', 1, lv) a,
lag(instr(hour, ',', 1, lv), 1, 0) over(order by lv) + 1 b
from
(
select t.col_hour as hour, level as lv
from hour_temp t
connect by level <=
length(t.col_hour) - length(replace(t.col_hour, ',', '')) + 1
)
);