Hi,
My tsql foo is weak and I'm looking for a little help. I am trying to find the largest gap between timesstamps for a column. As you can see in the small example below, I have five rows and I'm looking to find the difference in seconds between consecutive rows and then order them by the biggest gap descending.
I wouldn't assume the msg_ids are 100% sequential with never a gap:
WITH cte_msgs AS (
SELECT msg_datetime, ROW_NUMBER() OVER(ORDER BY msg_datetime) AS row_num
FROM table_name
)
SELECT MAX(DATEDIFF(SECOND, m1.msg_datetime, m2.msg_datetime)) AS Max_TimeDiff_In_Seconds
FROM cte_msgs m1
INNER JOIN cte_msgs m2 ON m2.row_num = m1.row_num + 1