title: SQL - Flagging a most recent entry by date
body: <p>I have the following data.</p>
  
  <pre><code>Clientid  Accountype  Dateapplied
  1         Current     01/01/2018
  1         Savings     03/01/2018
  1         Current     17/01/2018
  2         Current     01/04/2018
  2         Current     15/04/2018
  3         Savings     13/04/2018
  3         Savings     15/04/2018
  3         Current     14/04/2018
  </code></pre>
  
  <p>How do I select the latest dated entry in here per Client where the <code>Accountype = Current</code>. Basically I want to be able to flag to latest entry per client so when I can work out the select I would set a new field as True.
   So results I want to bring back are:</p>
  
  <pre><code>Clientid  Accountype  Dateapplied
  1         Current     17/01/2018
  2         Current     15/04/2018
  3         Current     14/04/2018
  </code></pre>
  
  <p>I've tried also sort of grouping by ClientID then selecting using max but whatever I try I can't pick out the latest one per clientid. Should be simple but racking my brains at it.</p>
  
  <p>So tried things like this but not working. Appreciated if help anyone.</p>
  
  <pre><code>select Dateapplied,Clientid, Accountype  
  from Clienttable t1
  WHERE EXISTS(SELECT 1
       FROM Clienttable t2
  
       WHERE Accountype  = 'Current'
       and  t2.Clientid = t1.X_Clientid
  
       GROUP BY t2.Clientid,
                t2.Dateapplied
       HAVING t1.Dateapplied= MAX(t2.Dateapplied))
  </code></pre>