I have a table like this:
Index , PersonID , ItemCount , UnixTimeStamp
1 , 1 , 1 , 1296000000
2 , 1 , 2 , 1296000100
3 , 2 , 4 , 1296003230
4 , 2 , 6 , 1296093949
5 , 1 , 0 , 1296093295
Time and index are always rising. Its basically a log table to register a list item every time it changes. I get the latest ItemCount for each person, like this:
SELECT *
FROM table a
INNER JOIN
(
SELECT MAX(index) as i
FROM table
GROUP BY PersonID) b
ON a.index = b.i;
I want to do to get the most recent record for each PersonID, which is at least 24 hours older than the most recent record for each Person identifier. Then I want to change the ItemCount value between the two to get the change in the item for each person in the last 24 hours:
personID ChangeInItemCountOverAtLeast24Hours
1 3
2 -11
3 6
I'm stuck on what to do next. How can I join another element of elements based on the last adjusted timestamp of individual rows?