I have a table of data that tracks the mileage of my beloved 2005 Toyota Prius.
+----+------------+---------+---------+-------+
| id | date      | mileage | gallons | cost |
+----+------------+---------+---------+-------+
|Â 1 | 2005-02-14 |Â Â Â Â 280 |Â Â 8.615 | 16.03 |
|Â 2 | 2005-02-27 |Â Â Â Â 480 |Â Â 4.775 |Â 8.59 |
|Â 3 | 2005-03-19 |Â Â Â Â 713 |Â Â 7.213 | 14.27 |
|Â 4 | 2005-04-09 |Â Â Â Â 999 |Â Â Â 7.86 | 16.81 |
|Â 5 | 2005-04-11 |Â Â Â 1172 |Â Â 3.174 |Â 6.63 |
|Â 6 | 2005-05-05 |Â Â Â 1560 |Â Â 8.889 | 18.66 |
|Â 7 | 2005-06-07 |Â Â Â 1985 |Â Â 9.815 | 20.01 |
|Â 8 | 2005-07-03 |Â Â Â 2444 |Â Â 9.868 |Â 21.7 |
|Â 9 | 2005-08-13 |Â Â Â 2762 |Â Â 7.728 |Â Â Â 20 |
| 10 | 2005-09-11 |Â Â Â 3271 |Â 10.072 | 30.11 |
| 11 | 2005-10-24 |Â Â Â 3646 |Â Â 8.953 | 22.19 |
| 12 | 2005-10-30 |Â Â Â 3959 |Â Â 6.583 | 17.11 |
| 13 | 2005-11-04 |Â Â Â 4184 |Â Â 4.538 | 11.25 |
| 14 | 2005-11-21 |Â Â Â 4631 |Â Â 8.742 |Â Â Â 18 |
| 15 | 2005-12-31 |Â Â Â 4897 |Â Â 8.511 | 18.64 |
| 16 | 2006-02-02 |Â Â Â 5609 |Â Â Â 7.83 |Â Â Â 18 |
| 17 | 2006-03-01 |Â Â Â 6064 |Â Â Â 3.38 |Â 7.16 |
| 18 | 2006-03-13 |Â Â Â 6400 |Â Â 7.171 |Â 16.2 |
| 19 | 2006-03-22 |Â Â Â 6605 |Â Â 5.399 | 13.17 |
| 22 | 2006-02-16 |Â Â Â 5944 |Â Â 7.957 |Â 17.5 |
| 20 | 2006-04-06 |Â Â Â 6974 |Â Â 7.774 | 19.43 |
| 21 | 2006-04-23 |Â Â Â 7316 |Â Â 7.102 | 20.45 |
+----+------------+---------+---------+-------+
I wanted a way to calculate how many miles were driven for each trip, i.e. between consecutive dates, and what was the miles per gallon (MPG) for that trip. To do this I modified what I learned in this post, unique ID field, getting next and previous existing ID from table to create a view that does all these calculations. The advantage of this view is that it does not matter what order I enter the data, it will always calculate correctly.
CREATE VIEW v_prius_mpg AS
 SELECT id, date, mileage, gallons, cost, mileage as trip_mileage,
mileage / gallons AS mpg, cost / gallons AS price_per_gallon
   FROM prius_mpg
   WHERE id = 1
 UNION
 SELECT New.id AS id, New.date, New.mileage, New.gallons, New.cost,
(New.mileage - Old.mileage) AS trip_mileage,
(New.mileage - Old.mileage)/ New.gallons AS mpg,
(New.cost / New.gallons) AS price_per_gallon
   FROM prius_mpg New, prius_mpg Old
   WHERE New.id > 1
     AND Old.id =
       (SELECT id
          FROM prius_mpg
          WHERE date < New.date
          ORDER BY date DESC
          LIMIT 1);
And here are the results of the view. Note how row 20 is out of order but it still calculates correctly the trip mileage and MPG.
+----+------------+---------+---------+--------------+------------------+
| id | date      | mileage | gallons | trip_mileage | mpg             |
+----+------------+---------+---------+--------------+------------------+
|Â 1 | 2005-02-14 |Â Â Â Â 280 |Â Â 8.615 |Â Â Â Â Â Â Â Â Â 280 |Â 32.501450957632 |
|Â 2 | 2005-02-27 |Â Â Â Â 480 |Â Â 4.775 |Â Â Â Â Â Â Â Â Â 200 | 41.8848167539267 |
|Â 3 | 2005-03-19 |Â Â Â Â 713 |Â Â 7.213 |Â Â Â Â Â Â Â Â Â 233 | 32.3027866352419 |
|Â 4 | 2005-04-09 |Â Â Â Â 999 |Â Â Â 7.86 |Â Â Â Â Â Â Â Â Â 286 | 36.3867684478371 |
|Â 5 | 2005-04-11 |Â Â Â 1172 |Â Â 3.174 |Â Â Â Â Â Â Â Â Â 173 | 54.5053560176434 |
|Â 6 | 2005-05-05 |Â Â Â 1560 |Â Â 8.889 |Â Â Â Â Â Â Â Â Â 388 | 43.6494543818202 |
|Â 7 | 2005-06-07 |Â Â Â 1985 |Â Â 9.815 |Â Â Â Â Â Â Â Â Â 425 |Â 43.301069791136 |
|Â 8 | 2005-07-03 |Â Â Â 2444 |Â Â 9.868 |Â Â Â Â Â Â Â Â Â 459 | 46.5139845966761 |
|Â 9 | 2005-08-13 |Â Â Â 2762 |Â Â 7.728 |Â Â Â Â Â Â Â Â Â 318 | 41.1490683229814 |
| 10 | 2005-09-11 |Â Â Â 3271 |Â 10.072 |Â Â Â Â Â Â Â Â Â 509 | 50.5361397934869 |
| 11 | 2005-10-24 |Â Â Â 3646 |Â Â 8.953 |Â Â Â Â Â Â Â Â Â 375 | 41.8854015413828 |
| 12 | 2005-10-30 |Â Â Â 3959 |Â Â 6.583 |Â Â Â Â Â Â Â Â Â 313 | 47.5467112258849 |
| 13 | 2005-11-04 |Â Â Â 4184 |Â Â 4.538 |Â Â Â Â Â Â Â Â Â 225 | 49.5813133539004 |
| 14 | 2005-11-21 |Â Â Â 4631 |Â Â 8.742 |Â Â Â Â Â Â Â Â Â 447 | 51.1324639670556 |
| 15 | 2005-12-31 |Â Â Â 4897 |Â Â 8.511 |Â Â Â Â Â Â Â Â Â 266 | 31.2536717189519 |
| 16 | 2006-02-02 |Â Â Â 5609 |Â Â Â 7.83 |Â Â Â Â Â Â Â Â Â 712 | 90.9323116219668 |
| 17 | 2006-03-01 |Â Â Â 6064 |Â Â Â 3.38 |Â Â Â Â Â Â Â Â Â 120 | 35.5029585798817 |
| 18 | 2006-03-13 |Â Â Â 6400 |Â Â 7.171 |Â Â Â Â Â Â Â Â Â 336 | 46.8553897643285 |
| 19 | 2006-03-22 |Â Â Â 6605 |Â Â 5.399 |Â Â Â Â Â Â Â Â Â 205 | 37.9699944434154 |
| 20 | 2006-02-16 |Â Â Â 5944 |Â Â 7.957 |Â Â Â Â Â Â Â Â Â 335 | 42.1012944577102 |
| 21 | 2006-04-06 |Â Â Â 6974 |Â Â 7.774 |Â Â Â Â Â Â Â Â Â 369 |Â 47.465912014407 |
| 22 | 2006-04-23 |Â Â Â 7316 |Â Â 7.102 |Â Â Â Â Â Â Â Â Â 342 | 48.1554491692481 |
+----+------------+---------+---------+--------------+------------------+
I always wanted to appear on every row how many gallons of gas had been consumed up to that day and how much had spent on gas up to date but I could not figure out how. The only thing I could do was determine the sums up until now.
mysql> select sum(gallons) as gallons_to_date,
sum(cost) as cost_to_date from prius_mpg where date <= current_date;
+-----------------+--------------+
| gallons_to_date | cost_to_date |
+-----------------+--------------+
|Â Â Â Â Â Â Â Â 161.949 |Â Â Â Â Â Â 371.91 |
+-----------------+--------------+
1 row in set (0.01 sec)