title: Get last item in a table - SQL
body: <p>I have a History Table in SQL Server that basically tracks an item through a process. The item has some fixed fields that don't change throughout the process, but has a few other fields including status and Id which increment as the steps of the process increase.</p>
  
  <p>Basically I want to retrieve the last step for each item given a Batch Reference. So if I do a </p>
  
  <pre><code>Select * from HistoryTable where BatchRef = @BatchRef
  </code></pre>
  
  <p>It will return all the steps for all the items in the batch - eg</p>
  
  <pre>
  <b>Id      Status  BatchRef        ItemCount</b>
  1       1       Batch001        100
  1       2       Batch001        110
  2       1       Batch001        60
  2       2       Batch001        100
  </pre>
  
  <p>But what I really want is:</p>
  
  <pre>
  <b>Id      Status  BatchRef        ItemCount</b>
  1       2       Batch001        110
  2       2       Batch001        100
  </pre>
  
  <p>Edit: Appologies - can't seem to get the TABLE tags to work with Markdown - followed the help to the letter, and looks fine in the preview</p>