title: Reuse function call inside select statement
body: <p>Simplified example of the problem:  </p>
  
  <pre><code>select p.id,
         p.name,
         -- other columns from joined tables
         decode(get_complicated_number(p.id), null, null, "The number is: " || get_complicated_number(p.id)))
  from some_table p
  -- join other tables and WHERE clause
  </code></pre>
  
  <p>It includes <code>get_complicated_number</code> call which queries multiple tables. I wasn't able to write it as a JOIN statement that would be as fast and as easy to maintain as a separate function so far.  </p>
  
  <p>Currently the function is called twice in case its return value is not NULL. </p>
  
  <p>In reality I have an XML generation package that gets the data with a select:  </p>
  
  <pre><code>select distinct  xmlAgg
    (
       xmlelement
       (
        "TestElement", 
          xmlelement("Id", p.id),
          xmlelement("Name", p.name),
          -- other elements from joined tables
          decode(get_complicated_number(p.id), null, null, xmlelement("ComplicatedNum", get_complicated_number(p.id)))
        )
     )
  from some_table p
  -- join other tables and WHERE clause 
  </code></pre>
  
  <p>Is there a way to make it only one call and still avoid creating an empty element on NULL?  </p>