--exec USP_UTIL_CRT_Validation_5min '2016-12-13'
CREATE procedure [dbo].[USP_UTIL_CRT_Validation_5min]
(@checkDay datetime)
as
set nocount on
declare @TableName	sysname,@UnitDefinition varchar(15),@TimeUnit varchar(10),@TimeInterval varchar(10),@sql nvarchar(max),@minLastRun datetime
create table #temp (CRTD_ID int,
					CRTD_Period datetime,
					CRTD_FirstQuote datetime,
					CRTD_LastQuote  datetime,
					CRTD_PairSymbol varchar(15),
					CRTD_BidOpeningValue float ,
					CRTD_BidClosingValue float ,
					CRTD_BidLowValue float ,
					CRTD_BidHighValue float ,
					CRTD_MidOpeningValue float ,
					CRTD_MidClosingValue float ,
					CRTD_MidLowValue float ,
					CRTD_MidHighValue float,
					StartIntervalDate datetime,
					EndIntervalDate datetime)

create index ix_StartIntervalDate on #temp(StartIntervalDate) 

create table #temp1 (StartPeriod datetime,
					EndPeriod datetime,
					First_Quote datetime,
					Last_Quote datetime,
					CRTD_PairSymbol varchar(15),
					BidMin_Value float,
					BidMax_Value float,
					MidMin_Value float,
					MidMax_Value float,
					Min_RowId int,
					Max_RowId int,
					BidOpen_Value float,
					BidClose_Value float,
					MidOpen_Value float,
					MidClose_Value float   )	

create table #MismatchRecords 
(CRTD_Period datetime,
CRTD_FirstQuote datetime,
CRTD_LastQuote datetime ,
CRTD_PairSymbol varchar(15),
CRTD_BidOpeningValue float,
CRTD_BidClosingValue float,
CRTD_BidLowValue float,
CRTD_BidHighValue float,
CRTD_MidOpeningValue float,
CRTD_MidClosingValue float,
CRTD_MidLowValue float,
CRTD_MidHighValue float)

SELECT CRTD_PairSymbol ,max([CRTD_LastQuote]) as max_LastQuote
into   #LastQuote
from  [Knoks_Price].[dbo].[CRT_Data_1Minute](nolock)
group by CRTD_PairSymbol 
	
insert into #temp(CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,CRTD_MidOpeningValue,
					CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue,StartIntervalDate,EndIntervalDate)
SELECT CRTD_Period ,CRTD_FirstQuote ,CRTD_LastQuote ,CRTD_PairSymbol ,CRTD_BidOpeningValue ,CRTD_BidClosingValue  
	    ,CRTD_BidLowValue ,CRTD_BidHighValue ,CRTD_MidOpeningValue ,CRTD_MidClosingValue ,CRTD_MidLowValue ,CRTD_MidHighValue,
			dateadd(minute, datediff(minute,0,CRTD_Period) /5 * 5, 0) as StartIntervalDate,
		dateadd(millisecond,-3,dateadd(minute,5, (dateadd(minute, datediff(minute,0,CRTD_Period) / 5* 5, 0)))) as EndIntervalDate
from  YB_Charts.dbo.CRT_Data_1Minute (nolock)
where CRTD_Period BETWEEN CAST(@checkDay AS DATE) AND CAST(dateadd(millisecond,-3,@checkDay+1) AS DATEtIME)

insert into #temp1
		(StartPeriod, EndPeriod, First_Quote, Last_Quote, CRTD_PairSymbol, BidMin_Value, BidMax_Value, MidMin_Value, MidMax_Value)			
select  StartIntervalDate,
		EndIntervalDate,
		min(CRTD_FirstQuote) First_Quote, max(CRTD_LastQuote) Last_Quote, 
		A.CRTD_PairSymbol,
		min(CRTD_BidLowValue) BidMin_Value, max(CRTD_BidHighValue) BidMax_Value, 
		min(CRTD_MidLowValue) MidMin_Value, max(CRTD_MidHighValue) MidMax_Value			 
from #temp A
	inner join #LastQuote B on A.CRTD_PairSymbol=B.CRTD_PairSymbol 
where A.EndIntervalDate<=B.max_LastQuote
group by StartIntervalDate,EndIntervalDate,A.CRTD_PairSymbol
				
update A
set BidOpen_Value = B.CRTD_BidOpeningValue,
	MidOpen_Value = B.CRTD_MidOpeningValue
FROM #temp1 A
	INNER JOIN (select StartIntervalDate,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_MidOpeningValue,ROW_NUMBER() over (partition by CRTD_PairSymbol,StartIntervalDate order by crtd_period) as rownum
				from #temp) B ON A.CRTD_PairSymbol = B.CRTD_PairSymbol and A.StartPeriod=B.StartIntervalDate
where B.rownum=1

update A
set BidClose_Value = B.CRTD_BidClosingValue,
	MidClose_Value = B.CRTD_MidClosingValue
FROM #temp1 A
	INNER JOIN (select StartIntervalDate,CRTD_PairSymbol,CRTD_BidClosingValue,CRTD_MidClosingValue,ROW_NUMBER() over (partition by CRTD_PairSymbol,StartIntervalDate order by crtd_period desc) as rownum
				from #temp) B ON A.CRTD_PairSymbol = B.CRTD_PairSymbol and A.StartPeriod=B.StartIntervalDate
where B.rownum=1

insert into #MismatchRecords 
	(CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
		CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue)
select StartPeriod, First_Quote, Last_Quote, CRTD_PairSymbol, BidOpen_Value, BidClose_Value, BidMin_Value, BidMax_Value,
		MidOpen_Value, MidClose_Value, MidMin_Value, MidMax_Value
from #temp1 
except
select CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
		CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue
from CRT_Data_5Minutes 
where CRTD_Period BETWEEN CAST(@checkDay AS DATE) AND CAST(dateadd(millisecond,-3,@checkDay+1) AS DATEtIME)

if @@rowcount>0
	begin
		select 'There are mismatches in CRT_Data_5Minutes. Here are the mismatched records:'
		select * from
					(select 'Calculated' as Source,CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
							CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue
					from #MismatchRecords 
					union all
					select 'Saved' as Source,CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
							CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue
					from CRT_Data_5Minutes A
					where CRTD_Period BETWEEN CAST(@checkDay AS DATE) AND CAST(dateadd(millisecond,-3,@checkDay+1) AS DATEtIME)
					and exists(select 1 from #MismatchRecords B where A.CRTD_Period=B.CRTD_Period and A.CRTD_PairSymbol=B.CRTD_PairSymbol))s
		order by CRTD_Period,CRTD_PairSymbol,Source
	end
else
	begin
		insert into #MismatchRecords 
			(CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
				CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue)
		select CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
				CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue
		from CRT_Data_5Minutes 
		where CRTD_Period BETWEEN CAST(@checkDay AS DATE) AND CAST(dateadd(millisecond,-3,@checkDay+1) AS DATEtIME)
		except
		select StartPeriod, First_Quote, Last_Quote, CRTD_PairSymbol, BidOpen_Value, BidClose_Value, BidMin_Value, BidMax_Value,
		  		MidOpen_Value, MidClose_Value, MidMin_Value, MidMax_Value
		from #temp1				
		if @@rowcount>0
			begin
				select 'There are mismatches in CRT_Data_5Minutes. Here are the mismatched records:'
				select * from
						(select 'Saved' as Source,CRTD_Period,CRTD_FirstQuote,CRTD_LastQuote,CRTD_PairSymbol,CRTD_BidOpeningValue,CRTD_BidClosingValue,CRTD_BidLowValue,CRTD_BidHighValue,
								CRTD_MidOpeningValue,CRTD_MidClosingValue,CRTD_MidLowValue,CRTD_MidHighValue
						from #MismatchRecords 
						union all
						select 'Calculated' as Source,StartPeriod, First_Quote, Last_Quote, CRTD_PairSymbol, BidMin_Value, BidMax_Value, MidMin_Value, MidMax_Value,
								MidOpen_Value, MidClose_Value,BidOpen_Value, BidClose_Value
						from #temp1 A
						where exists(select 1 from #MismatchRecords B where A.StartPeriod=B.CRTD_Period and A.CRTD_PairSymbol=B.CRTD_PairSymbol))s
						order by CRTD_Period,CRTD_PairSymbol,Source				
			end 		
		else
			select 'There are no mismatches in CRT_Data_5Minutes at '+convert(varchar(30),@checkDay,120)
	end
		
drop table #temp
drop table #temp1
drop table #MismatchRecords
drop table #LastQuote