Time Difference Between Two Consecutive Rows

oracle

i need simple SQL to calculate the time diff between Two Consecutive Rows
anyone can help

enter image description here

Best Answer

You can use a window function (aka "Analytic Function") to access values in the rows outside of the "current" row.

If you do that, it is mandatory to specify a criteria that defines the sort order of the rows otherwise there isn't such a thing as "the previous rows".

Given the extremely limited information you have shown us, you are probably looking for something like the following:

select subno,
       contrno, orderby, prepost_paid, 
       time_stamp, 
       lag(time_stamp) over (order by time_stamp) - time_stamp as timestamp_diff
from the_table
order by time_stamp;