Oracle LAG Function

April 26, 2016

I wanted to refer to the previous row in a query and to do some maths to see the difference in the current row's date from the previous row's date.

This can be done with the Oracle LAG function:

SELECT sch_header, sch_date, sch_number, sch_date - lag(sch_date, 1) OVER (order by sch_header, sch_number) 
FROM schedules 
WHERE sch_header IN (3340, 31341) 
ORDER BY sch_header, sch_number;