我寧願通過PL/SQL的方式,但在你的標籤只有 'SQL',所以我做了這個怪物:
with t as (
select 123 as id, 3 as pos, 'q' as new_char from dual
union all
select 123 as id, 6 as pos, 'z' as new_char from dual
union all
select 123 as id, 9 as pos, '1' as new_char from dual
union all
select 456 as id, 1 as pos, 'A' as new_char from dual
union all
select 456 as id, 4 as pos, 'Z' as new_char from dual
),
t1 as (
select 123 as id, 'Becirovic' as str from dual
union all
select 456 as id, 'Test' as str from dual
)
select listagg(out_text) within group (order by pos)
from(
select id, pos, new_char, str, prev, substr(str,prev,pos-prev)||new_char as out_text
from(
select id, pos, new_char, str, nvl(lag(pos) over (partition by id order by pos)+1,1) as prev
from (
select t.id, pos, new_char, str
from t, t1
where t.id = t1.id
) q
) a
) w
group by id
結果:
Beqirzvi1
AesZ
我認爲在這樣做,你就需要讓你的串入一個記錄所有應用替換規則(2任何機會你上面的例子)。我沒有辦法做到這一點。我們可以問問你是如何結束這個問題的? –
我認爲最好的方法是一個函數。你可以在環境中創建功能嗎? – pOrinG
@TimBiegeleisen我會加入表格,添加'group by'並添加'LISTAGG()'函數(對於MySQL來說也是GROUP_CONCAT)。需要訂購小組成員並剪切字符串以獲得多個部分。應該工作,但查詢會很棘手。更容易引入功能。 – StanislavL