您可以使用
LEAD功能将登录时间与注销时间进行匹配:
WITH cte AS ( SELECt devid , reason , amount , LEAD(reason) OVER (PARTITION BY devid ORDER BY datetime) AS next_reason , LEAD(amount) OVER (PARTITION BY devid ORDER BY datetime) AS next_amount FROM t WHERe reason IN (5, 8))SELECt *, amount - next_amount AS diffFROM cteWHERe reason = 5 -- logonAND next_reason = 8 -- next event is a logoutAND amount - next_amount >= 10 -- difference of current and next



