title: MySQL weighted average in a single query
body: <p>I have a MySQL table which looks like this:</p>
  
  <pre><code>id  load_transit    load_standby    hours_transit   hours_standby
  1   40              20              8               4
  2   30              15              10              10
  3   50              10              3               9
  </code></pre>
  
  <p>I need to do the following calculations:</p>
  
  <p>(intermediate calculations)</p>
  
  <pre><code>hours_transit_total = 8+10+3 = 21
  hours_standby_total = 4+10+9 = 23
  </code></pre>
  
  <p>(desired result)</p>
  
  <pre><code>load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
  load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
  </code></pre>
  
  <p>Is it possible to do this in a single query? What would the best design be?</p>