You have a table containing a StartDate column and desire to compute the time difference between two adjacent rows. To accomplish this, follow these steps:
If request IDs are non-consecutive, you can leverage the following query:
SELECT A.requestid, A.starttime, (B.starttime - A.starttime) AS timedifference FROM MyTable A CROSS JOIN MyTable B WHERE B.requestid IN (SELECT MIN (C.requestid) FROM MyTable C WHERE C.requestid > A.requestid) ORDER BY A.requestid ASC
This query employs a cross join and selects the minimum request ID greater than the current request ID from a subquery. Subsequently, it calculates the time difference as before.
The above is the detailed content of How to Calculate Time Differences between Consecutive Rows in a Table?. For more information, please follow other related articles on the PHP Chinese website!