-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathamazon_ads__search_report.sql
80 lines (68 loc) · 1.95 KB
/
amazon_ads__search_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
70
71
72
73
74
75
76
77
78
79
80
{{ config(enabled=var('ad_reporting__amazon_ads_enabled', True)) }}
with report as (
select *
from {{ var('search_term_ad_keyword_report') }}
),
account_info as (
select *
from {{ var('profile') }}
where _fivetran_deleted = False
),
portfolios as (
select *
from {{ ref('int_amazon_ads__portfolio_history') }}
),
campaigns as (
select *
from {{ var('campaign_history') }}
where is_most_recent_record = True
),
ad_groups as (
select *
from {{ var('ad_group_history') }}
where is_most_recent_record = True
),
keywords as (
select *
from {{ var('keyword_history') }}
where is_most_recent_record = True
),
fields as (
select
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,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
report.keyword_id,
keywords.keyword_text,
keywords.match_type,
keywords.serving_status,
keywords.state,
report.search_term,
report.targeting,
sum(report.cost) as cost,
sum(report.clicks) as clicks,
sum(report.impressions) as impressions
{{ fivetran_utils.persist_pass_through_columns(pass_through_variable='amazon_ads__search_term_ad_keyword_passthrough_metrics', transform='sum') }}
from report
left join keywords
on keywords.keyword_id = report.keyword_id
left join ad_groups
on ad_groups.ad_group_id = report.ad_group_id
left join campaigns
on campaigns.campaign_id = report.campaign_id
left join portfolios
on portfolios.portfolio_id = campaigns.portfolio_id
left join account_info
on account_info.profile_id = campaigns.profile_id
{{ dbt_utils.group_by(18) }}
)
select *
from fields