title: How do I get the record ID of the record with the min date for each foreign key?
body: <p>I have the following table</p>
  
  <pre><code>recordID               createdDate                         ForeignKeyID
  00QA000000PtFXaMAN     2012-01-03 13:23:36.000             001A000000ngM21IAE
  00QA000000OS2QiMAL     2011-12-15 12:03:02.000             001A000000ngM21IAE
  .
  .
  .
  .
  </code></pre>
  
  <p>I am trying to get the recordID for foreignKeyID where createdDAte is the min(createdDate) for foreignKeyID</p>
  
  <p>if recordID is identity int I can get that by doing the following query</p>
  
  <pre><code>Select min(recordId),ForeignkeyID
  from table 
  group by ForeignKeyId
  </code></pre>
  
  <p>I originaly thought that I can create temp table with the following query and then joining it to table on minDate and foreignKeyID but then I found out that there are multiple records for foreignKeyId that has the same exact createdDate.</p>
  
  <pre><code>Select min(createdDate) as minDate,ForeignKeyID
  from table
  group by ForeignKeyId
  </code></pre>
  
  <p>I'm open with using temp table or subquery or anything really. Thanks.</p>