tags:

views:

309

answers:

1

I need the difference between two dates in Oracle in terms of days in which the dates were in the same column. That is, the difference between two dates after order by that column. That is, after doing the order by, I need the difference between first two dates.

+11  A: 

You can use the LEAD/LAG OVER analytic functions.

Perhaps something like:

SELECT 
  TheDate CurrentDate, 
  LAG(TheDate, 1, 0) OVER (ORDER BY TheDate) PriorDate,
  TheDate - LAG(TheDate, 1, 0) OVER (ORDER BY TheDate) Difference
FROM SomeTable
David Andres