title: SQL - Calculate difference between two records
body: <p>I have a table that logs logins for each user and the current points total of that user at each login.</p>
  
  <p>What I'd like to do is get their first login for each day and calculate the points difference between their previous days' points total.</p>
  
  <p>To make things clearer, I have written a statement that gets each user's min login per day as follows:</p>
  
  <pre><code>SELECT loginLog.username, A.logInDate, loginLog.pointsTotal
  FROM loginLog
  JOIN
      (SELECT MIN(logID) AS logID, username, CAST(logInTime AS DATE) AS logInDate
      FROM loginLog
      GROUP BY username, CAST(logInTime AS DATE)) A
  ON loginLog.logID = A.logID
  ORDER BY username, logInDate DESC
  </code></pre>
  
  <p>Which produces the following results set:</p>
  
  <pre><code>username    logInDate       pointsTotal
  user1       2015-10-28      82685
  user1       2015-10-27      51330
  user1       2015-10-26      7810
  user2       2015-10-28      221223
  user2       2015-10-27      207234
  user2       2015-10-26      178781
  user3       2015-10-28      616120
  user3       2015-10-27      598715
  user3       2015-10-26      591289
  user4       2015-10-28      187654
  user4       2015-10-27      198378
  user4       2015-10-26      115014
  user5       2015-10-28      248138
  user5       2015-10-27      224729
  user5       2015-10-26      216229
  user6       2015-10-28      68546
  user6       2015-10-28      24139
  user6       2015-10-27      33171
  user6       2015-10-27      6459
  user6       2015-10-26      6391
  </code></pre>
  
  <p>So for example, on the first record I'd like to add a column dailyGrowth that would calculate 82685 - 51330, on the second record if would calculate 51330 - 7810 etc etc.</p>
  
  <p>Is this possible?</p>