Here is a solution without variables. I assume that you have initail data in a table named thetable .
SELECT date, time, ip, result - IFNULL( ( SELECT MAX( result ) FROM thetable WHERE ip = t1.ip AND ( date < t1.date OR date = t1.date AND time < t1.time ) ) , 0) AS diff FROM thetable AS t1 ORDER BY ip, date, time
Here we get the previous value with a subquery (maximum result previous timestamps from the same ip ). IFNULL gives us 0 if that was the first value, so the original results are displayed correctly.
I also recommend adding the following index to thetable :
CREATE INDEX sort1 ON thetable (ip, date, time);
vbence
source share