【发布时间】:2018-12-29 06:08:01
【问题描述】:
我有一张表,其中一列包含一个字符串,其中的项目用分号(;)分隔
我想根据字符串的模式有选择地将数据传输到新表中。
例如,它可能看起来像
16;;14;30;24;11;13;14;14;10;13;18;15;18;24;13/18;11;;23;12;;19;10;;11 ;26;;;42;26;38/39;12;;;;;;;11;;;;;;;;;;;;;;;
或
11;;11;11;11;11;11;11;11;11;11;11;11;11;11;11;11;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;
我不关心分号之间的内容,但我关心哪些位置包含项目。例如,如果我只希望第 1、3、4 个位置包含项目,我将允许以下...
32;;14;18/12;;;;;;;;;或 32;;14;18/12;;;;55;;;;11;;;;;;;
下面这个不行,因为第三个位置没有任何价值。
32;;;18/12;;;;;;;;;;
如果正则表达式适用于此,那么我可以使用merge into 将所需的记录移动到目标表中。如果无法做到这一点,我将不得不在 Java 中处理每条记录,并有选择地将记录插入到新表中。
源表:
id | StringValue | count
目标表:
id | StringValue | count
我想到的sql:
merge into you_target_table tt
using ( select StringValue, count
from source_table where REGEXP_LIKE ( StringValue, 'some pattern')
) st
on ( st.StringValue = tt.StringValue and st.count=tt.count )
when not matched then
insert (id, StringValue , count)
values (someseq.nextval, st.value1, st.count)
when matched then
update
set tt.count = tt.count + st.count;
另外我确定源表中的所有StringValue都是唯一的,所以when matched then之后的内容并不重要,但由于语法,我想我必须有一些东西。
【问题讨论】: