From 9d11aa165bf9b4bff102df8d7da54fb94edca05e Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Fri, 14 Jun 2024 15:31:46 +0200 Subject: [PATCH] past working --- .../int_daily_currency_exchange_rates.sql | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/models/intermediate/cross/int_daily_currency_exchange_rates.sql b/models/intermediate/cross/int_daily_currency_exchange_rates.sql index 4154de4..367c7ee 100644 --- a/models/intermediate/cross/int_daily_currency_exchange_rates.sql +++ b/models/intermediate/cross/int_daily_currency_exchange_rates.sql @@ -2,7 +2,7 @@ with stg_xedotcom__exchange_rates as ( select * from {{ ref("stg_xedotcom__exchange_rates") }} ), - -- CTEs for projecting forwards + -- CTEs for projecting forward most_recent_xe_date_per_curr_pair as ( select from_currency, to_currency, max(rate_date_utc) as most_recent_date_utc from stg_xedotcom__exchange_rates @@ -40,19 +40,20 @@ with last_rate.from_currency, last_rate.to_currency, future_dates.future_date, - 'inferred', + 'xe_inferred', 'forecast' ) as id_exchange_rate, last_rate.from_currency, last_rate.to_currency, last_rate.rate, future_dates.future_date as rate_date_utc, - 'inferred' as source, + 'xe_inferred' as source, 'forecast' as rate_version, now() as updated_at_utc from future_dates future_dates cross join last_xe_rate_per_curr_pair last_rate ), + -- CTEs for projecting forward oldest_xe_date_per_curr_pair as ( select from_currency, to_currency, max(rate_date_utc) as oldest_date_utc from stg_xedotcom__exchange_rates @@ -66,6 +67,38 @@ with on xe.from_currency = recent.from_currency and xe.to_currency = recent.to_currency and xe.rate_date_utc = recent.oldest_date_utc + ), + past_dates as ( + select date_trunc('day', day_series)::date as past_date + from + generate_series + ( + {{ var("start_date") }}::timestamp, + ( + select max(most_recent_date_utc) + from most_recent_xe_date_per_curr_pair + )::timestamp, + '1 day'::interval + ) day_series + ), + projected_past_rates as ( + select + concat( + first_rate.from_currency, + first_rate.to_currency, + past_dates.past_date, + 'xe_inferred', + 'guess' + ) as id_exchange_rate, + first_rate.from_currency, + first_rate.to_currency, + first_rate.rate, + past_dates.past_date as rate_date_utc, + 'xe_inferred' as source, + 'guess' as rate_version, + now() as updated_at_utc + from past_dates + cross join first_xe_rate_per_curr_pair first_rate ) -- here we union actual stuff, future projected stuff and past assumed stuff select @@ -91,3 +124,14 @@ select fu.rate_version, fu.updated_at_utc from projected_future_rates fu +union +select + pa.id_exchange_rate, + pa.from_currency, + pa.to_currency, + pa.rate, + pa.rate_date_utc, + pa.source, + pa.rate_version, + pa.updated_at_utc +from projected_past_rates pa