将列字符拆分为行Oracle

时间:2014-03-12 12:37:43

标签: sql oracle split

我到处寻找这个功能,我几乎放弃了。我有一个看起来像这样的Oracle表:

name      Number_of_Leaves    status
cheque1        5              PPURI
cheque4        10             UUSPDRIPPP

状态列显示每个检查叶的状态,包括“通过”,“已停止”,“已销毁”,“已发布”等。 我需要在这样的视图中表示信息:对于每个检查状态,我应该有一行:

name      Number_of_Leaves    status
    cheque1        5              P
    cheque1        5              P
    cheque1        5              U
    cheque1        5              R
    cheque1        5              I
    cheque4        10             U
    cheque4        10             U
    cheque4        10             S
    cheque4        10             P
    cheque4        10             D
    ...............................

这是可以实现的吗?

2 个答案:

答案 0 :(得分:1)

是的,我的朋友可能:

with tab1(rn,name,status,Number_of_Leaves,tes) as 
(select 1 rn,name,status,Number_of_Leaves,substr(status,1,1) tes from table1
union all 
select rn + 1 rn,name,status,Number_of_Leaves,substr(status,rn + 1,1) tes from tab1
where rn < Number_of_Leaves),
tab2 as (select status,rn,name,Number_of_Leaves,Tes from 
tab1 order by status,rn)
select name,Number_of_Leaves,Tes status from tab2;

SQL Fiddle

答案 1 :(得分:1)

只需自己添加此功能;)例如,使用TABLE运算符和预定义的分割函数:

CREATE OR REPLACE TYPE t_char IS TABLE OF VARCHAR2(1)

/

CREATE OR REPLACE FUNCTION TMP_SPLIT(p_val VARCHAR2)
RETURN t_char IS
v_res t_char;
BEGIN
v_res := t_char(SUBSTR(p_val,1,1));
v_res.extend(LENGTH(p_val)-1);
FOR i IN 2..LENGTH(p_val) LOOP
    v_res(i) := SUBSTR(p_val,i,1);
END LOOP;
RETURN v_res;
END;

/

WITH dat AS (SELECT 'cheque1' name,
                    5 Number_of_Leaves,
                    'PPURI' status FROM DUAL
             UNION 
             SELECT 'cheque4' name,
                    10 Number_of_Leaves,
                    'UUSPDRIPPP' status FROM DUAL)
SELECT dat.name, dat.number_of_leaves, spl.*
FROM dat 
     LEFT JOIN TABLE(TMP_SPLIT(dat.status)) spl ON 1=1

/

调用现有表的函数示例:

SELECT table1.name, table1.number_of_leaves, spl.*
FROM table1
     LEFT JOIN TABLE(TMP_SPLIT(table1.status)) spl ON 1=1