You can do something along these lines:
SELECT T.id,
T.other_col,
T.yourdate,
IFNULL(datediff(T.yourdate,(select MAX(TT.yourdate)
FROM yourtable TT
WHERE TT.yourdate < T.yourdate
AND TT.other_col = 'abc')),0)
FROM yourtable T
where other_col = 'abc';
You can see it in this fiddle
This works by calculating the difference between the current record's date (T.date
) and the date that is the biggest (MAX(TT.date)
) of the one's that are smaller than the current record's date ( WHERE TT.date < T.date
).
When there is no smaller date, there's the COALESCE to make it be 0, instead of null.
Edit. added coalesce to give 0 in the first line