[英]Oracle split table column delimited values to to rows
Need to help with following select col1, col2, split(col3) from test;需要帮助从测试中选择 col1、col2、split(col3);
Table values:表值:
Col1 col2 col3
xxx yyy a,b,c
iii jjj 1,2,3
col3 contains comma-delimited values. col3 包含逗号分隔的值。
looking to achieve the following result.希望达到以下结果。 Splitting Col3 values for same col1 and col2 vlaue.
为相同的 col1 和 col2 vlaue 拆分 Col3 值。
col1 col2 split
xxx yyy a
xxx yyy b
xxx yyy c
iii jjj 1
iii jjj 2
iii jjj 3
Any regex or pl/sql function idea to help with this.任何正则表达式或 pl/sql 函数的想法来帮助解决这个问题。
One option uses a standard recursive query:一种选择使用标准递归查询:
with cte (col1, col2, pos, split, rest) as (
select col1, col2, 1,
substr(col3 || ',', 1, instr(col3 || ',', ',') - 1),
substr(col3 || ',', instr(col3 || ',', ',') + 1)
from mytable
union all
select col1, col2, pos + 1,
substr(rest, 1, instr(rest, ',') - 1),
substr(rest, instr(rest, ',') + 1)
from cte
where instr(rest, ',') > 0
)
select col1, col2, split, pos from cte order by col1, col2, pos
Demo on DB Fiddlde : DB Faddlde 上的演示:
COL1 | COL2 | SPLIT | POS :--- | :--- | :---- | --: iii | jjj | 1 | 1 iii | jjj | 2 | 2 iii | jjj | 3 | 3 xxx | yyy | a | 1 xxx | yyy | b | 2 xxx | yyy | c | 3
One option would be using Regular Expressions in order to split by commas and count the comma-seperated portions for the column col3
within a Hierarchical Query :一种选择是使用正则表达式以按逗号分隔并计算分层查询中列
col3
的逗号分隔部分:
SELECT col1, col2, REGEXP_SUBSTR(col3,'[^,]',1,level) AS split
FROM t
CONNECT BY level <= REGEXP_COUNT(col3,',') + 1
AND PRIOR SYS_GUID() IS NOT NULL
AND PRIOR col1 = col1;
COL1 COL2 SPLIT
iii jjj 1
iii jjj 2
iii jjj 3
xxx yyy a
xxx yyy b
xxx yyy c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.