title: SQL Select last occurrence of unique attributes
body: <p>I have a table that looks like this:</p>
  
  <pre><code>   Date   | AttributeId | Score |
  4/4/2013         1         0.64      
  4/6/2013         2         0.35
  4/6/2013         1         0.86
  4/4/2013         5         0.34
  4/4/2013         4         0.23
  4/7/2013         3         0.54
  4/8/2013         1         0.66
  </code></pre>
  
  <p>Can I construct a query in MS SQL so that I get only the last occurrence of a particular
  AttributeID. From example above, the query would return this:</p>
  
  <pre><code>   Date   | AttributeId | Score |     
  4/6/2013         2         0.35
  4/4/2013         5         0.34
  4/4/2013         4         0.23
  4/7/2013         3         0.54
  4/8/2013         1         0.66
  </code></pre>
  
  <p>If it helps, I know that there are only 5 AttributeIDs (1-5) and that they will not change.  </p>
  
  <p>For the moment I'm querying the last 15 records (and hoping that those records contain all of my 5 attributes), and then on the application level, extracting the latest scores for all 5 attributes.<br>
  Is there a better solution?</p>
  
  <p>EDIT:<br>
  Also, if I have <code>Date</code>, <code>UserId</code>, <code>AttributeId</code> and <code>Score</code>, can I somehow group my results by <code>UserId</code>?<br>
  When I tried your answers, I saw that I only get the latest results, no matter which <code>UserId</code>.<br>
  I'm sorry guys, I should have mentioned <code>UserId</code> sooner  </p>
  
  <p>EDIT 2:
  I need to get the latest scores by attribute for every user and average their scores over attributes.
  The sample looks like this:  </p>
  
  <pre><code>   Date   |  UserId  | AttributeId | Score |
  4/4/2013        1           1         0.64      
  4/6/2013        1           1         0.35
  4/6/2013        2           1         0.86
  4/4/2013        1           3         0.34
  4/4/2013        2           3         0.23
  4/7/2013        2           1         0.54
  4/8/2013        1           5         0.69
  4/4/2013        2           4         0.27
  4/7/2013        2           2         0.54
  4/9/2013        1           4         0.66
  4/9/2013        2           2         0.58
  4/10/2013       1           4         0.66
  4/9/2013        1           2         0.33
  4/11/2013       2           5         0.10
  </code></pre>
  
  <p>first result looks like this:</p>
  
  <pre><code>   Date   |  UserId  | AttributeId | Score |
  4/6/2013        1           1         0.64      
  4/9/2013        1           2         0.33
  4/4/2013        1           3         0.34
  4/10/2013       1           4         0.66
  4/8/2013        1           5         0.69
  4/7/2013        2           1         0.86
  4/9/2013        2           2         0.58
  4/4/2013        2           3         0.23
  4/4/2013        2           4         0.27
  4/11/2013       2           5         0.10
  </code></pre>
  
  <p>And the last result after averaging scores by <code>UserId</code>:</p>
  
  <pre><code>UserId | AverageScore |
     1           0.532     
     2           0.408 
  </code></pre>