Скрипт ищет разрывы или пересечения в тарировках в показаниях от датчиков
На выходе таблица с номера терминалов, номер датчика, его код в таблице
Цитата
declare @SensorId int,@SensorMin numeric (19,4), @SensorMax numeric (19,4),@LSensorMin numeric (19,4), @LSensorMax numeric (19,4)
/****** Скрипт для команды SelectTopNRows из среды SSMS ******/
--SELECT TOP 1000 t.*
-- FROM [CP_ST-BusinessEntity].[Data]. [Taring]as t
-- left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E on t.EntityId=e.EntityId
-- where t. [SensorId]= 4802 and e.Deleted is null
--order by SensorMin
declare @t table ( [SensorId]int,SensorMin float)
declare cur cursor local for
select distinct [SensorId]from [CP_ST-BusinessEntity].[Data]. [Taring]as T
left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E2 on t.[SensorId]=e2.EntityId
where e2.Deleted is null
-- and t.SensorId=6232
open cur
while 1=1 begin
FETCH NEXT FROM cur
INTO @SensorId
if @@FETCH_STATUS<>0 break
declare curTar cursor local for
select SensorMin, SensorMax from [CP_ST-BusinessEntity].[Data]. [Taring]as t
left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E on t.EntityId=e.EntityId where t. [SensorId]= @SensorId and e.Deleted is null
order by SensorMin
--select @SensorId, t.SensorMin, t.SensorMax, t.*, e2.* from [CP_ST-BusinessEntity].[Data]. [Taring]as t
--left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E on t.EntityId=e.EntityId
--left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E2 on t.[SensorId]=e2.EntityId
--where t. [SensorId]= @SensorId and e.Deleted is null
--order by t.SensorMin
open curTar
FETCH NEXT FROM curTar
INTO @LSensorMin,@LSensorMax
while 1=1 begin
FETCH NEXT FROM curTar
INTO @SensorMin,@SensorMax
if @@FETCH_STATUS<>0 break
if @LSensorMax<>@SensorMin
begin
print @LSensorMax
print @SensorMin
print '----'
insert into @t values (@SensorId,@SensorMin);
end
select @LSensorMin = @SensorMin,@LSensorMax = @SensorMax
end
close curTar
deallocate curTar
end
close cur
deallocate cur
select distinct t.code, s.number, temp.SensorMin from @t as Temp inner join [CP_ST-BusinessEntity].[Data]. [Sensor]as S on s.EntityId=Temp.SensorId
inner join [CP_ST-BusinessEntity].[Data]. [Terminal]as T on s.TerminalId=t.EntityId
left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E on t.EntityId=Temp.SensorId
left join [CP_ST-BusinessEntity].[Entity]. [Entity]as E2 on t.EntityId=e2.EntityId
where e.Deleted is null and e2.Deleted is null