past working
This commit is contained in:
parent
3431f46502
commit
9d11aa165b
1 changed files with 47 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue