title: Find records based on id, compare date and update the one with the oldest date
body: <p>I have a table, let's call it the UserTable.</p>
  
  <p><img src="https://i.stack.imgur.com/Oeqac.png" alt="enter image description here"></p>
  
  <p>What I would like to do is to:</p>
  
  <ul>
  <li>Find the duplicate UserID</li>
  <li>Compare the StartDate and update the record with the oldest StartDate by filling in the EndDate (current date minus 1)</li>
  <li>Do this for all duplicate records in the table</li>
  </ul>
  
  <p>End result should look like:</p>
  
  <p><img src="https://i.stack.imgur.com/MTdJi.png" alt="enter image description here"></p>
  
  <p>I got it down to: </p>
  
  <pre><code>UPDATE UserTable
      SET EndDate = GETDATE()-1
  WHERE EndDate IS NULL AND StartDate &lt; (GETDATE()) AND
  (UserID) in (
    select UserID 
    from   UserTable
    group  by UserID
    having count(*) &gt; 1)
  </code></pre>
  
  <p>Only thing I can't figure out is how to update only the one with the oldest StartDate. Based on the code above, all records that are found are updated.</p>