title: Oracle Query: Get two most recently records for each document
body: <p>I need to get two most recently notes per each document from Oracle database.
  The query is:</p>
  
  <pre><code>select a.doc_id, b.notes, b.post_ts
  from tableA a, tableB b
  where a.doc_id=b.doc_id(+)
  order by doc_id, post_ts desc
  
  doc_id  notes   post_ts
  5743    Test 1  23-Aug-2010 10:25:03
  5743    Test 2  14-Aug-2010 14:11:59
  5743    Test 3  14-Aug-2010 13:56:20
  6813    Test 4  12-Oct-2010 14:34:37
  7543    Test 5  22-Apr-2014 17:02:23
  7543    Test 6  22-Apr-2014 09:46:33
  7543    Test 7  14-Mar-2014 12:17:58
  </code></pre>
  
  <p>The results should be:</p>
  
  <pre><code>doc_id  notes   post_ts
  5743    Test 1  23-Aug-2010 10:25:03
  5743    Test 2  14-Aug-2010 14:11:59
  6813    Test 4  12-Oct-2010 14:34:37
  7543    Test 5  22-Apr-2014 17:02:23
  7543    Test 6  22-Apr-2014 09:46:33
  </code></pre>
  
  <p>Can I just write one sql to handle this case or I have to write a PL/SQL function? I know how to handle it for one document, but couldn’t figure out how to do for multiple documents. </p>