Knocks/BackEnd/Knoks.Operate/Functions/GetPrevRate.sql

20 lines
730 B
Transact-SQL

CREATE FUNCTION GetPrevRate(
@Duration int,
@Currency1 varchar(30),
@Currency2 varchar(30),
@ExchangeId int) RETURNS @Result TABLE(RateDate datetime, Rate decimal(22, 8))
AS
Begin
INSERT INTO @Result(RateDate, Rate)
SELECT TOP 1 CRT.CRTD_CreationDate, CAST(CRT.CRTD_BidClosingValue AS decimal(22, 8))
FROM [Knoks_Prices].[dbo].[CRT_Data_LiveChart] CRT
WHERE CRT.CRTD_PairSymbol = @Currency1 + @Currency2 AND CRT.ExchangeId = @ExchangeId
AND CRT.CRTD_CreationDate <= GETUTCDATE()
AND EXISTS (SELECT 1 FROM GetLastRate(@Duration, @Currency1, @Currency2, @ExchangeId) t1
WHERE CRT.CRTD_CreationDate < t1.RateDate AND CRT.CRTD_BidClosingValue <> t1.Rate)
ORDER BY CRT.CRTD_CreationDate DESC
RETURN;
End
GO