I'm not 100% sure, what exactly it is, you're seeking.
If you want to show the "violating" records, do this:
select emp_name
,[datetime]
,amount
from yourtable as a
where exists (select 1
from yourtable as b
where b.emp_name=a.emp_name
and abs(datediff(second,b.[datetime],a.[datetime]))<=5
having count(*)>1
)
;
If you want to show all record from an employee who has "violated" your rule, do this:
with cte
as (select emp_name
from yourtable as a
where exists (select 1
from yourtable as b
where b.emp_name=a.emp_name
and abs(datediff(second,b.[datetime],a.[datetime]))<=5
having count(*)>1
)
group by emp_name
)
select b.emp_name
,b.[datetime]
,b.amount
from cte as a
inner join yourtable as b
on b.emp_name=a.emp_name
;
If you want to show all record from an employee who has "violated" your rule, but only the days he/she "violated" your rule, do this:
with cte
as (select emp_name
,dateadd(day,0,datediff(day,0,[datetime])) as from_date
,dateadd(day,1,datediff(day,0,[datetime])) as to_date
from yourtable as a
where exists (select 1
from yourtable as b
where b.emp_name=a.emp_name
and abs(datediff(second,b.[datetime],a.[datetime]))<=5
having count(*)>1
)
group by emp_name
,datediff(day,0,[datetime])
)
select b.emp_name
,b.[datetime]
,b.amount
from cte as a
inner join yourtable as b
on b.emp_name=a.emp_name
and b.[datetime]>=a.from_date
and b.[datetime]<a.to_date
;