This SQL query combines data from two staging tables: basic ad report and account history. It joins these tables on account ID and source relation, filtering for the most recent account records. The query then aggregates ad performance metrics (clicks, impressions, spend) at the account and date level, while including various account details. The result is a comprehensive daily account-level report of Facebook ad performance with associated account information.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
accounts.account_status,
accounts.business_country_code,
accounts.created_at,
accounts.currency,
accounts.timezone_name,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9
)
SELECT
*
FROM joined
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
date_day | date | The date of the performance. |
account_id | bigint | The ID of the related account. |
account_name | text | The name of the related account. |
clicks | bigint | The number of clicks the ad had on the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
account_status | text | None |
business_country_code | text | None |
created_at | timestamp without time zone | None |
currency | text | None |
timezone_name | text | None |
This SQL query integrates data from multiple Facebook Ads-related tables (ad reports, accounts, campaigns, ad sets, and ads) to create a comprehensive ad performance report. It joins these tables based on various IDs and the source relation, filters for the most recent records in history tables, and aggregates metrics such as clicks, impressions, and spend. The result is a detailed view of ad performance across different levels of the ad hierarchy (account, campaign, ad set, and ad).
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_sets AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_set_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_sets.ad_set_id,
ad_sets.ad_set_name,
ads.ad_id,
ads.ad_name,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN ads
ON report.ad_id = ads.ad_id AND report.source_relation = ads.source_relation
LEFT JOIN campaigns
ON ads.campaign_id = campaigns.campaign_id
AND ads.source_relation = campaigns.source_relation
LEFT JOIN ad_sets
ON ads.ad_set_id = ad_sets.ad_set_id
AND ads.source_relation = ad_sets.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
)
SELECT
*
FROM joined
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
date_day | date | The date of the performance. |
account_id | bigint | The ID of the related account. |
account_name | text | The name of the related account. |
campaign_id | bigint | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
ad_set_id | bigint | The ID of the related ad set. |
ad_set_name | text | The name of the related ad set. |
ad_id | bigint | The ID of the related ad. |
ad_name | text | The name of the related ad. |
clicks | bigint | The number of clicks the ad had on the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
This SQL query integrates data from multiple Facebook Ads related tables (basic ad reports, account history, campaign history, ad set history, and ad history) to create a comprehensive ad set report. It filters for the most recent records in the history tables, joins these tables based on various ID fields and source relations, and then aggregates metrics like clicks, impressions, and spend at the ad set level. The result is a detailed report that includes account, campaign, and ad set information along with performance metrics.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_sets AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_set_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_sets.ad_set_id,
ad_sets.ad_set_name,
ad_sets.start_at,
ad_sets.end_at,
ad_sets.bid_strategy,
ad_sets.daily_budget,
ad_sets.budget_remaining,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN ads
ON report.ad_id = ads.ad_id AND report.source_relation = ads.source_relation
LEFT JOIN campaigns
ON ads.campaign_id = campaigns.campaign_id
AND ads.source_relation = campaigns.source_relation
LEFT JOIN ad_sets
ON ads.ad_set_id = ad_sets.ad_set_id
AND ads.source_relation = ad_sets.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
)
SELECT
*
FROM joined
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
date_day | date | The date of the performance. |
account_id | bigint | The ID of the related account. |
account_name | text | The name of the related account. |
campaign_id | bigint | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
ad_set_id | bigint | The ID of the related ad set. |
ad_set_name | text | The name of the related ad set. |
clicks | bigint | The number of clicks the ad had on the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
start_at | timestamp without time zone | None |
end_at | timestamp without time zone | None |
bid_strategy | text | None |
daily_budget | integer | None |
budget_remaining | integer | None |
This SQL query combines data from multiple Facebook Ads-related tables to create a comprehensive campaign report. It joins account, campaign, and ad data with the basic ad report, filtering for the most recent records in history tables. The query then aggregates clicks, impressions, and spend data at the campaign level, providing a detailed view of campaign performance along with account and campaign details.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
campaigns.campaign_id,
campaigns.campaign_name,
campaigns.start_at,
campaigns.end_at,
campaigns.status,
campaigns.daily_budget,
campaigns.lifetime_budget,
campaigns.budget_remaining,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN ads
ON report.ad_id = ads.ad_id AND report.source_relation = ads.source_relation
LEFT JOIN campaigns
ON ads.campaign_id = campaigns.campaign_id
AND ads.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12
)
SELECT
*
FROM joined
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
date_day | date | The date of the performance. |
account_id | bigint | The ID of the related account. |
account_name | text | The name of the related account. |
campaign_id | bigint | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
clicks | bigint | The number of clicks the ad had on the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
start_at | timestamp without time zone | None |
end_at | timestamp without time zone | None |
status | text | None |
daily_budget | integer | None |
lifetime_budget | integer | None |
budget_remaining | double precision | None |
This SQL query integrates data from various Facebook Ads-related tables to create a comprehensive URL report. It joins information from ad accounts, campaigns, ad sets, ads, and creatives, focusing on URLs associated with the ads. The query filters out records where the URL is null, aggregates metrics like clicks, impressions, and spend, and groups the results by various dimensions including date, account, campaign, ad set, ad, and URL-related fields.
IntegrationFilteringAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad
), creatives AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads.int_facebook_ads__creative_history
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_sets AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_set_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_sets.ad_set_id,
ad_sets.ad_set_name,
ads.ad_id,
ads.ad_name,
creatives.creative_id,
creatives.creative_name,
creatives.base_url,
creatives.url_host,
creatives.url_path,
creatives.utm_source,
creatives.utm_medium,
creatives.utm_campaign,
creatives.utm_content,
creatives.utm_term,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN ads
ON report.ad_id = ads.ad_id AND report.source_relation = ads.source_relation
LEFT JOIN creatives
ON ads.creative_id = creatives.creative_id
AND ads.source_relation = creatives.source_relation
LEFT JOIN ad_sets
ON ads.ad_set_id = ad_sets.ad_set_id
AND ads.source_relation = ad_sets.source_relation
LEFT JOIN campaigns
ON ads.campaign_id = campaigns.campaign_id
AND ads.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
WHERE
NOT creatives.url IS NULL
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20
)
SELECT
*
FROM joined
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
date_day | date | The date of the performance. |
account_id | bigint | The ID of the related account. |
account_name | text | The name of the related account. |
campaign_id | bigint | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
ad_set_id | bigint | The ID of the related ad set. |
ad_set_name | text | The name of the related ad set. |
ad_id | bigint | The ID of the related ad. |
ad_name | text | The name of the related ad. |
creative_id | bigint | The ID of the related creative. |
creative_name | text | The name of the related creative. |
base_url | text | The base URL of the ad, extracted from the page_link and template_page_link. |
url_host | text | The URL host of the ad, extracted from the page_link and template_page_link. |
url_path | text | The URL path of the ad, extracted from the page_link and template_page_link. |
utm_source | text | The utm_source parameter of the ad, extracted from the page_link and template_page_link. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the page_link and template_page_link. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the page_link and template_page_link. |
utm_content | text | The utm_content parameter of the ad, extracted from the page_link and template_page_link. |
utm_term | text | The utm_term parameter of the ad, extracted from the page_link and template_page_link. |
clicks | bigint | The number of clicks the ad had on the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
This SQL query processes Facebook ads creative history data, focusing on URL tags. It starts by selecting the most recent records, then filters for non-null URL tags. The query then parses the JSON-formatted URL tags, flattens the structure, and extracts key-value pairs along with their types from the URL tags.
FilteringCleaningFeaturizationwith base as (
select *
from TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__creative_history
where is_most_recent_record = true
),
required_fields as (
select
source_relation,
_fivetran_id,
creative_id,
url_tags
from base
where url_tags is not null
),
cleaned_fields as (
select
source_relation,
_fivetran_id,
creative_id,
parse_json(url_tags) as url_tags
from required_fields
where url_tags is not null
),
fields as (
select
source_relation,
_fivetran_id,
creative_id,
url_tags.value:key::string as key,
url_tags.value:value::string as value,
url_tags.value:type::string as type
from cleaned_fields,
lateral flatten( input => url_tags ) as url_tags
)
select *
from fields
This SQL query processes Facebook ad creative data, focusing on URL-related information. It starts by selecting the most recent records from the creative history, then joins this with URL tag data. The query extensively cleans and extracts various components of URLs (such as base URL, host, and path) and UTM parameters. It also handles cases where UTM parameters might be directly in the URL or in a separate tags table. The result is a comprehensive view of each creative's URL structure and associated tracking parameters.
FilteringCleaningFeaturizationIntegrationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__creative_history
WHERE
is_most_recent_record = TRUE
), url_tags AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads.facebook_ads__url_tags
), url_tags_pivoted AS (
SELECT
source_relation,
_fivetran_id,
creative_id,
MIN(CASE WHEN key = 'utm_source' THEN value END) AS utm_source,
MIN(CASE WHEN key = 'utm_medium' THEN value END) AS utm_medium,
MIN(CASE WHEN key = 'utm_campaign' THEN value END) AS utm_campaign,
MIN(CASE WHEN key = 'utm_content' THEN value END) AS utm_content,
MIN(CASE WHEN key = 'utm_term' THEN value END) AS utm_term
FROM url_tags
GROUP BY
1,
2,
3
), fields AS (
SELECT
base.source_relation,
base._fivetran_id,
base.creative_id,
base.account_id,
base.creative_name,
COALESCE(page_link, template_page_link) AS url,
SPLIT_PART(COALESCE(page_link, template_page_link), '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(
REPLACE(
REPLACE(COALESCE(page_link, template_page_link), 'android-app://', ''),
'http://',
''
),
'https://',
''
),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', '')
) - COALESCE(
NULLIF(
STR_POSITION(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', ''),
'/'
),
0
),
STR_POSITION(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', ''),
'?'
) - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', ''),
LENGTH(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', '')
) - COALESCE(
NULLIF(
STR_POSITION(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', ''),
'/'
),
0
),
STR_POSITION(
REPLACE(REPLACE(COALESCE(page_link, template_page_link), 'http://', ''), 'https://', ''),
'?'
) - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
COALESCE(
url_tags_pivoted.utm_source,
NULLIF(
SPLIT_PART(SPLIT_PART(COALESCE(page_link, template_page_link), 'utm_source=', 2), '&', 1),
''
)
) AS utm_source,
COALESCE(
url_tags_pivoted.utm_medium,
NULLIF(
SPLIT_PART(SPLIT_PART(COALESCE(page_link, template_page_link), 'utm_medium=', 2), '&', 1),
''
)
) AS utm_medium,
COALESCE(
url_tags_pivoted.utm_campaign,
NULLIF(
SPLIT_PART(SPLIT_PART(COALESCE(page_link, template_page_link), 'utm_campaign=', 2), '&', 1),
''
)
) AS utm_campaign,
COALESCE(
url_tags_pivoted.utm_content,
NULLIF(
SPLIT_PART(SPLIT_PART(COALESCE(page_link, template_page_link), 'utm_content=', 2), '&', 1),
''
)
) AS utm_content,
COALESCE(
url_tags_pivoted.utm_term,
NULLIF(
SPLIT_PART(SPLIT_PART(COALESCE(page_link, template_page_link), 'utm_term=', 2), '&', 1),
''
)
) AS utm_term
FROM base
LEFT JOIN url_tags_pivoted
ON base._fivetran_id = url_tags_pivoted._fivetran_id
AND base.source_relation = url_tags_pivoted.source_relation
AND base.creative_id = url_tags_pivoted.creative_id
)
SELECT
*
FROM fields
Name | Type | Comment |
---|---|---|
source_relation | text | None |
_fivetran_id | text | None |
creative_id | bigint | None |
account_id | bigint | None |
creative_name | text | None |
url | character varying | None |
base_url | text | None |
url_host | text | None |
url_path | text | None |
utm_source | text | None |
utm_medium | text | None |
utm_campaign | text | None |
utm_content | text | None |
utm_term | text | None |
This SQL query stages data from a Facebook Ads account history source table. It casts fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each account. The query also includes a mechanism to generate a surrogate key when the natural key (id) is missing.
CleaningDeduplicationFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__account_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS account_status,
CAST(NULL AS TEXT) AS business_country_code,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS TEXT) AS timezone_name,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS BIGINT) AS account_id,
_fivetran_synced,
name AS account_name,
account_status,
business_country_code,
created_time AS created_at,
currency,
timezone_name,
CASE
WHEN id IS NULL AND _fivetran_synced IS NULL
THEN ROW_NUMBER() OVER (PARTITION BY source_relation ORDER BY source_relation)
ELSE ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC)
END = 1 AS is_most_recent_record
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
account_id | bigint | The ID of the ad account. |
account_name | text | Name of the account. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. All records should have this value set to True given we filter on it. |
_fivetran_synced | timestamp without time zone | When the record was last synced by Fivetran. |
account_status | text | Current status of account. |
business_country_code | text | Country code of business associated to account. |
created_at | timestamp without time zone | The time account was created. |
currency | text | Currency associated with account. |
timezone_name | text | Timezone associated with account. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT. It's likely used as a placeholder or template for further operations in a dbt (data build tool) model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
name | text | None |
_fivetran_synced | timestamp without time zone | None |
This SQL query performs several operations on the Facebook Ads data. It starts by selecting all columns from a temporary staging table, then defines a set of fields with specific data types. The final select statement casts various ID fields to BIGINT, renames some columns, and adds an 'is_most_recent_record' flag. This flag is determined using a ROW_NUMBER() function, which helps in identifying the most recent record for each unique ad (or source relation if id is null).
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS updated_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS INT) AS ad_set_id,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS creative_id,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
updated_time AS updated_at,
CAST(id AS BIGINT) AS ad_id,
name AS ad_name,
CAST(account_id AS BIGINT) AS account_id,
CAST(ad_set_id AS BIGINT) AS ad_set_id,
CAST(campaign_id AS BIGINT) AS campaign_id,
CAST(creative_id AS BIGINT) AS creative_id,
CASE
WHEN id IS NULL AND updated_time IS NULL
THEN ROW_NUMBER() OVER (PARTITION BY source_relation ORDER BY source_relation)
ELSE ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_time DESC)
END = 1 AS is_most_recent_record
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
ad_id | bigint | The ID of this ad. |
account_id | bigint | The ID of the ad account that this ad belongs to. |
ad_set_id | bigint | ID of the ad set that contains the ad. |
campaign_id | bigint | Ad campaign that contains this ad. |
creative_id | bigint | The ID of the ad creative to be used by this ad. |
ad_name | text | Name of the ad. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. All records should have this value set to True given we filter on it. |
updated_at | timestamp without time zone | The timestamp of the last update of a record. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, containing NULL values. The LIMIT 0 clause ensures no rows are returned. This query is likely used as a placeholder or template for generating a schema without actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
account_id | bigint | None |
ad_set_id | bigint | None |
campaign_id | bigint | None |
creative_id | bigint | None |
name | text | None |
_fivetran_synced | timestamp without time zone | None |
updated_time | timestamp without time zone | None |
This SQL query performs several transformations on data from a Facebook Ads source table. It casts various fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each ad set. The query also includes logic to handle cases where 'id' and 'updated_time' might be null.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__ad_set_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS updated_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS TEXT) AS bid_strategy,
CAST(NULL AS INT) AS daily_budget,
CAST(NULL AS INT) AS budget_remaining,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
updated_time AS updated_at,
CAST(id AS BIGINT) AS ad_set_id,
name AS ad_set_name,
CAST(account_id AS BIGINT) AS account_id,
CAST(campaign_id AS BIGINT) AS campaign_id,
start_time AS start_at,
end_time AS end_at,
bid_strategy,
daily_budget,
budget_remaining,
status,
CASE
WHEN id IS NULL AND updated_time IS NULL
THEN ROW_NUMBER() OVER (PARTITION BY source_relation ORDER BY source_relation)
ELSE ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_time DESC)
END = 1 AS is_most_recent_record
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
ad_set_id | bigint | The ID of the ad set. |
account_id | bigint | The ID of the ad account that this ad set belongs to. |
campaign_id | bigint | Ad campaign that contains this ad set. |
ad_set_name | text | The name of the ad set. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. All records should have this value set to True given we filter on it. |
updated_at | timestamp without time zone | The timestamp of the last update of a record. |
start_at | timestamp without time zone | Timestamp of designated ad set start time. |
end_at | timestamp without time zone | Timestamp of designated ad set end time. |
bid_strategy | text | Bid strategy values are - 'LOWEST_COST_WITHOUT_CAP', 'LOWEST_COST_WITH_BID_CAP', 'COST_CAP', 'LOWEST_COST_WITH_MIN_ROAS'. |
daily_budget | integer | Daily budget of ad set. |
budget_remaining | integer | Remaining budget of ad set. |
status | text | Status values are - 'ACTIVE', 'PAUSED', 'DELETED', 'ARCHIVED'. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, which is set to NULL. The query is limited to 0 rows, effectively returning no data. This type of query is often used as a placeholder or template in data modeling tools like dbt (data build tool) to define the structure of a staging table without actually populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
account_id | bigint | None |
campaign_id | bigint | None |
name | text | None |
_fivetran_synced | timestamp without time zone | None |
updated_time | timestamp without time zone | None |
This SQL query performs data type casting and column renaming on a Facebook ads dataset. It starts by selecting all columns from a temporary staging table, then defines a set of fields with specific data types. Finally, it casts some columns to different data types, renames a few columns, and applies a COALESCE function to handle potential NULL values in the 'clicks' column.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__basic_ad_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS ad_id,
CAST(NULL AS TEXT) AS ad_name,
CAST(NULL AS TEXT) AS adset_name,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS inline_link_clicks,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS INT) AS reach,
CAST(NULL AS FLOAT) AS frequency,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_id AS BIGINT) AS ad_id,
ad_name,
adset_name AS ad_set_name,
date AS date_day,
CAST(account_id AS BIGINT) AS account_id,
impressions,
COALESCE(inline_link_clicks, 0) AS clicks,
spend,
reach,
frequency
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
ad_id | bigint | The ID of the ad the report relates to. |
ad_name | text | Name of the ad the report relates to. |
ad_set_name | text | Name of the ad set the report relates to. |
date_day | date | The date of the reported performance. |
account_id | bigint | The ID of the ad account that this ad belongs to. |
impressions | integer | The number of impressions the ad had on the given day. |
clicks | integer | The number of clicks the ad had on the given day. |
spend | double precision | The spend on the ad in the given day. |
reach | integer | The number of people who saw any content from your Page or about your Page. This metric is estimated. |
frequency | double precision | The average number of times each person saw your ad; it is calculated as impressions divided by reach. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, which is set to NULL. The query is limited to 0 rows, effectively returning no data. This type of query is often used as a placeholder or for testing purposes, particularly in data modeling or ETL processes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_id | bigint | None |
date | date | None |
account_id | bigint | None |
impressions | integer | None |
inline_link_clicks | integer | None |
spend | double precision | None |
This SQL query stages data from a temporary Facebook Ads campaign history table. It casts columns to specific data types, renames some columns, and adds a flag to identify the most recent record for each campaign. The query also includes a source relation column, though it's set to an empty string in this example.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS updated_time,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS TIMESTAMP) AS stop_time,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS INT) AS daily_budget,
CAST(NULL AS INT) AS lifetime_budget,
CAST(NULL AS FLOAT) AS budget_remaining,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
updated_time AS updated_at,
created_time AS created_at,
CAST(account_id AS BIGINT) AS account_id,
CAST(id AS BIGINT) AS campaign_id,
name AS campaign_name,
start_time AS start_at,
stop_time AS end_at,
status,
daily_budget,
lifetime_budget,
budget_remaining,
CASE
WHEN id IS NULL AND updated_time IS NULL
THEN ROW_NUMBER() OVER (PARTITION BY source_relation ORDER BY source_relation)
ELSE ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_time DESC)
END = 1 AS is_most_recent_record
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
campaign_id | bigint | The ID of the campaign. |
account_id | bigint | The ID of the ad account that this campaign belongs to. |
campaign_name | text | The name of the campaign. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. All records should have this value set to True given we filter on it. |
updated_at | timestamp without time zone | The timestamp of the last update of a record. |
created_at | timestamp without time zone | The time the campaign was created. |
start_at | timestamp without time zone | Timestamp of designated campaign start time. |
end_at | timestamp without time zone | Timestamp of designated campaign end time. |
daily_budget | integer | Daily budget of campaign. |
budget_remaining | double precision | Remaining budget of campaign. |
lifetime_budget | integer | Lifetime budget of the campaign. |
status | text | Status values are - 'ACTIVE', 'PAUSED', 'DELETED', 'ARCHIVED'. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, which is set to NULL. The query is limited to 0 rows, effectively creating a schema-only representation of the table without any actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
account_id | bigint | None |
name | text | None |
_fivetran_synced | timestamp without time zone | None |
updated_time | timestamp without time zone | None |
This SQL query performs several data transformations on the Facebook Ads creative history data. It starts by selecting all columns from a temporary table, then casts various fields to specific data types. The query renames some columns, such as changing 'id' to 'creative_id'. It also adds a 'source_relation' column and creates an 'is_most_recent_record' flag using window functions to identify the most recent record for each creative ID. Finally, it selects all columns from the transformed data.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_facebook_ads_source.stg_facebook_ads__creative_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS page_link,
CAST(NULL AS TEXT) AS template_page_link,
CAST(NULL AS TEXT) AS url_tags,
CAST(NULL AS TEXT) AS asset_feed_spec_link_urls,
CAST(NULL AS TEXT) AS object_story_link_data_child_attachments,
CAST(NULL AS TEXT) AS object_story_link_data_caption,
CAST(NULL AS TEXT) AS object_story_link_data_description,
CAST(NULL AS TEXT) AS object_story_link_data_link,
CAST(NULL AS TEXT) AS object_story_link_data_message,
CAST(NULL AS TEXT) AS template_app_link_spec_android,
CAST(NULL AS TEXT) AS template_app_link_spec_ios,
CAST(NULL AS TEXT) AS template_app_link_spec_ipad,
CAST(NULL AS TEXT) AS template_app_link_spec_iphone,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
_fivetran_id,
_fivetran_synced,
CAST(id AS BIGINT) AS creative_id,
CAST(account_id AS BIGINT) AS account_id,
name AS creative_name,
page_link,
template_page_link,
url_tags,
asset_feed_spec_link_urls,
object_story_link_data_child_attachments,
object_story_link_data_caption,
object_story_link_data_description,
object_story_link_data_link,
object_story_link_data_message,
template_app_link_spec_ios,
template_app_link_spec_ipad,
template_app_link_spec_android,
template_app_link_spec_iphone,
CASE
WHEN id IS NULL AND _fivetran_synced IS NULL
THEN ROW_NUMBER() OVER (PARTITION BY source_relation ORDER BY source_relation)
ELSE ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC)
END = 1 AS is_most_recent_record
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | text | The source of the record if the unioning functionality is being used. If not this field will be empty. |
_fivetran_id | text | Unique record identifier |
_fivetran_synced | timestamp without time zone | When the record was last synced by Fivetran. |
creative_id | bigint | Unique ID for an ad creative. |
account_id | bigint | Ad account ID for the account this ad creative belongs to. |
creative_name | text | Name of this ad creative as seen in the ad account's library. |
url_tags | text | A set of query string parameters which will replace or be appended to urls clicked from page post ads, message of the post, and canvas app install creatives only. |
is_most_recent_record | boolean | Indicates whether a record is the most recent version of that record. |
page_link | character varying | Link for the page. |
template_page_link | character varying | URL destination of Facebook dynamic ads. |
asset_feed_spec_link_urls | text | Link to the asset feed spec |
object_story_link_data_child_attachments | text | Link of the object story child attachments |
object_story_link_data_caption | text | Link of the object story caption |
object_story_link_data_description | text | Link of the object story description |
object_story_link_data_link | text | Link of the object story link |
object_story_link_data_message | text | Link of the object story message |
template_app_link_spec_ios | text | Link of the object story spec for ios |
template_app_link_spec_ipad | text | Link of the template app spec for ipad |
template_app_link_spec_android | text | Link of the template app for android |
template_app_link_spec_iphone | text | Link of the template app for iphone |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, initialized to NULL. The query is likely used as a placeholder or template for further development, or to establish the structure of a table without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
page_link | character varying | None |
template_page_link | character varying | None |
id | bigint | None |
account_id | bigint | None |
name | text | None |
url_tags | text | None |
_fivetran_synced | timestamp without time zone | None |
asset_feed_spec_link_urls | text | None |
object_story_link_data_child_attachments | text | None |
object_story_link_data_caption | text | None |
object_story_link_data_description | text | None |
object_story_link_data_link | text | None |
object_story_link_data_message | text | None |
template_app_link_spec_ios | text | None |
_fivetran_id | text | None |