Если мы удаляем эти результаты из набора результатов, и события каждого пользователя обрабатываются отдельно, тогда работают следующие (кража таблицы defn из ответа Andomar):
declare @t table (ID int, Username varchar(50), EventDate datetime)
insert @t
select 1, 'UserA', '2010-10-21 16:59:59.367'
union all select 2, 'UserA', '2010-10-21 17:00:00.114'
union all select 3, 'UserA', '2010-10-21 17:00:00.003'
union all select 4, 'UserA', '2010-10-21 17:00:02.867'
union all select 5, 'UserB', '2010-10-21 18:43:26.538'
union all select 6, 'UserB', '2010-10-21 18:47:33.373'
;WITH PerUserIDs AS (
SELECT ID,Username,EventDate,ROW_NUMBER() OVER (PARTITION BY Username ORDER BY EventDate) as R from @t
), Sequenced AS (
SELECT ID,Username,EventDate,R from PerUserIDs where R = 1
union all
select pui.ID,pui.UserName,pui.EventDate,pui.R
from
Sequenced s
inner join
PerUserIDs pui
on
s.R < pui.R and
s.Username = pui.Username and
DATEDIFF(millisecond,s.EventDate,pui.EventDate) >= 3000
where
not exists(select * from PerUserIDs anti where anti.R < pui.R and s.R < anti.R and s.Username = anti.username and DATEDIFF(millisecond,s.EventDate,anti.EventDate)>= 3000)
)
select * from Sequenced order by Username,EventDate
Если вам нужнона самом деле удалить, затем вы можете удалить из таблицы, где идентификатор не в (выберите идентификатор из последовательности)