How can I Filter all records where same id's Time differ a few seconds sql server -
this select statement data between dates:
select distinct events.clock,events.tagid events (datediff(second, events.clock, '2013/04/20') <= 30) , (datediff(second, events.clock, '2013/04/21') >= 30) order events.tagid desc
this results statement.
| clock | tagid | --------------------------------------------- 1 | 2013-04-20 12:39:18 | 4cb0000060032 | 2 | 2013-04-20 12:39:16 | 4cb0000060032 | 3 | 2013-04-20 16:53:09 | 4cb0000060032 | 4 | 2013-04-20 13:22:38 | 4cb00000600ef | 5 | 2013-04-20 13:22:40 | 4cb00000600ef | 6 | 2013-04-20 15:20:56 | 4cb00000600d2 | 7 | 2013-04-20 15:17:31 | 4cb00000600d2 | 8 | 2013-04-20 15:20:58 | 4cb00000600d2 | 9 | 2013-04-20 19:33:09 | 4cb00000600d1 | 10 | 2013-04-20 20:39:16 | 4cb00000600d1 | 11 | 2013-04-20 11:10:38 | 4cb00000600d1 |
now filter results more because can see there records time differ in milliseconds , others in seconds, rid of records same tagid , time follows few seconds/milliseconds. duplicates of tagid fine not times few seconds/milliseconds apart another.
thus record 2, 4, 6, 7 not there.
then results following:
| clock | tagid | ------------------------------------------- 1 | 2013-04-20 12:39:18 | 4cb0000060032 | 2 | 2013-04-20 16:53:09 | 4cb0000060032 | 3 | 2013-04-20 13:22:40 | 4cb00000600ef | 4 | 2013-04-20 15:20:58 | 4cb00000600d2 | 5 | 2013-04-20 19:33:09 | 4cb00000600d1 | 6 | 2013-04-20 20:39:16 | 4cb00000600d1 | 7 | 2013-04-20 11:10:38 | 4cb00000600d1 |
create temp table hold resuls current query:
create table #events ( clock datetime, tagid varchar(20) )
your current query:
insert #events select distinct events.clock,events.tagid events (datediff(second, events.clock, '2013/04/20') <= 30) , (datediff(second, events.clock, '2013/04/21') >= 30) order events.tagid desc
then select rows there no earlier dated row (for same tagid) within timeframe. i've used 3000 milliseconds here example.
select * #events e1 not exists (select 1 #events e2 e2.tagid = e1.tagid , e2.clock < e1.clock , datediff(millisecond, e2.clock, e1.clock) < 3000)