CREATE Procedure [dbo].[USP_CRT_LiveChartProcess] AS SET NOCOUNT ON declare @DT_Sec bigint, @StartDate datetime, @interval int, @RefDate datetime set @StartDate = getdate() SET @RefDate = '2015-01-01' --select getdate() declare @MaxId bigint Declare @Queue table ( [Id] BIGINT Primary Key Clustered , [PairSymbol] [varchar](50) NOT NULL, [Bid] [float] NOT NULL, [Ask] [float] NOT NULL, [Mid] [float] NOT NULL, [BidProviderId] [tinyint] NOT NULL, [AskProviderId] [tinyint] NOT NULL, [BidCreationDate] [datetime] NOT NULL, [AskCreationDate] [datetime] NOT NULL, [BidReceivedDate] [datetime] NOT NULL, [AskReceivedDate] [datetime] NOT NULL, [SavedDate] [datetime] NOT NULL ) select @MaxId = Max([Id]) from [YB_Quotes].[dbo].[SpotRatesQueue] with (nolock) --select @MaxId MaxId IF @MaxId IS NOT NULL Begin DELETE [YB_Quotes].[dbo].[SpotRatesQueue] OUTPUT DELETED.[Id], DELETED.[PairSymbol], DELETED.[Bid], DELETED.[Ask], DELETED.[Mid], DELETED.[BidProviderId], DELETED.[AskProviderId], DELETED.[BidCreationDate], DELETED.[AskCreationDate], DELETED.[BidReceivedDate], DELETED.[AskReceivedDate], DELETED.[SavedDate] INTO @Queue WHERE [Id] <= @MaxId select [PairSymbol], convert(varchar(16),[BidReceivedDate],121) as [minute], min([Bid]) as Bid_Low , --- Low max([Bid]) as Bid_High, --- High min([Mid]) as Mid_Low , --- Low max([Mid]) as Mid_High, --- High Count([Id]) as Cnt INTO #c2 from @Queue where 1=1 group by convert(varchar(16), [BidReceivedDate] ,121) ,[PairSymbol] select [PairSymbol], convert(varchar(16),[BidReceivedDate] ,121) as [minute], [BidReceivedDate] AS [DateQuote], [Bid] AS Bid_Val, [Mid] AS Mid_Val, ROW_NUMBER() over (partition by [PairSymbol],convert(varchar(16),[BidReceivedDate] ,121) order by [BidReceivedDate] ) as Bid_start, ROW_NUMBER() over (partition by [PairSymbol],convert(varchar(16),[BidReceivedDate] ,121) order by [BidReceivedDate] desc) as Bid_end INTO #c3 from @Queue where 1=1 create index ix_PairSymbol on #c2(PairSymbol) create index ix_minute on #c2([minute]) create index ix_PairSymbol on #c3(PairSymbol) create index ix_minute on #c3([minute]) create index ix_Bid_start on #c3(Bid_start) create index ix_Bid_end on #c3(Bid_end) UPDATE [dbo].[CRT_Data_LiveChart] SET [CRTD_CreationDate] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.[CRTD_Period] else [CRT_Data_LiveChart].[CRTD_CreationDate] end ,[CRTD_FirstQuote] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.[CRTD_FirstQuote] else [CRT_Data_LiveChart].[CRTD_FirstQuote] end ,[CRTD_LastQuote] = A.[CRTD_LastQuote] ,[CRTD_BidOpeningValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then IsNull([CRTD_BidClosingValue],A.Bid_OpenValue) else [CRTD_BidOpeningValue] end ,[CRTD_BidClosingValue] = A.Bid_CloseValue ,[CRTD_BidLowValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.Bid_Low when A.Bid_Low < [CRTD_BidLowValue] then A.Bid_Low else [CRTD_BidLowValue] end ,[CRTD_BidHighValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.Bid_High when A.Bid_High > [CRTD_BidHighValue] then A.Bid_High else [CRTD_BidHighValue] end ,[CRTD_MidOpeningValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then IsNull([CRTD_MidClosingValue],A.Mid_OpenValue) else [CRTD_MidOpeningValue] end ,[CRTD_MidClosingValue] = A.Mid_CloseValue ,[CRTD_MidLowValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.Mid_Low when A.Mid_Low < [CRTD_MidLowValue] then A.Mid_Low else [CRTD_MidLowValue] end ,[CRTD_MidHighValue] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then A.Mid_High when A.Mid_High > [CRTD_MidHighValue] then A.Mid_High else [CRTD_MidHighValue] end ,[CRTD_DateModified] = GetDate() ,[CRTD_Rows] = Case when A.[CRTD_Period] > [CRT_Data_LiveChart].[CRTD_CreationDate] then IsNull([CRTD_Rows],0) + Cnt else Cnt end From ( select '1Minute' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*1))*(60*1) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '1Minute' Else Null End As [Ind], */ c2.[minute] [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 C2 Inner join #c3 C3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '5Minutes' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*5))*(60*5) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '5Minutes' Else Null End As [Ind], */ dateadd(minute, datepart(minute, C2.[minute]) / 5 * 5 - datepart(minute, C2.[minute]), C2.[minute]) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 AS c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '15Minutes' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*15))*(60*15) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '15Minutes' Else Null End As [Ind], */ dateadd(minute, datepart(minute, C2.[minute]) / 15 * 15 - datepart(minute, C2.[minute]), C2.[minute]) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '30Minutes' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*30))*(60*30) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '30Minutes' Else Null End As [Ind], */ dateadd(minute, datepart(minute, C2.[minute]) / 30 * 30 - datepart(minute, C2.[minute]), C2.[minute]) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '1Hour' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*60))*(60*60) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '1Hour' Else Null End As [Ind], */ dateadd(minute, datepart(minute, C2.[minute]) / 60 * 60 - datepart(minute, C2.[minute]), C2.[minute]) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '2Hours' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*60*2))*(60*60*2) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '2Hours' Else Null End As [Ind], */ --dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) dateadd(hour, datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) ) / 2 * 2 - datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) ), dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) ) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '4Hours' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*60*4))*(60*60*4) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '4Hours' Else Null End As [Ind], */ --dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) dateadd(hour, datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 240 * 240 - datepart(minute, C2.[minute]), C2.[minute]) ) / 4 * 4 - datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 240 * 240 - datepart(minute, C2.[minute]), C2.[minute]) ), dateadd(minute, datepart(minute, C2.[minute]) / 240 * 240 - datepart(minute, C2.[minute]), C2.[minute]) ) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '8Hours' AS [Target], /* Case when (datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) /(60*60*8))*(60*60*8) =datediff(second,@RefDate,Cast(convert(char(16), GetDate(),121) as datetime)) then '8Hours' Else Null End As [Ind], */ --dateadd(minute, datepart(minute, C2.[minute]) / 120 * 120 - datepart(minute, C2.[minute]), C2.[minute]) dateadd(hour, datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 480 * 480 - datepart(minute, C2.[minute]), C2.[minute]) ) / 8 * 8 - datepart(hour, dateadd(minute, datepart(minute, C2.[minute]) / 480 * 480 - datepart(minute, C2.[minute]), C2.[minute]) ), dateadd(minute, datepart(minute, C2.[minute]) / 480 * 480 - datepart(minute, C2.[minute]), C2.[minute]) ) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '1Week' AS [Target], /* Case when dateadd(day, -datepart(weekday, Cast(Convert(char(10),c2.[minute],121) as datetime))+1 , Cast(Convert(char(10),c2.[minute],121) as datetime)) = dateadd(day, -datepart(weekday, Cast(Convert(char(10),GetDate(),121) as datetime))+1 , Cast(Convert(char(10),GetDate(),121) as datetime)) then '1Week' Else Null End As [Ind], */ dateadd(day, -datepart(weekday, Cast(Convert(char(10),c2.[minute],121) as datetime))+1 , Cast(Convert(char(10),c2.[minute],121) as datetime)) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION select '1Day' AS [Target], /* Case when Cast(Convert(char(10),c2.[minute],121) as datetime) = Cast(Convert(char(10),GetDate(),121) as datetime) then '1Day' Else Null End As [Ind], */ Cast(Convert(char(10),c2.[minute],121) as datetime) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 UNION ---select dateadd(day,-datepart(day,getdate())+1,Cast(Convert(char(10),GetDate(),121) as datetime) ) ---select dateadd(day,-datepart(day,c2.[minute])+1,Cast(Convert(char(10),c2.[minute],121) as datetime) ) select '1Month' AS [Target], /* Case when dateadd(day,-datepart(day,c2.[minute])+1,Cast(Convert(char(10),c2.[minute],121) as datetime) ) = Cast(Convert(char(10),GetDate(),121) as datetime) then '1Month' Else Null End As [Ind], */ dateadd(day,-datepart(day,c2.[minute])+1,Cast(Convert(char(10),c2.[minute],121) as datetime) ) [CRTD_Period], c3.[DateQuote] [CRTD_FirstQuote], c3_Bid.[DateQuote] [CRTD_LastQuote], c2.PairSymbol, c3.Bid_Val as Bid_OpenValue, IsNull(c3_Bid.Bid_Val,0) as Bid_CloseValue, Bid_Low, Bid_High, IsNull(c3.Mid_Val,0) as Mid_OpenValue, IsNull(c3_Bid.Mid_Val,0) as Mid_CloseValue, Mid_Low, Mid_High, GetDate() As Date_Inserted, c2.Cnt from #c2 as c2 Inner join #c3 as c3 on c2.[PairSymbol] = c3.[PairSymbol] and c2.[minute] = c3.[minute] and Bid_start =1 Inner join #c3 as c3_Bid on c2.[PairSymbol] = c3_Bid.[PairSymbol] and c2.[minute] = c3_Bid.[minute] and c3_Bid.Bid_end =1 Where 1=1 ) AS A Where [CRTD_TargetName] = A.[Target] collate SQL_Latin1_General_CP1_CI_AS And [CRTD_PairSymbol] = A.PairSymbol collate SQL_Latin1_General_CP1_CI_AS --and PairSymbol = 'AUDUSD' --and Target = '1Minute' drop table #c2 drop table #c3 End -- select datediff(ms,@StartDate,getdate()) TimeToProcess ---- select * from [dbo].[CRT_Data_LiveChart] where [CRTD_PairSymbol] = 'AUDUSD' AND [CRTD_TargetName] = '1Minute'