SQL Computed Column Dynamic datediff

Viewed 12

I have one column of dates and wish to calculate a second column that shows the time difference in minutes between all dates. Example below.

time_stamp                diff_in_minutes
2022-09-23 18:40:00.000         --
2022-09-23 18:45:00.000         5

My problem is that the query needs to be dynamic for any time_stamp column size. I don't think/am not sure that datediff can be used this way.

Thank you!

1 Answers

For anyone looking at this in the future, Barmar comment was a big help.

TIMESTAMPDIFF(MINUTE, lag(time_stamp, 1, 0) over(), time_stamp)
Related