title: Join two tables and then pull Distinct Records
body: <p>I'm writing a VS 2010 program in C# and I've ran into a SELECT statement in SQL that is taking me to long to figure out and could use some help.</p>
  
  <pre><code>Table 1 - mailfiles
  id,fname,lname,etc...
  
  Table 2 - details,
  id,timestamp,page_id,mailfile_id(FK),campaign_id
  </code></pre>
  
  <p>I want to pull the unique/distinct mailfile_id sorted by the most current timestamp and then join them to the mailfiles table to get the rest of my info.</p>
  
  <p>I had something like this,</p>
  
  <pre><code>SELECT mailfiles.id,mailfiles.fname,mailfiles.lname,mailfiles.company2,mailfiles.city,etc...
  FROM mailfiles
  JOIN
  (SELECT DISTINCT(details.mailfile_id)
  FROM details
  GROUP BY details.mailfile_id) as TMP
  ON mailfiles.id = TMP.mailfile_id
  ORDER BY TMP.mailfile_id DESC 
  </code></pre>
  
  <p>Which gets me the distinct/unique records but I don't have access to the details columns, which I want to display the timestamp.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thanks</p>
  
  <p>Nick</p>