title: SQL server select distinct rows using most recent value only
body: <p>I have a table that has the following columns</p>
  
  <ul>
  <li>Id</li>
  <li>ForeignKeyId</li>
  <li>AttributeName</li>
  <li>AttributeValue</li>
  <li>Created</li>
  </ul>
  
  <p>Some of the data may look like this:</p>
  
  <pre><code>1, 1, 'EmailPreference', 'Text', 1/1/2010
  2, 1, 'EmailPreference', 'Html', 1/3/2010
  3, 1, 'EmailPreference', 'Text', 1/10/2010
  4, 2, 'EmailPreference', 'Text', 1/2/2010
  5, 2, 'EmailPreference', 'Html', 1/8/2010
  </code></pre>
  
  <p>I'd like to run a query that pulls the most recent value of the AttributeValue column for each distinct ForeignKeyId andAttributeName, using the Created column to determine the most recent value. Example output would be:</p>
  
  <pre><code>ForeignKeyId AttributeName    AttributeValue Created
  -------------------------------------------------------
  1           'EmailPreference' 'Text'         1/10/2010
  2           'EmailPreference' 'Html'         1/8/2010
  </code></pre>
  
  <p>How can I do this using SQL Server 2005?</p>