CREATE PROCEDURE [dbo].[GetData]
	@pair  varchar(50),
	@start DATETIME,
	@end DATETIME, 
	@resolution varchar(10)
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

/*
---- Example :
Exec [GetData] 'EURUSD','2015-06-01', '2015-06-05','1'
*/


IF @resolution = '5' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_5Minutes]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]
    

ELSE IF @resolution = '15' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_15Minutes]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = '30' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_30Minutes]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = '60' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_1Hour]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = '120' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_2Hours]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = '240' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_4Hours]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = '480' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_8Hours]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = 'D' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_1Day]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = 'W' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_1Week]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE IF @resolution = 'M' 
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_1Month]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
    ORDER BY [CRTD_Period]

ELSE
		SELECT 
		 [CRTD_Period] as 'Timestamp'
		,[CRTD_BidOpeningValue] as 'BidOpen'
		,[CRTD_BidClosingValue] as 'BidClose'
		,[CRTD_BidLowValue] as 'BidLow'
		,[CRTD_BidHighValue] as 'BidHigh'

		,[CRTD_MidOpeningValue] as 'MidOpen'
		,[CRTD_MidClosingValue] as 'MidClose'
		,[CRTD_MidLowValue] as 'MidLow'
		,[CRTD_MidHighValue] as 'MidHigh'


	FROM [CRT_Data_1Minute]
	WHERE [CRTD_Period] >= @start
		AND [CRTD_Period] <= @end
		AND [CRTD_PairSymbol] = @pair
   ORDER BY [CRTD_Period]
RETURN 0