title: Calculate weighted average in single query
body: <p>Example data:</p>
  
  <pre><code>table A
  part    rating   numReviews
  A308    100      7
  A308    98       89
  </code></pre>
  
  <p>I'm trying to get the average rating for the above data.</p>
  
  <p>What it needs to be is the sum of <code>rating</code>*<code>numReviews</code> for each line divided by the total <code>numReviews</code></p>
  
  <p>This is what I'm trying but it's giving incorrect result (49.07, should be 98.15):</p>
  
  <pre><code>select part, 
  cast((AVG(rating*numReviews)/sum(numReviews)) as decimal(8,2)) as rating_average
  from A group by part order by part
  </code></pre>
  
  <p>Can this be done in a single query? I'm using SQL Server</p>