
/*




*/

CREATE Procedure [dbo].[USP_CRT_LiveChartDataInit]




	 @PairSymbol varchar(50)

AS 



--- EXEC USP_CRT_LiveChartDataInit @PairSymbol = 'SYBGBP'

---- truncate table [CRT_Data_LiveChart]
--delete from CRT_Data_LiveChart where [CRTD_PairSymbol] = @PairSymbol


IF EXISTS ( select 1 from [CRT_Data_LiveChart] where [CRTD_PairSymbol] = @PairSymbol)
Begin
	print 'Data Already Exists'
	Return 1
End


INSERT INTO [dbo].[CRT_Data_LiveChart]
           ([CRTD_TargetName]
           ,[CRTD_CreationDate]
           ,[CRTD_FirstQuote]
           ,[CRTD_LastQuote]
           ,[CRTD_PairSymbol]
           ,[CRTD_BidOpeningValue]
           ,[CRTD_BidClosingValue]
           ,[CRTD_BidLowValue]
           ,[CRTD_BidHighValue]
           ,[CRTD_MidOpeningValue]
           ,[CRTD_MidClosingValue]
           ,[CRTD_MidLowValue]
           ,[CRTD_MidHighValue]
           ,[CRTD_DateModified]
           )
 select top 1 '15Minutes' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		@PairSymbol [CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_15Minutes] with (nolock)
WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '1Day' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_1Day] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]

UNION


 select '1Hour' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_1Hour] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '1Minute' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_1Minute] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '1Month' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_1Month] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]



UNION

 select '1Week' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_1Week] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '2Hours' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_2Hours] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '30Minutes' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_30Minutes] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]

UNION

 select '4Hours' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_4Hours] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]


UNION

 select '5Minutes' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_5Minutes] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]

UNION

 select '8Hours' AS [CRTD_TargetName],
		Max([CRTD_Period]) AS [CRTD_Period] ,
		Max([CRTD_LastQuote]) AS [CRTD_FirstQuote],
		Max([CRTD_LastQuote]) AS [CRTD_LastQuote],
		[CRTD_PairSymbol],
		0 AS 	[CRTD_BidOpeningValue],
        0 AS    [CRTD_BidClosingValue],
        Max([CRTD_BidLowValue])   AS  [CRTD_BidLowValue],
        Min([CRTD_BidHighValue])  AS  [CRTD_BidHighValue],
        0 AS    [CRTD_MidOpeningValue],
        0 AS   [CRTD_MidClosingValue],
        Max([CRTD_MidLowValue]) AS  [CRTD_MidLowValue],
        Min([CRTD_MidHighValue]) AS [CRTD_MidHighValue],
        GetDate() AS [CRTD_DateModified]

from [dbo].[CRT_Data_8Hours] with (nolock)
	WHERE  [CRTD_PairSymbol] = @PairSymbol
Group By [CRTD_PairSymbol]



ORDER BY 1,2,5


	

select * from [dbo].[CRT_Data_LiveChart] 
WHERE  [CRTD_PairSymbol] = @PairSymbol


/*

-- Checks : 
select * from [YB_Quotes].[dbo].[PairSymbols] with (nolock)
where  [PairSymbol] NOT in (select [CRTD_PairSymbol] collate SQL_Latin1_General_CP1_CI_AS from YB_Charts..[CRT_Data_LiveChart]) 



select [CRTD_PairSymbol],Count(*) 
from  [dbo].[CRT_Data_LiveChart] with (nolock)
Group By [CRTD_PairSymbol]
Having count(*) <> 11
*/