﻿
CREATE PROCEDURE [dbo].[Charts_HistoricalData_Process]

AS

Truncate table [Knoks_Price].dbo.YP_CRT_Data_1Minute
Truncate table [Knoks_Price].dbo.YP_CRT_Data_5Minutes
Truncate table [Knoks_Price].dbo.YP_CRT_Data_15Minutes
Truncate table [Knoks_Price].dbo.YP_CRT_Data_30Minutes
Truncate table [Knoks_Price].dbo.YP_CRT_Data_1Hour
Truncate table [Knoks_Price].dbo.YP_CRT_Data_2Hours
Truncate table [Knoks_Price].dbo.YP_CRT_Data_4Hours
Truncate table [Knoks_Price].dbo.YP_CRT_Data_8Hours
Truncate table [Knoks_Price].dbo.YP_CRT_Data_1Day
Truncate table [Knoks_Price].dbo.YP_CRT_Data_1Week
Truncate table [Knoks_Price].dbo.YP_CRT_Data_1Month


---- select * from YP_CRT_Data_1Week where CRTD_PairSymbol = 'BACUSD'
---- select * from CRT_Data_1Week where CRTD_PairSymbol = 'BACUSD'

--------------------------------------------
---- Upload Source Data
--------------------------------------------

----YP_CRT_Data_1Minute
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_1Minute]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_Period 
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),HD_Hour,108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_1Minute] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 


--YP_CRT_Data_5Minutes
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_5Minutes]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_Period 
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),DateAdd(minute,4,HD_Hour),108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_5Minutes] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol


----YP_CRT_Data_15Minutes
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_15Minutes]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_Period 
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),DateAdd(minute,14,HD_Hour),108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_15Minutes] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol


----YP_CRT_Data_30Minutes
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_30Minutes]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_Period 
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),DateAdd(minute,29,HD_Hour),108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_30Minutes] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 

----YP_CRT_Data_1Hour
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_1Hour]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(2),HD_Hour,108) + ':00:00'	as datetime)  AS CRTD_Period 
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(8),HD_Hour,108)  	as datetime)  AS CRTD_FirstQuote
--         ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),DateAdd(minute,29,HD_Hour),108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_1Hour] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 

----YP_CRT_Data_4Hours
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_4Hours]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_Period
--  		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' ' + convert(Char(6),HD_Hour,108) + '59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_4Hours] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 


--YP_CRT_Data_1Day
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_1Day]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_Period
--  		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),HD_Date,121) + ' 23:59:59.997' 	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_1Day] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 

----YP_CRT_Data_1Week
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_1Week]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_Period
--  		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),DateAdd(day,6,HD_Date),121) +  ' 23:59:59.997'  	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_1Week] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 

----YP_CRT_Data_1Month
--INSERT INTO [Knoks_Price].dbo.[YP_CRT_Data_1Month]
--           ([CRTD_ID]
--           ,[CRTD_Period]
--           ,[CRTD_FirstQuote]
--           ,[CRTD_LastQuote]
--           ,[CRTD_PairSymbol]
--           ,[CRTD_BidOpeningValue]
--           ,[CRTD_BidClosingValue]
--           ,[CRTD_BidLowValue]
--           ,[CRTD_BidHighValue]
--           ,[CRTD_MidOpeningValue]
--           ,[CRTD_MidClosingValue]
--           ,[CRTD_MidLowValue]
--           ,[CRTD_MidHighValue]
--           ,[CRTD_DateIn])

-- Select HD_Id AS  CRTD_ID    
--		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_Period
--  		,CAST
--			(
--				CASE WHEN 	
--				SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)<13 THEN 
					
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),1,4)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),7,2)+'-'+
--					SUBSTRING(CONVERT(char(10), HD_Date, 112),5,2)
					
--				ELSE CONVERT(char(10), HD_Date, 121)
--				END 

--				+ ' ' + CONVERT(CHAR(8),HD_Hour,108)  	AS DATETIME
--			)  AS CRTD_FirstQuote
--        ,Cast(convert(char(10),EOMonth(HD_Date),121) +  ' 23:59:59.997'  	as datetime)  AS CRTD_LastQuote
--        ,II.[InstrumentSymbol] AS CRTD_PairSymbol
--        ,[HD_Open] AS CRTD_BidOpeningValue
--        ,[HD_Close] AS CRTD_BidClosingValue
--        ,[HD_Low] AS CRTD_BidLowValue
--        ,[HD_High] AS CRTD_BidHighValue
--        ,[HD_Open] AS CRTD_MidOpeningValue
--        ,[HD_Close] AS CRTD_MidClosingValue
--        ,[HD_Low] AS CRTD_MidLowValue
--        ,[HD_High] AS CRTD_MidHighValue
--        ,GetDate() AS CRTD_DateIn
--  FROM YB_Hist.[dbo].[Hist_Data_1Month] HD
--  Inner Join YB_Main.dbo.Instruments II with (nolock)
--  ON HD.[HD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS = II.InstrumentSymbol
 

   ----------------------------------------------
  ------ Remove Data from target by Import Data
  ----------------------------------------------
  /*
  SELECT *	INTO [Knoks_Price].[dbo].[CRT_Data_1Day_BAK_2016_12_21] FROM [Knoks_Price].[dbo].[CRT_Data_1Day]


  SELECT *		  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Day] with (nolock)  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Day] 
 )

  DELETE		  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Day]   
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Day] 
 )


  SELECT *	INTO [Knoks_Price].[dbo].[CRT_Data_4Hours_BAK_2016_12_21] FROM [Knoks_Price].[dbo].[CRT_Data_4Hours]

  SELECT *		  
 FROM [Knoks_Price].[dbo].[CRT_Data_4Hours] with (nolock)  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_4Hours] 
 )

 DELETE	  
 FROM [Knoks_Price].[dbo].[CRT_Data_4Hours] 
  WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_4Hours] 
 )

 SELECT *	INTO [Knoks_Price].[dbo].[CRT_Data_1Month_BAK_2016_12_21] FROM [Knoks_Price].[dbo].[CRT_Data_1Month]

  SELECT *		  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Month] with (nolock)  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Month] 
 )

 DELETE  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Month]  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Month] 
 )
 
  SELECT *	INTO [Knoks_Price].[dbo].[CRT_Data_1Week_BAK_2016_12_21] FROM [Knoks_Price].[dbo].[CRT_Data_1Week]

  SELECT *		  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Week] with (nolock)  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Week] 
 )

 DELETE	  
 FROM [Knoks_Price].[dbo].[CRT_Data_1Week]  
 WHERE  CRTD_ID <0 
 AND CRTD_PairSymbol IN
 (
 SELECT DISTINCT CRTD_PairSymbol FROM [Knoks_Price].[dbo].[YP_CRT_Data_1Week] 
 )

 */
  --select * from YB_Hist.[dbo].[Hist_Data_1Month] order by 2
  ----------------------------------------------
  ------ Insert Data to target Charts Tables
  ----------------------------------------------
  
  SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

       declare @MinId bigint
	   declare @Sql varchar(max)

/*
 select top 1 * from CRT_Data_5Minutes	 Order by 1 
 select top 1 * from CRT_Data_15Minutes	 Order by 1 
 select top 1 * from CRT_Data_30Minutes	 Order by 1 
 select top 1 * from CRT_Data_1Hour		 Order by 1 
 select top 1 * from CRT_Data_2Hours	 Order by 1 
 select top 1 * from CRT_Data_4Hours	 Order by 1 
 select top 1 * from CRT_Data_8Hours     Order by 1 
 select top 1 * from CRT_Data_1Day		 Order by 1 
 select top 1 * from CRT_Data_1Week		 Order by 1 
 select top 1 * from CRT_Data_1Month	 Order by 1 
*/

  IF EXISTS (select 1 from [Knoks_Price].dbo.[YP_CRT_Data_1Minute]) 
  BEGIN

		
      RAISERROR ('Processing Data 1Minute', 0, 1) WITH NOWAIT
	  ----------------------------
	  ---- YP_CRT_Data_1Minute
	  ----------------------------
	  IF Object_ID('YB_Charts.dbo.TMP_Data_1Minute')  IS NOT NULL
		Drop Table YB_Charts.dbo.TMP_Data_1Minute

	   --DECLARE @MinId bigint
	   --DECLARE @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_1Minute ORDER BY 1 
       If @MinId >=0 SET @MinId = -1


		select @Sql = 'CREATE TABLE [Knoks_Price].dbo.[TMP_Data_1Minute](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 


		  ;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_1Minute] with (nolock)
			Group By [CRTD_PairSymbol]
		)

		INSERT INTO [Knoks_Price].dbo.[TMP_Data_1Minute]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	
	  FROM [Knoks_Price].dbo.[YP_CRT_Data_1Minute] CC
	  LEFT JOIN [Knoks_Price].dbo.[CRT_Data_1Minute] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  Order by [CRTD_PairSymbol],[CRTD_Period] DESC


	   -- select * from #CRTD_1Minute  Order by CRTD_PairSymbol,CRTD_Period desc
	   -- select * from CRT_Data_1Minute where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_1Minute] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_1Minute]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[Knoks_Price].dbo.TMP_Data_1Minute
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_1Minute] OFF


  End

  IF EXISTS (select 1 from [Knoks_Price].dbo.[YP_CRT_Data_5Minutes]) 
  BEGIN
	
	  RAISERROR ('Processing Data 5Minutes', 0, 1) WITH NOWAIT
	  ----------------------------
	  ---- YP_CRT_Data_5Minutes
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_5Minutes')  IS NOT NULL
		Drop Table YB_Charts.dbo.TMP_Data_5Minutes

	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_5Minutes ORDER BY 1 
       If @MinId >=0 SET @MinId = -1


		select @Sql = 'CREATE TABLE [Knoks_Price].dbo.[TMP_Data_5Minutes](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 




		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_5Minutes] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [Knoks_Price].dbo.[TMP_Data_5Minutes]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	

	  FROM [Knoks_Price].dbo.[YP_CRT_Data_5Minutes] CC
	  LEFT JOIN [dbo].[CRT_Data_5Minutes] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	  --  select * from #CRTD_5Minutes  Order by CRTD_PairSymbol,CRTD_Period desc
	  -- select * from CRT_Data_5Minutes where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_5Minutes] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_5Minutes]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[Knoks_Price].dbo.[TMP_Data_5Minutes]
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_5Minutes] OFF


  End

  IF EXISTS (select 1 from [Knoks_Price].dbo.[YP_CRT_Data_15Minutes]) 
  BEGIN

	  RAISERROR ('Processing Data 15Minutes', 0, 1) WITH NOWAIT
  
	  ----------------------------
	  ---- YP_CRT_Data_15Minutes
	  ----------------------------
	  IF Object_ID('YB_Charts.dbo.TMP_Data_15Minutes')  IS NOT NULL
		Drop Table YB_Charts.dbo.TMP_Data_15Minutes

	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from [Knoks_Price].dbo.CRT_Data_15Minutes ORDER BY 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [Knoks_Price].dbo.[TMP_Data_15Minutes](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 



		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_15Minutes] with (nolock)
			Group By [CRTD_PairSymbol]
		)

		INSERT INTO [Knoks_Price].dbo.[TMP_Data_15Minutes]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	  FROM [Knoks_Price].dbo.[YP_CRT_Data_15Minutes] CC
	  LEFT JOIN [Knoks_Price].dbo.[CRT_Data_15Minutes] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	  --  select * from #CRTD_15Minutes  Order by CRTD_PairSymbol,CRTD_Period desc
	  -- select * from CRT_Data_15Minutes where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_15Minutes] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_15Minutes]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[Knoks_Price].dbo.[TMP_Data_15Minutes]
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_15Minutes] OFF


  END	

  IF EXISTS (select 1 from [YP_CRT_Data_30Minutes]) 
  BEGIN

	  RAISERROR ('Processing Data 30Minutes', 0, 1) WITH NOWAIT

	  ----------------------------
	  ---- YP_CRT_Data_30Minutes
	  ----------------------------
	  IF Object_ID('YB_Charts.dbo.TMP_Data_30Minutes')  IS NOT NULL
		Drop Table YB_Charts.dbo.TMP_Data_30Minutes

	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from [Knoks_Price].dbo.CRT_Data_30Minutes     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [Knoks_Price].dbo.[TMP_Data_30Minutes](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 


		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_30Minutes] with (nolock)
			Group By [CRTD_PairSymbol]
		)

		INSERT INTO [Knoks_Price].dbo.[TMP_Data_30Minutes]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	  FROM [Knoks_Price].dbo.[YP_CRT_Data_30Minutes] CC
	  LEFT JOIN [Knoks_Price].dbo.[CRT_Data_30Minutes] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	  --  select * from #CRTD_30Minutes  Order by CRTD_PairSymbol,CRTD_Period desc
	  -- select * from CRT_Data_30Minutes where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_30Minutes] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_30Minutes]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[Knoks_Price].dbo.[TMP_Data_30Minutes]
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_30Minutes] OFF


  End

  
  IF EXISTS (select 1 from [Knoks_Price].dbo.[YP_CRT_Data_1Hour]) 
  BEGIN

     RAISERROR ('Processing Data 1Hour', 0, 1) WITH NOWAIT
   
	  ----------------------------
	  ---- YP_CRT_Data_1Hour
	  ----------------------------
	  IF Object_ID('YB_Charts.dbo.TMP_Data_1Hour')  IS NOT NULL
		Drop Table YB_Charts.dbo.[TMP_Data_1Hour]

       -- declare @MinId bigint
	   -- declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from [Knoks_Price].dbo.CRT_Data_1Hour     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [Knoks_Price].dbo.[dbo].[TMP_Data_1Hour](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 


		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_1Hour] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [Knoks_Price].dbo.[TMP_Data_1Hour]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
		  FROM [Knoks_Price].dbo.[YP_CRT_Data_1Hour] CC
		  LEFT JOIN [Knoks_Price].dbo.[CRT_Data_1Hour] XX with (nolock)
		  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
			 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
		  LEFT JOIN MinPeriodsPerSymbol PS
		  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
		  where XX.[CRTD_ID] IS NULL
		  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
		  Order by [CRTD_PairSymbol],[CRTD_Period] DESC

		  --  select * from #CRTD_1Hour  Order by CRTD_PairSymbol,CRTD_Period desc
		  -- select * from CRT_Data_1Hour where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_1Hour] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_1Hour]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[Knoks_Price].dbo.[TMP_Data_1Hour]
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_1Hour] OFF


  End

  IF EXISTS (select 1 from [Knoks_Price].dbo.[YP_CRT_Data_2Hours]) 
  BEGIN

     RAISERROR ('Processing Data 2Hours', 0, 1) WITH NOWAIT
  
	  ----------------------------
	  ---- YP_CRT_Data_2Hours
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_2Hours')  IS NOT NULL
		Drop Table YB_Charts..TMP_Data_2Hours


       select top 1 @MinId= [CRTD_ID] -1 from [Knoks_Price].dbo.CRT_Data_2Hours     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [Knoks_Price].dbo.[dbo].[TMP_Data_2Hours](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 



		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [Knoks_Price].dbo.[CRT_Data_2Hours] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [Knoks_Price].dbo.[TMP_Data_2Hours]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	

	  FROM [Knoks_Price].dbo.[YP_CRT_Data_2Hours] CC
	  LEFT JOIN [dbo].[CRT_Data_2Hours] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  Order by [CRTD_PairSymbol],[CRTD_Period] DESC


	 --  select * from #CRTD_2Hours  Order by CRTD_PairSymbol,CRTD_Period desc
	 -- select * from CRT_Data_2Hours where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_2Hours] ON
		INSERT INTO [Knoks_Price].dbo.[CRT_Data_2Hours]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	TMP_Data_2Hours
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [Knoks_Price].dbo.[CRT_Data_2Hours] OFF


  End


  IF EXISTS (select 1 from [YP_CRT_Data_4Hours]) 
  BEGIN
  
      RAISERROR ('Processing Data 4Hours', 0, 1) WITH NOWAIT
	  ----------------------------
	  ---- YP_CRT_Data_4Hours
	  ----------------------------
	  IF Object_ID('YB_Charts.dbo.TMP_Data_4Hours')  IS NOT NULL
		Drop Table YB_Charts.dbo.TMP_Data_4Hours


	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_4Hours     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [dbo].[TMP_Data_4Hours](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 

		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [dbo].[CRT_Data_4Hours] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [dbo].[TMP_Data_4Hours]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	
	  FROM [dbo].[YP_CRT_Data_4Hours] CC
	  LEFT JOIN [dbo].[CRT_Data_4Hours] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  --Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	 --  select * from #CRTD_4Hours  Order by CRTD_PairSymbol,CRTD_Period desc
	 -- select * from CRT_Data_4Hours where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [CRT_Data_4Hours] ON
		INSERT INTO [dbo].[CRT_Data_4Hours]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	TMP_Data_4Hours 
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_4Hours] OFF


  End


  IF EXISTS (select 1 from [YP_CRT_Data_8Hours]) 
  BEGIN

     RAISERROR ('Processing Data 8Hours', 0, 1) WITH NOWAIT

	  ----------------------------
	  ---- YP_CRT_Data_8Hours
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_8Hours')  IS NOT NULL
		Drop Table YB_Charts..TMP_Data_8Hours


	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_8Hours     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [dbo].[TMP_Data_8Hours](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 




		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [dbo].[CRT_Data_8Hours] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [dbo].[TMP_Data_8Hours]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	  FROM [dbo].[YP_CRT_Data_8Hours] CC
	  LEFT JOIN [dbo].[CRT_Data_8Hours] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  --Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	 --  select * from #CRTD_8Hours  Order by CRTD_PairSymbol,CRTD_Period desc
	 -- select * from CRT_Data_8Hours where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [CRT_Data_8Hours] ON
		INSERT INTO [dbo].[CRT_Data_8Hours]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	#CRTD_8Hours 
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_8Hours] OFF

	 

  End

  IF EXISTS (select 1 from [YP_CRT_Data_1Day]) 
  BEGIN

      RAISERROR ('Processing Data 1Day', 0, 1) WITH NOWAIT

	  ----------------------------
	  ---- YP_CRT_Data_1Day
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_1Day')  IS NOT NULL
		Drop Table YB_Charts..TMP_Data_1Day


	   --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_1Day     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [dbo].[TMP_Data_1Day](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 



		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [dbo].[CRT_Data_1Day] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [dbo].[TMP_Data_1Day]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]	
	  FROM [dbo].[YP_CRT_Data_1Day] CC
	  LEFT JOIN [dbo].[CRT_Data_1Day] XX with (nolock)
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
		 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  --Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	 --  select * from #CRTD_1Day  Order by CRTD_PairSymbol,CRTD_Period desc
	 -- select * from CRT_Data_1Day where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [CRT_Data_1Day] ON
		INSERT INTO [dbo].[CRT_Data_1Day]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[TMP_Data_1Day] 
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_1Day] OFF

  End

  
  IF EXISTS (select 1 from [YP_CRT_Data_1Week]) 
  BEGIN

      RAISERROR ('Processing Data 1Week', 0, 1) WITH NOWAIT

	  ----------------------------
	  ---- YP_CRT_Data_1Week
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_1Week')  IS NOT NULL
		Drop Table YB_Charts..TMP_Data_1Week


       --declare @MinId bigint
	   --declare @Sql varchar(max)
       select top 1 @MinId= [CRTD_ID] -1 from [CRT_Data_1Week]     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [dbo].[TMP_Data_1Week](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 



		;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [dbo].[CRT_Data_1Week] with (nolock)
			Group By [CRTD_PairSymbol]
		)


		INSERT INTO [dbo].[TMP_Data_1Week]
				   ([CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn])
		  SELECT 
			   CC.[CRTD_Period]
			  ,CC.[CRTD_FirstQuote]
			  ,CC.[CRTD_LastQuote]
			  ,CC.[CRTD_PairSymbol]
			  ,CC.[CRTD_BidOpeningValue]
			  ,CC.[CRTD_BidClosingValue]
			  ,CC.[CRTD_BidLowValue]
			  ,CC.[CRTD_BidHighValue]
			  ,CC.[CRTD_MidOpeningValue]
			  ,CC.[CRTD_MidClosingValue]
			  ,CC.[CRTD_MidLowValue]
			  ,CC.[CRTD_MidHighValue]
			  ,CC.[CRTD_DateIn]
		  FROM [dbo].[YP_CRT_Data_1Week] CC
		  LEFT JOIN [dbo].[CRT_Data_1Week] XX with (nolock)
		  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
			 AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
		  LEFT JOIN MinPeriodsPerSymbol PS
		  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
		  where XX.[CRTD_ID] IS NULL
		  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
		  --Order by [CRTD_PairSymbol],[CRTD_Period] DESC


	 --  select * from #CRTD_1Week  Order by CRTD_PairSymbol,CRTD_Period desc
	 -- select * from CRT_Data_1Week where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc

	    SET IDENTITY_INSERT [CRT_Data_1Week] ON
		INSERT INTO [dbo].[CRT_Data_1Week]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
		SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[TMP_Data_1Week]
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_1Week] OFF




  End

  IF EXISTS (select 1 from [YP_CRT_Data_1Month]) 
  BEGIN

      RAISERROR ('Processing Data 1Month', 0, 1) WITH NOWAIT
	  ----------------------------
	  --- YP_CRT_Data_1Month
	  ----------------------------
	  IF Object_ID('YB_Charts..TMP_Data_1Month')  IS NOT NULL
		Drop Table YB_Charts..TMP_Data_1Month

	   --declare @MinId bigint
	   --declare @Sql varchar(max)

       select top 1 @MinId= [CRTD_ID] -1 from CRT_Data_1Month     Order by 1 
       If @MinId >=0 SET @MinId = -1

	   SELECT @Sql = 'CREATE TABLE [dbo].[TMP_Data_1Month](
			[CRTD_ID] [int] IDENTITY(' + cast(@MinId as varchar(10))+ ',-1) NOT NULL,
			[CRTD_Period] [datetime] NOT NULL,
			[CRTD_FirstQuote] [datetime] NOT NULL,
			[CRTD_LastQuote] [datetime] NOT NULL,
			[CRTD_PairSymbol] [varchar](50) NOT NULL,
			[CRTD_BidOpeningValue] [float] NOT NULL,
			[CRTD_BidClosingValue] [float] NOT NULL,
			[CRTD_BidLowValue] [float] NOT NULL,
			[CRTD_BidHighValue] [float] NOT NULL,
			[CRTD_MidOpeningValue] [float] NOT NULL,
			[CRTD_MidClosingValue] [float] NOT NULL,
			[CRTD_MidLowValue] [float] NOT NULL,
			[CRTD_MidHighValue] [float] NOT NULL,
			[CRTD_DateIn] [datetime] NOT NULL )'

		exec (@Sql) 
	


	  DECLARE @CurrMonth date

	  SELECT @CurrMonth = cast(DateAdd(day , -datePart(day,getdate())+1, getdate()) as date)

	   ;With MinPeriodsPerSymbol AS
		(
			SELECT [CRTD_PairSymbol], Min([CRTD_Period]) MinOfPeriod 
			FROM [dbo].[CRT_Data_1Month] with (nolock)
			Group By [CRTD_PairSymbol]
		)	


	INSERT INTO [dbo].[TMP_Data_1Month]
			   ([CRTD_Period]
			   ,[CRTD_FirstQuote]
			   ,[CRTD_LastQuote]
			   ,[CRTD_PairSymbol]
			   ,[CRTD_BidOpeningValue]
			   ,[CRTD_BidClosingValue]
			   ,[CRTD_BidLowValue]
			   ,[CRTD_BidHighValue]
			   ,[CRTD_MidOpeningValue]
			   ,[CRTD_MidClosingValue]
			   ,[CRTD_MidLowValue]
			   ,[CRTD_MidHighValue]
			   ,[CRTD_DateIn])
	  SELECT 
		   CC.[CRTD_Period]
		  ,CC.[CRTD_FirstQuote]
		  ,CC.[CRTD_LastQuote]
		  ,CC.[CRTD_PairSymbol]
		  ,CC.[CRTD_BidOpeningValue]
		  ,CC.[CRTD_BidClosingValue]
		  ,CC.[CRTD_BidLowValue]
		  ,CC.[CRTD_BidHighValue]
		  ,CC.[CRTD_MidOpeningValue]
		  ,CC.[CRTD_MidClosingValue]
		  ,CC.[CRTD_MidLowValue]
		  ,CC.[CRTD_MidHighValue]
		  ,CC.[CRTD_DateIn]
	
	  FROM [dbo].[YP_CRT_Data_1Month] CC
	  LEFT JOIN [dbo].[CRT_Data_1Month] XX
	  ON CC.[CRTD_Period] = XX.[CRTD_Period] 
	  AND CC.CRTD_PairSymbol = XX.CRTD_PairSymbol
	  LEFT JOIN MinPeriodsPerSymbol PS
	  ON CC.[CRTD_PairSymbol] = PS.[CRTD_PairSymbol]
  
	  where XX.[CRTD_ID] IS NULL
	  AND cast(dateadd(day,-datepart(day,CC.[CRTD_Period])+1, CC.[CRTD_Period]) as date) < @CurrMonth
	  AND CC.[CRTD_Period] < IsNull(PS.MinOfPeriod,GetDate())
	  --Order by [CRTD_PairSymbol],[CRTD_Period] DESC

	-- select * from #CRTD_1Month  Order by CRTD_PairSymbol,CRTD_Period desc
	-- select * from CRT_Data_1Month where  CRTD_PairSymbol = 'AAPLUSD' Order by CRTD_PairSymbol,CRTD_Period desc


    SET IDENTITY_INSERT [CRT_Data_1Month] ON
	INSERT INTO [dbo].[CRT_Data_1Month]
				   ([CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
				   )
     SELECT		
					[CRTD_ID]
				   ,[CRTD_Period]
				   ,[CRTD_FirstQuote]
				   ,[CRTD_LastQuote]
				   ,[CRTD_PairSymbol]
				   ,[CRTD_BidOpeningValue]
				   ,[CRTD_BidClosingValue]
				   ,[CRTD_BidLowValue]
				   ,[CRTD_BidHighValue]
				   ,[CRTD_MidOpeningValue]
				   ,[CRTD_MidClosingValue]
				   ,[CRTD_MidLowValue]
				   ,[CRTD_MidHighValue]
				   ,[CRTD_DateIn]
		FROM	[TMP_Data_1Month]  
		ORDER BY CRTD_PairSymbol,CRTD_Period DESC
		SET IDENTITY_INSERT [CRT_Data_1Month] OFF



	  


  End


  /*

    select top 5000 * from CRT_Data_1Minute where CRTD_PairSymbol = 'AAPLUSD'
  and cast([CRTD_Period] as date) = '2016-06-07'
  order by [CRTD_Period]

  select * from #CRTD_1Minute  where CRTD_PairSymbol = 'AAPLUSD' order by CRTD_PairSymbol,CRTD_Period desc
  */