title: sql server sum column to find out who reached a value first
body: <p>I asked a question similar to this here:</p>
  
  <p><a href="https://stackoverflow.com/questions/25746457/sql-sum-a-column-and-also-return-the-last-time-stamp">sql sum a column and also return the last time stamp</a></p>
  
  <p>It turns out my use case was incorrect though so I need to make an adjustment to my question.</p>
  
  <p>I've got a table in SQL Server with several columns.  The relevant ones are:</p>
  
  <pre><code>name
  distance
  create_date
  </code></pre>
  
  <p>I have many people identified by name, and every few days they travel a certain distance.  For example:</p>
  
  <pre><code>name     distance     create_date
  john     15           09/12/2014
  john     20           09/22/2014
  alex     10           08/15/2014
  alex     12           09/05/2014
  alex     20           09/12/2014
  john     8            09/30/2014
  alex     30           09/14/2014
  mike     12           09/10/2014
  </code></pre>
  
  <p>The query I need has 3 parameters:</p>
  
  <pre><code>@start_date
  @end_date
  @count
  </code></pre>
  
  <p>I need a query that between the two dates, returns for each person the distance traveled.  The trick though is that for each person I should sum to an amount just past the amount indicated in @count and return the date this was achieved, or if the person did not pass the @count then return the sum and last date of entry.  So for example, if I use the parameters:</p>
  
  <pre><code>@start_date=08/01/2014
  @end_date=09/25/2014
  @count=35
  </code></pre>
  
  <p>I would expect the following:</p>
  
  <pre><code>name     distance     create_date
  alex     42           09/12/2014
  john     35           09/22/2014
  mike     12           09/10/2014
  </code></pre>
  
  <p>Does someone have an idea for this?</p>
  
  <p>Thank you!</p>