Oracle SQL: Conditionally update a table based on comparison with a SQL statement -
i have table (table1) has 4 columns:
uid | pid | value1 | value2
i have sql statement (stmt) returns 3 columns other tables:
uid | pid | value1
now here condition of how want update table1:
first, compare uid , pid pair stmt in table1,
for new uid , pid pair not in table1, insert new row stmt table1, set table1.value2 = 0 (i've done one)
for existing uid , pid pair in both stmt , table1, do:
a. if table1.value2 > 0, update table1.value1 = stmt.value1
b. if table1.value2 = 0 , if table1.value1 != stmt.value1, update table1.value1 = stmt.value1, otherwise nothing.
i'm having trouble coming solution condition 2. appreciated!
update ( select table1.uid ,table1.pid ,table1.value1 table1_value1 ,table1.value2 table1_value2 ,stmt.value1 stmt_value1 table1 inner join (select ? uid ,? pid ,? value1 whatever) stmt on stmt.uid = table1.uid , stmt.pid = table1.pid ) x set x.table1_value1 = case when (x.table1_value2 > 0) or (x.table1_value2 = 0 , x.table1_value1 <> x.stmt_value1) x.stmt_value1 else x.table1_value1 end
Comments
Post a Comment