You are on page 1of 2

{{ config(materialized='table', sort = ['yearmonth', 'next_id', 'marketplace_id'],

post_hook= 'GRANT SELECT ON {{this}} TO GROUP bbe_central') }}

with
monthly_exchange_rate as
(
SELECT source_currency_code, TO_CHAR("date", 'MM') as avg_month,
TO_CHAR("date", 'YYYY') as avg_year, AVG(exchange_rate) as exchange_rate,
cast(avg_year as int) * 100 + cast(avg_month as int) as yearmonth
FROM sftp_monthly_payment_reports.daily_exchange_rate der
GROUP BY source_currency_code, avg_month, avg_year
),
vals as
(
select
"date",exposure_group,exp_group,users_that_purchased,impression_reach,total_p1_impr
essions,total_p2_impressions,

total_p3_impressions,sp_purchases,sp_products_sale,sp_units_sold,dsp_total_product_
sales,dsp_total_units,

dsp_total_purchases,total_impressions,total_purchases,total_sale,total_units,next_i
d,marketplace_id,yearmonth,
'q1' as query_type from amc_analysis.triple_overlap_q1

union all

select
"date",exposure_group,exp_group,users_that_purchased,impression_reach,total_p1_impr
essions,total_p2_impressions,

total_p3_impressions,sp_purchases,sp_products_sale,sp_units_sold,dsp_total_product_
sales,dsp_total_units,

dsp_total_purchases,total_impressions,total_purchases,total_sale,total_units,next_i
d,marketplace_id,yearmonth,
'q2' as query_type from amc_analysis.triple_overlap_q2

union all

select
"date",exposure_group,exp_group,users_that_purchased,impression_reach,total_p1_impr
essions,total_p2_impressions,

total_p3_impressions,sp_purchases,sp_products_sale,sp_units_sold,dsp_total_product_
sales,dsp_total_units,

dsp_total_purchases,total_impressions,total_purchases,total_sale,total_units,next_i
d,marketplace_id,yearmonth,
'q3' as query_type from amc_analysis.triple_overlap_q3
)
select vals.*,
(CASE when amcm.amazon_marketplace_name<>'US' then
(dsp_total_product_sales/mr.exchange_rate) else dsp_total_product_sales end)
as dsp_total_product_sales_converted_usd,
(CASE when amcm.amazon_marketplace_name<>'US' then
(sp_products_sale/mr.exchange_rate) else sp_products_sale end) as
sp_products_sale_converted_usd,
(CASE when amcm.amazon_marketplace_name<>'US' then (total_sale/mr.exchange_rate)
else total_sale end) as total_sale_converted_usd,
m."bp name" as bp_name from vals
inner join dbt_bbe.mapping m on vals.next_id = m.next_id and vals.marketplace_id =
m.marketplace_id
and vals.marketplace_id = m.marketplace_id
inner join sftp_monthly_payment_reports.amazon_marketplace_currency_mapping amcm
on vals.marketplace_id = amcm.marketplace_id
left join monthly_exchange_rate mr on
vals.yearmonth = mr.yearmonth and amcm.currency_code = mr.source_currency_code

You might also like