Domanda

Here is the problem I am trying to solve (Oracle v10g+).
Table 1 Data:

ID        Text_Formula    
1         'FIELD1 = XYZ + ABC'

Table 2 Data:

ID       Formula_Component      Actual_Component    
1              XYZ                  a.br_width    
1              ABC                  b.br_height

Desired Result:

ID       Text_Formula    
1        'FIELD1 = a.br_width + b.br_height'

Table 2 can have any number of rows. I've tried variations using LEAD, LAG, xmlagg in combination with REPLACE, and have not hit upon anything that works. Any pointers would be greatly appreciated!

È stato utile?

Soluzione

I think you should create a function for this operation and use this function in your select query from table1, function should be like this;

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;

Altri suggerimenti

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
)

fiddle

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top