Oracle SQL Merge Failure. Avoid Cartesian Product

Viewed 29

I am trying to update the customer OLD_VALUE with NEW_VALUE. I need some suggestions in handling the update.

TEMP_TABLE has the below data

enter image description here

I am using the below MERGE statement for updating these values into the TARGET_TABLE

merge into target_table tt
using (
          select distinct order_id, old_value, new_value
            from test_table 
       )
 test on (test.order_id= tt.order_id)
 when matched then 
 update set tt.old_value= test.new_value;

The above MERGE will fail, because ORDER_ID = 4,5,6 has multiple NEW_VALUE. I tried using RANK function on the temp table to achieve below output, so the MERGE will work. But couldn't get the below result set. Need help on achieving the below result set in TEMP_TABLE

enter image description here

0 Answers
Related