I have a table with an object, index, date and value:
+--------------+-------+------------+------------+
| object | index | date | value |
+--------------+-------+------------+------------+
| 32 | 1 | 2011-02-25 | 2100000000 |
| 32 | 2 | 2011-02-25 | 27800000 |
| 32 | 3 | 2011-02-25 | 5700000 |
| 32 | 1 | 2011-02-26 | 2100000000 |
| 32 | 2 | 2011-02-26 | 28700000 |
| 32 | 3 | 2011-02-26 | 5800000 |
| 32 | 1 | 2011-02-27 | 2200000000 |
| 32 | 2 | 2011-02-27 | 29500000 |
| 32 | 3 | 2011-02-27 | 5900000 |
+--------------+-------+------------+------------+
and I need a query with the difference of the value between two consecutive days for every objectindex
so something like this
+--------------+-------+------------+------------+
| object | index | date | value_24h |
+--------------+-------+------------+------------+
| 32 | 1 | 2011-02-26 | 0 |
| 32 | 2 | 2011-02-26 | 0 |
| 32 | 3 | 2011-02-26 | 100000 |
| 32 | 1 | 2011-02-27 | 100000000 |
| 32 | 2 | 2011-02-27 | 800000 |
| 32 | 3 | 2011-02-27 | 100000 |
+--------------+-------+------------+------------+
Is this possible in mysql or do I better calculate these values in my program (python).
Or would a different/better table layout help?
Thanks,
Michael
1 Answer