Hello folks & fellow developers,
I am working on an Oracle DB and have some XML data stored in blob format, The XML has:
a) a formula (eg. %1 - %2
, or %2||'-'||%1
)
b) a delimited list of variables that fit into the formula (eg. SALE_Q1| SALE_Q2
or YEAR| MONTH
)
I have managed to pull this data into 2 different columns (can be merged into 1 column if needed too) and what I need to do is get the output column as a superimposition of the variables onto the placeholders.
(eg. SALE_Q1 - SALE_Q2 , or MONTH||'-'||YEAR
)
there are also a few caveats to this like:
- I don't know how many variables will each formula have and,
- the variables will not always be used in the formula in the same order as the delimited list (see eg.2)
we can consider the data coming from a query like: SELECT formula || ', ' || columns_used FROM data_table;
for the input string
and the output i am currently getting is like:
%1||'-'||%2||%3, SITE_NO| SITE_NAME| COUNTRY
0.1 * %1, WIND_RES
%1, TOTAL
CASE WHEN LENGTH(%1) < 8 THEN NULL ELSE TO_DATE(%1,'yyyymmdd')END, MIN_DATE
%1(+)=%3 and %2(+)=%4, ABC| LMN| PQR| XYZ
I am pretty new to SQL and this requirement is going over my head, any help will be highly appreciated. I need an SQL solution as a PL/SQL solution is not feasible in my requirement (this script is going to pull data from one DB and feed it to another repo on a regular basis)
I have seen some articles on XML table, model, or recursive regexp but I am not sure how I can use these. I have also seen this question on StackOverflow but my requirement is a little different from that and is a bit trickier as well. getting the formula string and the variables into the same string for processing like that question is also a plausible route. Any solution that can be written in an SQL query will be very helpful.
some more examples for your consideration:
"%1||'-'||%2||%3, SITE_NO| SITE_NAME| COUNTRY" => "SITE_NO||'-'||SITE_NAME||COUNTRY"
"0.1 * %1, WIND_RES" => "0.1 * WIND_RES"
"%1, TOTAL" => "TOTAL"
"CASE WHEN LENGTH(%1) < 8 THEN NULL ELSE TO_DATE(%1,'yyyymmdd')END, MIN_DATE" => "CASE WHEN LENGTH(MIN_DATE) < 8 THEN NULL ELSE TO_DATE(MIN_DATE,'yyyymmdd')END"
"%1(+)=%3 and %2(+)=%4, ABC| LMN| PQR| XYZ" => "ABC(+)=PQR and LMN(+)=XYZ"