I have some historical data in a MySQL database, and I'm making graphs of it. Currently, my graphs look like this:
I'd like, however, to get the change in the value over time - i.e. show the change since the last measurement. Something like this (obviously the data isn't correct):
How can I write a MySQL query that gets the delta of a value when compared with the previous row?
This is my current query, with help from juergen d:
SELECT `timestamp`, total_users, total_users - @prev AS col1, @prev := total_users, FROM stats, (select @prev := 0) p GROUP BY DAY(`timestamp`), floor(HOUR(`timestamp`)/2)
Generally
Use a variable that holds the last records' value to calculate the delta
SQLFiddle demo
The actual query