I need to find the difference between columns in consecutive rows where the type is datetime. I have a query to sort the data, but can't rely on the index of each for this problem.

I think I have a solution if I dump the sorted rows in another table where the index is in order and incremented by one for each row, but I'd like to avoid using another table. Basically, the sorted rows are obtained with

SELECT f1, f2, datetime FROM table
ORDER BY f1, f2, datetime;

One catch is that for the rows where the difference is computed, f1 and f2 and to be the same between the two rows.

Thanks.