title: T-SQL - SELECT by nearest date and GROUPED BY ID
body: <p>From the data below I need to select the record nearest to a specified date for each Linked ID using SQL Server 2005:</p>
  
  <blockquote>
  <pre><code>ID     Date      Linked ID
  ...........................
  1    2010-09-02     25
  2    2010-09-01     25
  3    2010-09-08     39
  4    2010-09-09     39
  5    2010-09-10     39
  6    2010-09-10     34
  7    2010-09-29     34
  8    2010-10-01     37
  9    2010-10-02     36
  10   2010-10-03     36
  </code></pre>
  </blockquote>
  
  <p>So selecting them using 01/10/2010 should return:</p>
  
  <blockquote>
  <pre><code>1    2010-09-02     25 
  5    2010-09-10     39
  7    2010-09-29     34 
  8    2010-10-01     37
  9    2010-10-02     36
  </code></pre>
  </blockquote>
  
  <p>I know this must be possible, but can't seem to get my head round it (must be too near the end of the day :P) If anyone can help or give me a gentle shove in the right direction it would be greatly appreciated! </p>
  
  <p><strong>EDIT:</strong> Also I have come across this sql to get the closest date:</p>
  
  <pre><code>abs(DATEDIFF(minute, Date_Column, '2010/10/01'))
  </code></pre>
  
  <p>but couldn't figure out how to incorporate into the query properly...</p>
  
  <p>Thanks</p>