在搜索论坛后,我提出了以下内容,但它不起作用:/
我有一张包含以下内容的表格;
ID | Strings
123| abc fgh dwd
243| dfs dfd dfg
353| dfs
424| dfd dfw
523|
.
.
.
请注意,大约有20,000行,我的另一个选择是编写存储过程来执行此操作...基本上我需要将字符串拆分,以便每个字符串都有这样的行
ID | Strings
123| abc
123| fgh
123| dwd
243| dfs
依旧......
这就是我所拥有的。
create table Temp AS
SELECT ID, strings
From mytable;
SELECT DISTINCT ID, trim(regexp_substr(str, '[^ ]+', 1, level)) str
FROM (SELECT ID, strings str FROM temp) t
CONNECT BY instr(str, ' ', 1, level -1) >0
ORDER BY ID;
感谢任何帮助
答案 0 :(得分:6)
这应该可以解决问题:
SELECT DISTINCT ID, regexp_substr("Strings", '[^ ]+', 1, LEVEL)
FROM T
CONNECT BY regexp_substr("Strings", '[^ ]+', 1, LEVEL) IS NOT NULL
ORDER BY ID;
注意我在connect by子句中如何使用regexp_substr
。这是为了处理多个空格的情况。
如果每行的项目数有可预测的上限,则可能值得将上面的递归查询的性能与简单的CROSS JOIN
进行比较:
WITH N as (SELECT LEVEL POS FROM DUAL CONNECT BY LEVEL < 10)
-- ^^
-- up to 10 substrings
SELECT ID, regexp_substr("Strings", '[^ ]+', 1, POS)
FROM T CROSS JOIN N
WHERE regexp_substr("Strings", '[^ ]+', 1, POS) IS NOT NULL
ORDER BY ID;
请参阅http://sqlfiddle.com/#!4/444e3/1了解实时演示
答案 1 :(得分:4)
更灵活,更好的解决方案:
还有其他使用 XMLTABLE 和 MODEL子句的示例,请阅读Split comma delimited strings in a table。
例如,
没有ID列:
SQL> WITH T AS
2 (SELECT 'abc fgh dwd' AS text FROM dual
3 UNION
4 SELECT 'dfs dfd dfg' AS text FROM dual
5 UNION
6 SELECT 'dfs' AS text FROM Dual
7 UNION
8 SELECT 'dfd dfw' AS text FROM dual
9 )
10 SELECT trim(regexp_substr(t.text, '[^ ]+', 1, lines.column_value)) text
11 FROM t,
12 TABLE (CAST (MULTISET
13 (SELECT LEVEL FROM dual CONNECT BY instr(t.text, ' ', 1, LEVEL - 1) > 0
14 ) AS sys.odciNumberList )) lines
15 /
TEXT
-----------
abc
fgh
dwd
dfd
dfw
dfs
dfs
dfd
dfg
9 rows selected.
使用ID列:
SQL> WITH T AS
2 (SELECT 123 AS id, 'abc fgh dwd' AS text FROM dual
3 UNION
4 SELECT 243 AS id, 'dfs dfd dfg' AS text FROM dual
5 UNION
6 SELECT 353 AS Id, 'dfs' AS text FROM Dual
7 UNION
8 SELECT 424 AS id, 'dfd dfw' AS text FROM dual
9 )
10 SELECT id, trim(regexp_substr(t.text, '[^ ]+', 1, lines.column_value)) text
11 FROM t,
12 TABLE (CAST (MULTISET
13 (SELECT LEVEL FROM dual CONNECT BY instr(t.text, ' ', 1, LEVEL - 1) > 0
14 ) AS sys.odciNumberList )) lines
15 ORDER BY id
16 /
ID TEXT
---------- -----------
123 abc
123 fgh
123 dwd
243 dfs
243 dfd
243 dfg
353 dfs
424 dfd
424 dfw
9 rows selected.
SQL>
答案 2 :(得分:-1)
With T As
(select 123 as id, 'abc fgh dwd' as strings from dual
union
select 243 as id, 'dfs dfd dfg' as strings from dual
union
Select 353 As Id, 'dfs' As Strings From Dual
union
select 424 as id, 'dfd dfw' as strings from dual
)
select distinct id, REGEXP_SUBSTR (Replace(Strings, ' ', ','), '[^,]+', 1, level) as Strings
from t
Connect By Level <= Length(Regexp_Replace(Replace(Strings, ' ', ','),'[^,]*'))+1
order by id, strings;
**********OUTPUT*************
ID STRINGS
---------- -----------
123 abc
123 dwd
123 fgh
243 dfd
243 dfg
243 dfs
353 dfs
424 dfd
424 dfw
9 rows selected