sql - Get a Row if within certain time period of other row -


i have sql statement using return number of rows database:

select     as1.assettagid, as1.tagid, as1.categoryid,     as1.description, as1.homelocationid, as1.parentassettagid assets as1     inner join assetsreads ar  on as1.assettagid = ar.assettagid     (ar.readpointlocationid='readpoint1' or ar.readpointlocationid='readpoint2')     , (ar.datescanned between 'lastscan' , 'now')     , as1.tagid!='000000000000000000000000' 

i wanting query row oldest datescanned query , row database if there 1 within period of time row (say 5 seconds example). oldest record relatively simple selecting first record in descending sort, how second record if within time period of first?

i know process multiple queries, there way combine process 1 query?

the database using sql server 2008 r2.

also please note datescanned times placeholders , taking care of in application using query.

here general way approach it. oldest scan date using min() window function, use date arithmetic rows want:

select t.*  -- or whatever fields want (select as1.assettagid, as1.tagid, as1.categoryid,              as1.description, as1.homelocationid, as1.parentassettagid,              min(datescanned) on () mindatescanned, datescanned       assets as1            inner join assetsreads ar  on as1.assettagid = ar.assettagid       (ar.readpointlocationid='readpoint1' or ar.readpointlocationid='readpoint2')             , (ar.datescanned between 'lastscan' , 'now')             , as1.tagid!='000000000000000000000000'      ) t datediff(second, mindatescanned, datescanned) <= 5; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -