SQL server query for selecting data with 5 seconds interval -
can tell me t-sql script selecting data interval of 5 seconds? have scada system logs data parameter every second want retrieve data interval of 5 sec, start time decided user. example if start = 10:00:01 , end 11:00:00 want see data @ 10:00:06, 10:00:11, 10:00:16... not data @ 10:00:02, 10:00:03 10:00:05, , between 10:00:07 , 10:00:10
i first create dummy data every second 10:00 10:16
declare @data table(creation datetime2); declare @start datetime = '20150930 10:00:00'; declare @end datetime; -- create dummy data 10:00:00 10:16:40 ids(n) ( select row_number() over(order n) ( select 1 (values(1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) x1(n) cross join (values(1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) x2(n) cross join (values(1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) x3(n) ) x(n) ) insert @data(creation) select dateadd(second, n, @start) ids
i calculate difference in second between start date , creation date , use modulo 5 (%5) in order find date every 5s:
set @start = '20150930 10:01:03' set @end = '20150930 10:05:00'; select * , datediff(second, creation, @start) , datediff(second, creation, @start) % 5 @data (datediff(second, creation, @start) % 5) = 0 , creation >= @start , creation <= @end
output:
creation datediff modulo (no column name) 2015-09-30 10:01:03.0000000 0 0 2015-09-30 10:01:08.0000000 -5 0 2015-09-30 10:01:13.0000000 -10 0 ... ... ...
you can see datediff n*5 , there modulo = 0
Comments
Post a Comment