Zdravim.
som zaciatocnik v SQL a potreboval by som pomoct s nasledovnym problemom: mam proceduru, ktora vyhodnocuje rozdiel nekonecneho pocitadla medzi dvoma zadanymi casovymi intervalmi:
Kód:
USE [Runtime]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[MNEORCH]
@Rok int, @Mesiac int, @Den int, @Hodina int, @Minuta int, @Sekunda int, @Rok2 int, @Mesiac2 int, @Den2 int, @Hodina2 int, @Minuta2 int, @Sekunda2 int
-- Pociatocny a konecny datum sledovaneho obdobia
as
declare @Zaciatok as datetime , @Koniec as datetime -- zaciatok a koniec sledovaneho intervalu
declare @obdobie varchar(99)
set @obdobie=cast(@Rok as varchar(5))+'-'+cast(@Mesiac as varchar(5))+'-'+cast(@Den as varchar(5))+' '+cast(@Hodina as varchar(5))+':'+cast(@Minuta as varchar(5))+':'+cast(@Sekunda as varchar(5))
set @Zaciatok=convert(datetime,@obdobie,120)
set @obdobie=cast(@Rok2 as varchar(5))+'-'+cast(@Mesiac2 as varchar(5))+'-'+cast(@Den2 as varchar(5))+' '+cast(@Hodina2 as varchar(5))+':'+cast(@Minuta2 as varchar(5))+':'+cast(@Sekunda2 as varchar(5))
set @Koniec=convert(datetime,@obdobie,120)
declare @Tab table(Polozka varchar(50),Hodnota bigint)
declare @Tab2 table(Polozka varchar(50),Hodnota bigint)
declare @Tab3 table(Cas varchar(50),Hodnota datetime)
insert into @Tab
select 'TAGx' ,
((select Value from AnalogHistory where Tagname='TAGx'+'_TOT.MLD_Sec' and Datetime=@Koniec) *1000000000
+(select Value from AnalogHistory where Tagname='TAGx'+'_TOT.Sec' and Datetime=@Koniec)) -
((select Value from AnalogHistory where Tagname='TAGx'+'_TOT.MLD_Sec' and Datetime=@Zaciatok) *1000000000+
(select Value from AnalogHistory where Tagname='TAGx'+'_TOT.Sec' and Datetime=@Zaciatok))
insert into @Tab3
select 'Pociatocny' ,(select @Zaciatok)
insert into @Tab3
select 'Konecny' ,(select @Koniec)
-- vsetky NULL hodnoty nahradim -1
insert into @Tab2
select Polozka,
case
when Hodnota is null then -1
else Hodnota
end
from @Tab
-- vysledny SELECT klientovi
select * from @Tab2
select * from @Tab3
problem vsak nastane, ak bolo toto pocitadlo znulovane niekde v sledovanom casovom obdobi, kedy sa moze stat napr. ze hodnota v konecnom case < hodnata v zaciatocnom case... potreboval by som to nejak osetrit, aby si procedura sama zistila, ci ma pocitadlo v zadanom obdobi stale rastucu tendenciu a ak nema, rozdeli si obdobie na viacero usekov, kde nema pokles a zrata rozdiely z jednotlivych usekov.....alebo nejake podobne riesenie, co by osetrilo znulovanie toho pocitadla.... verim, ze sa to da nejak jednoducho elegantne urobit, ale neviem ako... pls help