title: Nested aggregate functions with grouping in postgresql
body: <p>I'm trying to get an average of sums using nested aggregate functions and grouping. What I would want to do is:</p>
  
  <pre><code>SELECT AVG(SUM(x) GROUP BY y) WHERE ... GROUP BY ...;
  </code></pre>
  
  <p>That is, for each row returned, I want one of the fields to be an average of sums, where each sum is over the rows where y is the same.</p>
  
  <p>I would like to avoid subselects if possible.</p>