title: SQL: GROUP BY records and then get last record from each group?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/438938/sql-server-only-last-entry-in-group-by">SQL Server: Only last entry in GROUP BY</a>  </p>
  </blockquote>
  
  
  
  <p>I have a table like this:</p>
  
  <pre><code>id| name  | attendence
  1 | Naveed| 1
  2 | Naveed| 1
  3 | Adil  | 1
  4 | Adil  | 1
  </code></pre>
  
  <p>I use following query:</p>
  
  <pre><code>SELECT * FROM `test` WHERE `attendence`=1 GROUP BY name
  </code></pre>
  
  <p>The result with above query:</p>
  
  <pre><code>id| name   | attendence
  3 | Adil   | 1
  1 | Naveed | 1
  </code></pre>
  
  <h1>Question:</h1>
  
  <p>Above result group rows by name but show first row from each group. I want to select last row(by id) from each group.</p>
  
  <p>For example:</p>
  
  <pre><code>id| name   | attendence
  2 | Naveed | 1
  4 | Adil   | 1
  </code></pre>
  
  <p>How to write query for above result.</p>
  
  <p>Thanks</p>