title: SQL weighted average
body: <p>Have a table like below.</p>
  
  <pre><code>make   | model | engine | cars_checked | avg_mileage
  ---------------------------------------|--------
  suzuki | sx4   | petrol | 11           | 12
  suzuki | sx4   | diesel | 150          | 16
  suzuki | swift | petrol | 140          | 15
  suzuki | swift | diesel | 18           | 19
  toyota | prius | petrol | 16           | 17
  toyota | prius | hybrid | 250          | 24
  </code></pre>
  
  <p>The output desired is </p>
  
  <ol>
  <li>average mileage by engine (petrol, diesel)</li>
  <li>average mileage by make </li>
  <li>average mileage by model</li>
  </ol>
  
  <p>Cannot do a simple <code>group by</code> as the weight-age in terms of the number of samples for each record (<code>cars_checked</code>) need to considered so as to avoid the average of averages problem.</p>
  
  <p>What is the right way to achieve it ? is there a way to consider the number of samples to do a weighted average in <code>group by</code> ?</p>
  
  <p><strong>update</strong> - output format added for #1 above as example</p>
  
  <pre><code>engine   | mileage_by_engine
  --------------------------
  petrol   | xx.z
  diesel   | yy.z
  </code></pre>