日期:2014-05-18 浏览次数:20603 次
;with cte as( select *,row_number() over (partition by userid order by operate_time) rid from tb ) select * from cte a left join cte b on (a.rid=b.rid-1 and datediff(seconde,a.operate_time,b.operate_time)>30) --没环境,LZ试下,思路差不多,细节自己可以改下
------解决方案--------------------
use Tempdb go --> --> if not object_id(N'user_log') is null drop table user_log Go Create table user_log([userid] nvarchar(2),[operate_time] Datetime) Insert user_log select N'a','2011-1-4 10:00:00' union all select N'b','2011-1-4 10:00:00' union all select N'b','2011-1-4 10:00:05' union all select N'a','2011-1-4 10:00:31' Go Select * from user_log AS a WHERE DATEADD(s,5,[operate_time])<(SELECT MIN([operate_time]) FROM user_log WHERE [userid]=a.[userid] AND [operate_time]>a.[operate_time]) /* userid operate_time a 2011-01-04 10:00:00.000*/