title: Find average of last X records for every person
body: <p>I have a table which stores every score from every player. I want to take out the last 3 (or less) and display the average.</p>
  <p>Example:</p>
  <pre><code>TABLE log
  time  p_id  score
  ----  ----  -----
  12:00 0     10
  14:23 0     25
  14:25 1     30
  16:27 0     70
  17:00 0     80
  </code></pre>
  <p>Now I want to find the average of all players' last 3 entries...</p>
  <p>Result:</p>
  <pre><code>p_id  avg
  ----  ---
  0     58
  1     30
  </code></pre>
  <p>How should I proceed?</p>
  <p><em>Edit in 2020</em>: I was suddenly reminded of this question because of a notification of a new answer, and I see that I also have a neglected answer since back in 2010 that I should've given some feedback on.</p>
  <p>While I'm not in dire need of a solution here (I believe I ended up with a loop, sending queries once for every player. It worked fine in my case with 50 players total), here is a sqlfiddle I set up to test the different solutions to be able to give feedback. <a href="http://sqlfiddle.com/#!9/4d7ca5b" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/4d7ca5b</a></p>