title: SQL use value from CASE EXISTS
body: <p>Postgres 9.5.3</p>
  
  <p>A fragment from a bigger query which updates a JSONB field in a different table (I don't think the JSONB stuff has any relevance to the question however):</p>
  
  <pre><code>CASE WHEN EXISTS(SELECT role FROM people WHERE result_id = r2.id AND admin = 't')
  THEN (elem || jsonb_build_object('admin', true, 'role', role))
  ELSE (elem - 'admin' - 'role')
  END
  </code></pre>
  
  <p>Essentially if the record exists, I want to be able to use the value of the <code>role</code> field from that record.</p>
  
  <p>As it currently is written, we get an error <code>column "role" does not exist</code></p>