SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE procedure [dbo].[p_currencies_rates_by_date]
	@exchange_date  date 

as

set nocount on

select acr.*
from acc_currencies_rates acr
	inner join	(
					select	cr_base_code, 
							cr_traded_code, 
							max(cr_date) as max_date 
					from acc_currencies_rates
					where cr_date <= @exchange_date
					group by cr_base_code, cr_traded_code 
				) as tmp on		acr.cr_base_code = tmp.cr_base_code
							and acr.cr_traded_code = tmp.cr_traded_code
							and acr.cr_date = tmp.max_date	

GO
