title: Subquery to return the latest entry for each parent ID
body: <p>I have a parent table with entries for documents and I have a history table which logs an audit entry every time a user accesses one of the documents.</p>
  
  <p>I'm writing a search query to return a list of documents (filtered by various criteria) with the latest user id to access each document returned in the result set.</p>
  
  <p>Thus for </p>
  
  <pre><code>
      DOCUMENTS
      ID | NAME
      1  | Document 1
      2  | Document 2
      3  | Document 3
      4  | Document 4
      5  | Document 5
  
      HISTORY
      DOC_ID | USER_ID | TIMESTAMP
      1      | 12345   | TODAY
      1      | 11111   | IN THE PAST
      1      | 11111   | IN THE PAST
      1      | 12345   | IN THE PAST
      2      | 11111   | TODAY
      2      | 12345   | IN THE PAST
      3      | 12345   | IN THE PAST
  </code></pre>
  
  <p>I'd be looking to get a return from my search like</p>
  
  <pre><code>
      ID | NAME       | LAST_USER_ID
      1  | Document 1 | 12345
      2  | Document 2 | 11111
      3  | Document 3 | 12345
      4  | Document 4 | 
      5  | Document 5 | 
  </code></pre>
  
  <p>Can I easily do this with one SQL query and a join between the two tables?</p>