set nocount on
declare @tele_time datetime, @last_tele_time datetime,@start_time datetime, @min_diff int, @count int,@ObjectID int
set @min_diff=4 -- количество минут указывается в интерфейсе
select @ObjectID=ObjectID from [dbo].[FLEET_Object] where ObjectActive=1 and ObjectGarNum like 'н428%' -- гаражный номер указывается в интерфейсе
create table #per (start_time datetime, end_time datetime, coord_count int, diff_min int)
declare cur cursor local for select tele_time from [dbo].[SYS_DEV_ArchiveData] as AD
where AD.ObjectID = @ObjectID and tele_time>dateadd(dd,-1,getdate()) order by tele_time
open cur fetch next from cur into @tele_time
select @last_tele_time=@tele_time,@start_time=@tele_time,@count=1
while 1=1 begin
fetch next from cur into @tele_time if @@FETCH_STATUS!=0 break
if abs(datediff (ss,@last_tele_time,@tele_time))>@min_diff*60
begin insert into #per values (@start_time,@last_tele_time,@count,datediff (mi,@last_tele_time,@tele_time)) select @count=0, @start_time=@tele_time end
select @last_tele_time=@tele_time, @count=@count+1
end
close cur deallocate cur
insert into #per values (@start_time,@tele_time,@count,datediff (mi,@last_tele_time,@tele_time))
select start_time, end_time, coord_count,diff_min from #per drop table #per where start_time is not null