title: Include data in a table looking in every insert if there is a match with the table values
body: <p>I need to insert data from one table into another, but this insert must look into the table which receives data to determine if there is a match or not, and if it is, don't insert new data.</p>
  
  <p>So, i have the next tables <em>(NODE_ID refers to values in NODE1 and NODE2, think about lines with two nodes everyone)</em>:</p>
  
  <pre><code>Table A:
  | ARC | NODE1 | NODE2 | STATE |
  | x   |  1    |  2    |   A   |
  | y   |  2    |  3    |   A   |
  | z   |  3    |  4    |   B   |
  
  Table B:
  | NODE_ID| VALUE |
  |    1   |   N   |
  |    2   |   N   |
  |    3   |   N   |
  |    4   |   N   |
  </code></pre>
  
  <p>And want the next result, that relates NODE_ID with ARCS and write in the result table the value of STATE from ARCS table, <strong>only one result for each NODE</strong>, because if not, i would have more than one row for the same NODE:</p>
  
  <pre><code>Table C result:
  | NODE_ID| STATE |
  |    1   |   A   |
  |    2   |   A   |
  |    3   |A(or B)|
  </code></pre>
  
  <p>I tried to do this with CASE statement with EXISTS, IF , and NVL2() and so on in the select but have no result at this time.</p>
  
  <p>Any idea about how could i write this query?</p>
  
  <p>Thank you very much for your help</p>
  
  <hr>
  
  <p>Ok guys, i edit my message to explain how i did it finally, i've also changed a little bit my first message to make it more clear to undestand because we had problems with that. </p>
  
  <p>So finally i used this query, that @mathguy introduced to me:</p>
  
  <pre><code>merge into Table_C c
     using (select distinct b.NODE_ID as nodes, a.STATE
            from Table_A a, Table_B b
            where (b.NODE_ID=a.NODE1 or b.NODE_ID=a.NODE2) s
     on (s.nodes=c.NODE_ID)
  
  when not matched then
    insert (NODE_ID, STATE)
    values (s.nodes, s.STATE)
  </code></pre>
  
  <p>That's all</p>