我有一个名为tbl_notes需要使用Oracle的表。它有2列ID和Notes。
Select * from tbl_notes where id > 11 and id < 15显示:
D_
12 .运输至MEE的再转机产品
13 .在通往M44、MTT、MZZ的道路上
14台产品已向M99进货
我想让它显示:
D_
12 ._
13 .生产成本-间接M44
13 .再接再分配MTT法
13 -中转站
14
14 .准工业用M99
发布于 2021-06-09 16:01:46
这是一个有点麻烦的数据模型,试图提取这些值可能会导致误报,但您可以使用regexp_substr()和分层查询(或递归CTE)尝试它--本质上将其视为分隔文本:
select id, notes, regexp_substr(notes, '(M[[:alnum:]]{2})([^[:alnum:]]|$)', 1, level, null, 1) as locations
from tbl_notes
connect by level < regexp_count(notes, '(M[[:alnum:]]{2})([^[:alnum:]]|$)')
and id = prior id
and prior sys_guid() is not null用你的数据得到:
ID NOTES LOCATIONS
-- ------------------------------------ ---------
12 Item on shipment to MEE MEE
13 MATARIAL ON THE WAY TO M44, MTT, MZZ M44
13 MATARIAL ON THE WAY TO M44, MTT, MZZ MTT
13 MATARIAL ON THE WAY TO M44, MTT, MZZ MZZ
14 MVV shipments coming to M99 MVV
14 MVV shipments coming to M99 M99但是,所有示例都与“下一个2重复”部分匹配;如果引入另一个与此不匹配的行,则该值也将被选中:
ID NOTES LOCATIONS
-- ------------------------------------ ---------
...
15 MVA shipments coming to M999 MVA您可以使用子查询和substr检查来消除以下内容:
select id, locations
from (
select id, regexp_substr(notes, '(M[[:alnum:]]{2})([^[:alnum:]]|$)', 1, level, null, 1) as locations
from tbl_notes
connect by level <= regexp_count(notes, '(M[[:alnum:]]{2})([^[:alnum:]]|$)')
and id = prior id
and prior sys_guid() is not null
)
where substr(locations, 2, 1) = substr(locations, 3, 1)这会得到:
ID LOCATIONS
-- ---------
12 MEE
13 M44
13 MTT
13 MZZ
14 MVV
14 M99然而,这可能会在某一时刻中断,因此,当您遇到边缘情况时,可能会反复调整和调整。
https://stackoverflow.com/questions/67907025
复制相似问题