After read this thread: MySQL - Subtracting value from previous row, group by
I used the method described in the most upvoted answer and wrote the following code:
SELECT
T.id_str,
T.created_at,
T.retweet_count,
T.tweeted_at,
if (@lastid_str = T.id_str, T.retweet_count - @lastretweet_count, 0000.00) as RetweetChange,
@lastid_str := T.id_str,
@lastretweet_count := T.retweet_count
FROM
Tweets T,
(SELECT @lastid_str := 0,
@lastretweet_count :=0 ) SQLVars
ORDER BY
T.tweeted_at DESC,
T.id_str;
Old problem solved, new problem: how to write a trigger.
My table looks like this. Where id_str is a unique identifier for a specific tweet. Since I am inserting 50 tweets from a single user every minute, there would be many same id_str. What I want to look at is the change of retweet_count every minute. tweeted_at is when the user tweeted, created_at is when this data is inserted into my database. I want to generate retweet_change for each new data inserted into the database compared to the same old tweet (into the column retweet_change). How should I write the trigger?