I have a table (TABLE1) which has 4 columns:
UID | PID | VALUE1 | VALUE2
I have another SQL statement (STMT) which returns 3 columns from some other tables:
UID | PID | VALUE1
Now here are the condition of how I want to update TABLE1:
First, compare UID and PID pair from STMT with those in TABLE1,
-
for all new UID and PID pair not in TABLE1, insert new row from STMT into TABLE1, set TABLE1.VALUE2 = 0 (I’ve already done this one)
-
for existing UID and PID pair in both STMT and TABLE1, do:
a. if TABLE1.VALUE2 > 0, update TABLE1.VALUE1 = STMT.VALUE1
b. if TABLE1.VALUE2 = 0 and if TABLE1.VALUE1 != STMT.VALUE1, update TABLE1.VALUE1 = STMT.VALUE1, otherwise do nothing.
I’m having trouble coming up with a solution for condition 2. Any help is appreciated!
1 Answer