转置查询..我需要处理150万条记录

时间:2014-02-02 04:20:31

标签: sql oracle oracle10g

需要帮助

ACTUAL TABLE

COL1  COL2
A     1,2,3,4

我需要转换

COL1   COL2
A       1
A       2
A       3
A       4

1 个答案:

答案 0 :(得分:1)

可能的行数很大!!哎呀..这可能是一个糟糕的方法。

SELECT col1,REGEXP_SUBSTR (col2,,'[^,]+',1,LEVEL) txt
  FROM my_table
CONNECT BY REGEXP_SUBSTR (col2,'[^,]+',1,LEVEL) IS NOT NULL
   AND PRIOR col2 = col2
   AND PRIOR sys_guid() IS NOT NULL;

另一个版本FRom Here

SELECT col1, REGEXP_SUBSTR(t1.col2, '([^,])+', 1, t2.COLUMN_VALUE)
FROM my_table t1 CROSS JOIN
            TABLE
            (
                CAST
                (
                    MULTISET
                    (
                        SELECT LEVEL
                        FROM DUAL 
                        CONNECT BY LEVEL <= REGEXP_COUNT(t1.colval, '([^,])+')
                    )
                    AS SYS.odciNumberList
                )
            ) t2;