我的数据库中有列,其中的值如下所示:
3862,3654,3828
在虚拟列中没有任何编号。逗号分隔的值可以出现。我尝试了以下查询,但它正在创建重复的结果。
select regexp_substr(dummy,'[^,]+',1,Level) as dummycol from (select * from dummy_table) connect by level <= length(REGEXP_REPLACE(dummy,'[^,]+'))+1
我不明白这个问题。谁能帮忙?
非常适合我-
SQL> WITH dummy_table AS( 2 SELECT '3862,3654,3828' dummy FROM dual 3 ) 4 SELECT trim(regexp_substr(dummy,'[^,]+',1,Level)) AS dummycol 5 FROM dummy_table 6 CONNECT BY level <= LENGTH(REGEXP_REPLACE(dummy,'[^,]+'))+1 7 / DUMMYCOL -------------- 3862 3654 3828 SQL>
还有许多其他方法可以实现它。阅读将单个逗号分隔的字符串拆分成行。
*关于使用列而不是单个字符串值时的重复项的 *更新 。只见PRIOR子句中使用DBMS_RANDOM的摆脱循环回路的在这里
尝试以下方法
SQL> WITH dummy_table AS 2 ( SELECT 1 rn, '3862,3654,3828' dummy FROM dual 3 UNION ALL 4 SELECT 2, '1234,5678' dummy FROM dual 5 ) 6 SELECT trim(regexp_substr(dummy,'[^,]+',1,Level)) AS dummycol 7 FROM dummy_table 8 CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(dummy,'[^,]+'))+1 9 AND prior rn = rn 10 AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL 11 / DUMMYCOL -------------- 3862 3654 3828 1234 5678 SQL>
更新2
其他方式,
SQL> WITH dummy_table AS 2 ( SELECT 1 rn, '3862,3654,3828' dummy FROM dual 3 UNION ALL 4 SELECT 2, '1234,5678,xyz' dummy FROM dual 5 ) 6 SELECT trim(regexp_substr(t.dummy, '[^,]+', 1, levels.column_value)) AS dummycol 7 FROM dummy_table t, 8 TABLE(CAST(MULTISET 9 (SELECT LEVEL 10 FROM dual 11 CONNECT BY LEVEL <= LENGTH (regexp_replace(t.dummy, '[^,]+')) + 1 12 ) AS sys.OdciNumberList)) LEVELS 13 / DUMMYCOL -------------- 3862 3654 3828 1234 5678 xyz 6 rows selected. SQL>