title: Mysql get difference between two rows based on their timestamp
body: <p>so I have this data below</p>
  
  <pre><code># AgreementID, AssetID, EventDate,               OdometerReading
  '247',         '27',    '2017-07-21 15:02:22',   '30'
  '247',         '74',    '2017-07-21 14:58:17',   '78'
  '247',         '27',    '2017-07-21 14:58:15',   '12'
  '247',         '74',    '2017-07-21 14:57:07',   '70'
  '247',         '74',    '2017-07-21 14:46:46',   '63'
  '247',         '74',    '2017-07-21 14:45:19',   '60'
  '247',         '74',    '2017-07-21 14:38:58',   '59'
  '247',         '74',    '2017-07-21 14:37:30',   '5'
  '245',         '40',    '2017-07-21 11:43:23',   '12'
  '245',         '40',    '2017-07-21 11:11:45',   '10'
  </code></pre>
  
  <p>What I'm trying to do is get the difference between the odometerReading of the <code>MAX(EventDate)</code> and the <code>MIN(EventDate)</code> of each assetID for each agreementID.</p>
  
  <p>The result should look like below:</p>
  
  <pre><code># AgreementID, AssetID, OdometerDifference
  '247',         '27',    18
  '247',         '74',    73
  '245',         '40',    2
  </code></pre>
  
  <p>So far i got</p>
  
  <pre><code> select MAX(T.OdometerReading) - MIN(T.OdometerReading)
   from T
   group by T.agreementID, T.AssetID
   order by T.AgreementID desc
  </code></pre>
  
  <p>It's doing what i want it to do and returning the correct results, but i want the result to be based on the max and min eventdate.</p>