title: Sum of (actual value - average value across all rows) in SQL
body: <p>Lets say I have a table of statisctics:</p>
  
  <pre><code>Page | Action | Time
  --------------------
  Home | Logon  |  12
  Home | Logon  |  11
  Home | Search |  20
  About| Comment|  10
  </code></pre>
  
  <p>I can write a query to get the average Action time for each page:</p>
  
  <pre><code>select Page, avg(Time) from statistics group by Page
  </code></pre>
  
  <p>But I'd like to be a bit more clever. In order to highlight which pages have the most <em>slow</em> actions on, I'd like to get the sum of (time - average time for all actions on all pages). I could do this in separate queries:</p>
  
  <pre><code>select avg(Time) from statistics
  -&gt; 15
  select Page, sum(Time - 15) from statistics group by Page
  </code></pre>
  
  <p>Question is, is there a way to do this in a single query?</p>
  
  <p>Obviously <code>select Page, sum(Time - avg(Time)) from statistics group by Page</code> doesn't work. Oracle throws "ORA-00937: not a single-group group function".</p>