title: Chaining SQL queries
body: <p>Right now I'm running</p>
  
  <pre><code>SELECT formula('FOO') FROM table1
  WHERE table1.foo = 'FOO' &amp;&amp; table1.bar = 'BAR';
  </code></pre>
  
  <p>but I would like to run this not on the constant <code>FOO</code> but on each value from the query</p>
  
  <pre><code>SELECT foos FROM table2
  WHERE table2.bar = 'BAR';
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p><strong>Edit:</strong> Important change: added FOO to the arguments of function.</p>
  
  <p>Illustration:</p>
  
  <pre><code>SELECT foo FROM table1 WHERE foo = 'FOO' &amp;&amp; table1.bar = 'BAR';
  </code></pre>
  
  <p>gives a column with FOOa, FOOb, FOOc.</p>
  
  <pre><code>SELECT formula('FOO') FROM table1
  WHERE table1.foo = 'FOO' &amp;&amp; table1.bar = 'BAR';
  </code></pre>
  
  <p>gives a single entry, say sum(FOO) (actually much more complicated, but it uses aggregates at some point to combine the results).</p>
  
  <p>I want some query which gives a column with sum(FOO1), sum(FOO2), ... where each FOOn is computed in like manner to FOO.  But I'd like to do this with one query rather than <em>n</em> queries (because <em>n</em> may be large and in any case the particular values of FOOn vary from case to case).</p>