SQL SERVER LEAD和LAG使用

示例:获取在48小时之内重复的记录

SELECT * FROM ( SELECT b.* , LAG(b.OperatorTime, 1, b.OperatorTime) OVER ( PARTITION BY b.No ORDER BY b.OperatorTime ) AS BeforTime , LEAD(b.OperatorTime, 1, b.OperatorTime) OVER ( PARTITION BY b.No ORDER BY b.OperatorTime ) AS NextTime FROM Test b ) a WHERE DATEDIFF(HH, a.BeforTime, a.OperatorTime) < 24 AND DATEDIFF(HH, a.OperatorTime, a.NextTime) < 24 AND a.No IN ( SELECT c.No FROM dbo.Test c GROUP BY c.No HAVING COUNT(c.No) > 1 )

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggffw.html