I have a mysql table A which stores a sequence of numbers and timestamps
value timestamp
12087 2012-01-01 00:00:00
12110 2012-01-02 02:00:00
12223 2012-01-04 03:00:00
12312 2012-01-04 05:00:00
12387 2012-01-04 06:00:00
12388 2012-01-05 01:11:23
...
The both values and timestamps are ordered: the greater timestamp the greater value, but there is no other dependency between them.
I need to find the greatest difference between values in sorted sequence for a period. In the example the differences are:
12110-12087=23
12223-12100=13
12312-12223=89
12387-12312=75
12388-12387=1
So we have the greatest difference 89.
The question is: how to code SQL query for that? Do know, that I don’t want to use full product and/or correlated queries since the table is enormous.
add the period restrictors into the from subquery
edit, new info tells me this is an unreliable method
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html (ctrl + f for
SELECT @a, @a:=@a+1)