Nawaf
Nawaf

Reputation: 540

How to find Difference Time Between two record

i Get some Problem with SQL , the problem is find the Difference between two record not in the same row . i take picture of the Excel sheet to illustrate what i mean . i need to find the difference between the recoed n and n+1 , one by one .

EXCEL SHEET

Upvotes: 0

Views: 52

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

You want the lag() function:

select t.*,
       (t.admission_date_time - lag(t.admission_date_time) over (order by t.admission_date_time
       ) as diff
from table t;

This will fetch the value from the previous row.

Upvotes: 2

Related Questions