-
Notifications
You must be signed in to change notification settings - Fork 2
/
amazon_ads__portfolio_report.sql
69 lines (59 loc) · 2.07 KB
/
amazon_ads__portfolio_report.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{{ config(enabled=fivetran_utils.enabled_vars(['ad_reporting__amazon_ads_enabled','amazon_ads__portfolio_history_enabled'])) }}
with report as (
select *
--use campaign report since portfolio report not provided
from {{ var('campaign_level_report') }}
),
account_info as (
select *
from {{ var('profile') }}
where _fivetran_deleted = False
),
portfolios as (
select *
from {{ var('portfolio_history') }}
where is_most_recent_record = True
),
campaigns as (
select *
from {{ var('campaign_history') }}
where is_most_recent_record = True
),
fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
account_info.country_code,
account_info.profile_id,
portfolios.portfolio_name,
portfolios.portfolio_id,
portfolios.budget_amount,
portfolios.budget_currency_code,
portfolios.budget_start_date,
portfolios.budget_end_date,
portfolios.budget_policy,
portfolios.in_budget,
portfolios.serving_status,
portfolios.state,
sum(report.cost) as cost,
sum(report.clicks) as clicks,
sum(report.impressions) as impressions,
sum(report.purchases_30_d) as purchases_30_d,
sum(report.sales_30_d) as sales_30_d
{{ amazon_ads_persist_pass_through_columns(pass_through_variable='amazon_ads__campaign_passthrough_metrics', identifier='report', transform='sum', coalesce_with=0, exclude_fields=['purchases_30_d','sales_30_d']) }}
from portfolios
left join campaigns
on campaigns.portfolio_id = portfolios.portfolio_id
and campaigns.source_relation = portfolios.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation
left join report
on report.campaign_id = campaigns.campaign_id
and report.source_relation = campaigns.source_relation
{{ dbt_utils.group_by(16) }}
)
select *
from fields