WITH a as (
SELECT 1 id,'2024-09-13 09:00:00' tm
UNION
SELECT 2 id,'2024-09-13 09:05:00' tm
UNION
SELECT 3 id,'2024-09-13 09:10:00' tm
UNION
SELECT 4 id,'2024-09-13 09:15:00' tm
)
SELECT
id,
tm,
-- 使用LAG函数获取上一行的时间戳
LAG(tm) OVER (ORDER BY id) AS PreviousTransactionTime,
-- 计算时间差
DATEDIFF(MINUTE, LAG(tm) OVER (ORDER BY id), tm) AS TimeDifferenceInMinutes
FROM
a;