title: Oracle SQL: Conditionally update a table based on comparison with a SQL statement
body: <p>I have a table (TABLE1) which has 4 columns:</p>
  
  <p><strong>UID | PID | VALUE1 | VALUE2</strong></p>
  
  <p>I have another SQL statement (STMT) which returns 3 columns from some other tables:</p>
  
  <p><strong>UID | PID | VALUE1</strong></p>
  
  <p>Now here are the condition of how I want to update TABLE1:</p>
  
  <p>First, compare UID and PID pair from STMT with those in TABLE1,</p>
  
  <ol>
  <li><p>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)</p></li>
  <li><p>for existing UID and PID pair in both STMT and TABLE1, do:</p>
  
  <p>a. if TABLE1.VALUE2 > 0, update TABLE1.VALUE1 = STMT.VALUE1</p>
  
  <p>b. if TABLE1.VALUE2 = 0 and if TABLE1.VALUE1 != STMT.VALUE1, update TABLE1.VALUE1 = STMT.VALUE1, otherwise do nothing.</p></li>
  </ol>
  
  <p>I'm having trouble coming up with a solution for condition 2. Any help is appreciated!</p>