How do I get the differences between two dates in Oracle that were in the same column?

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

+2


source to share


1 answer


You can use LEAD / LAG OVER analytical 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

      

+11


source







All Articles