Skip to content

Instantly share code, notes, and snippets.

@smarenich
Created November 5, 2018 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarenich/5a1f2addf5a3ec2c4ef9e144c9eedcde to your computer and use it in GitHub Desktop.
Save smarenich/5a1f2addf5a3ec2c4ef9e144c9eedcde to your computer and use it in GitHub Desktop.
IF OBJECT_ID ('dbo.[px_Rates]', 'V') IS NOT NULL
DROP VIEW [dbo].[px_Rates]
GO
CREATE VIEW [dbo].[px_Rates] AS
select FromCury.CompanyID as CompanyID, FromCury.CuryID as FromCuryID, ToCury.CuryID as ToCuryID, Date as CuryEffDate,
Coalesce((Select top 1 CuryRateID
from CurrencyRate r
where r.CuryEffDate <= Date and r.CompanyID = FromCury.CompanyID and r.FromCuryID = FromCury.CuryID and r.ToCuryID = ToCury.CuryID
order by CuryEffDate), 0) as RateID
from (SELECT DATEADD(DAY, nbr - 1, '20110901') as Date
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS Nbr
FROM sys.columns c
) nbrs
WHERE nbr - 1 <= DATEDIFF(DAY, '20160101', '20250101')) Dates
Cross Join Currency FromCury
Left Join Currency ToCury on FromCury.CompanyID = ToCury.CompanyID and FromCury.CuryID <> ToCury.CuryID
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment