title: PostgreSQL Conditional selects
body: <p>I have written a recursive function and depending on the output I need to select different fields. My question is now, how can I do this multiple times without having to call the function more then once? What I'm doing right now is just using the <code>CASE WHEN...</code> condition and checking every time what the functions return. (This is only a pseudo code and doesn't do anything real, it's just for understanding)</p>
  
  <pre><code>SELECT
  id,
  (CASE WHEN (function(id) &gt; 0)
      THEN field1
      ELSE field2
  END) as value1,
  (CASE WHEN (function(id) &gt; 0)
      THEN field3
      ELSE field4
  END) as value2,
  (CASE WHEN (function(id) &gt; 0)
      THEN field5
      ELSE field6
  END) as value3
  FROM table1
  ...
  </code></pre>
  
  <p>How can I optimize this query and call the function only once?
  Thanks in advance!</p>