I have a table with columns date and time_spent. I want to find for each date D the sum of the values of \'time_spent\' for the period of time : (D
date
time_spent
Here you go:
select t.*, @total:=@total + t.time_spent - coalesce(t2.time_spent, 0) as your_total_over_last_7_days from rolling_total t left join rolling_total t2 on t.date = t2.date + interval 8 day , (select @total:=0) v order by t.`date`