title: Conditional insert into postgres DB from CSV
body: <p>I want to import a CSV into a Postgres table, but I want to be able to decide on the fly whether or not a column should be a 0 or a 1 depending on existing rows in the table.</p>
  
  <p>For example, I'm adding contacts to a table and want to mark them as primary if no contact exists already that is primary, otherwise add them as secondary:</p>
  
  <p>Existing rows: </p>
  
  <pre><code>contact_id | branch_id | primary
  -------------+-----------+--------
  1          | 100       | 1
  2          | 101       | 1
  3          | 101       | 0
  </code></pre>
  
  <p>CSV data,</p>
  
  <pre><code>contact_id | branch_id
  -----------+-----------
  4          | 100
  5          | 101
  6          | 102
  7          | 103
  </code></pre>
  
  <p>Desired result,</p>
  
  <pre><code>contact_id | branch_id | primary
  -----------+-----------+--------
  1          | 100       | 1
  2          | 101       | 1
  3          | 101       | 0
  4          | 100       | 0
  5          | 101       | 0
  6          | 102       | 1
  7          | 103       | 1
  </code></pre>
  
  <p>Notice contact 4 and 5 gets added as secondary as primary contacts already exist for those branches, whereas 6 and 7 get added as primary as no primary contacts exist for those branches.</p>
  
  <p>Is this possible with postgres 9.2?</p>