Oracle递归选择
以下是我试图解决的Oracle v10g+问题。 表1数据:Oracle递归选择,oracle,select,recursive-query,Oracle,Select,Recursive Query,以下是我试图解决的Oracle v10g+问题。 表1数据: ID Text_Formula 1 'FIELD1 = XYZ + ABC' 表2数据: ID Formula_Component Actual_Component 1 XYZ a.br_width 1 ABC b.br_height 预
ID Text_Formula
1 'FIELD1 = XYZ + ABC'
表2数据:
ID Formula_Component Actual_Component
1 XYZ a.br_width
1 ABC b.br_height
预期结果:
表2可以有任意数量的行。我尝试过使用LEAD、LAG、xmlagg和REPLACE的变体,但没有发现任何有效的方法。任何指点都将不胜感激 我认为您应该为此操作创建一个函数,并在表1的select查询中使用此函数,函数应该如下所示
create or replace function transform_data(p_input in varchar2) return varchar2
is
v_result varchar2(2000);
v_col_value varchar2(200);
begin
v_result := p_input;
for rec in (select * from table2)
loop
if instr(v_result, rec.Formula_Component) > 0 then
v_result := replace(v_result, rec.Formula_Component, rec.Actual_Component);
end if;
end loop;
return v_result;
end;
有关表格格式的简单方法,请参阅。谢谢-这将起到作用。虽然我更喜欢sql,但我还是选择了函数。鉴于数据量较小,函数不应该是问题。再次感谢!
create or replace function transform_data(p_input in varchar2) return varchar2
is
v_result varchar2(2000);
v_col_value varchar2(200);
begin
v_result := p_input;
for rec in (select * from table2)
loop
if instr(v_result, rec.Formula_Component) > 0 then
v_result := replace(v_result, rec.Formula_Component, rec.Actual_Component);
end if;
end loop;
return v_result;
end;
select id, replace(formula, chr(0)) as text_formula from (
select
id, rn, regexp_replace(text_formula, '(\w+)', chr(0)||'\1'||chr(0)) formula
from t1 natural join (select id, count(0) rn from t2 group by id)
) model
reference dic on (
select
id,
chr(0)||formula_component||chr(0) as term,
actual_component as value,
row_number() over (partition by id order by null) as rn
from t2
) dimension by (id, rn) measures (term, value)
partition by (id) dimension by (1 x) measures (formula, rn)
rules iterate (1000000) until (rn[1] <= 0) (
formula[1] = replace(formula[1], term[cv(id), rn[1]], value[cv(id), rn[1]]),
rn[1] = rn[1] - 1
)