This SQL query integrates advertising data from multiple platforms (e.g., Twitter, Facebook, Google, Microsoft, Apple, LinkedIn, Pinterest, Snapchat, TikTok, Amazon, and Reddit) into a single unified report. It standardizes the data structure across platforms, casting data types and renaming columns for consistency. The query then aggregates the data by source, date, platform, account ID, and account name, summing up clicks, impressions, and spend.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__account_report AS (
WITH twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__account_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__account_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__account_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__account_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__organization_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__account_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__advertiser_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__account_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__advertiser_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__account_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__account_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__account_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates ad performance data from multiple advertising platforms (e.g., Google Ads, Facebook Ads, LinkedIn Ads) into a single unified report. It standardizes data formats across platforms, aligning column names and data types. The query then aggregates the data by date, platform, account, campaign, and ad group, calculating total clicks, impressions, and spend.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__ad_group_report AS (
WITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__ad_group_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__ad_group_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__ad_group_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__campaign_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__ad_set_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__ad_group_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(ad_squad_id AS TEXT) AS ad_group_id,
CAST(ad_squad_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__ad_squad_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__ad_group_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__line_item_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__ad_group_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__ad_group_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__ad_group_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
ad_group_id,
ad_group_name,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates ad reporting data from multiple advertising platforms (such as Google Ads, Microsoft Ads, Apple Search Ads, Facebook Ads, etc.) into a single unified view. It standardizes the column names and data types across all platforms, performs type casting for consistency, and then aggregates the data by summing clicks, impressions, and spend for each unique combination of date, platform, account, campaign, ad group, and ad.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__ad_report AS (
WITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__ad_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__ad_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__ad_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__ad_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(creative_id AS TEXT) AS ad_id,
CAST(NULL AS TEXT) AS ad_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__creative_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(pin_promotion_id AS TEXT) AS ad_id,
CAST(pin_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__pin_promotion_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__ad_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__ad_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(promoted_tweet_id AS TEXT) AS ad_id,
CAST(tweet_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__promoted_tweet_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(advertised_asin AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__ad_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__ad_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__ad_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
ad_group_id,
ad_group_name,
ad_id,
ad_name,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_id | text | The ID representing the ad, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates campaign report data from multiple advertising platforms (such as Twitter, Facebook, Google, Microsoft, Apple, LinkedIn, Pinterest, Snapchat, TikTok, Amazon, and Reddit) into a single unified dataset. It standardizes the column names and data types across all platforms, performs type casting for consistency, and then aggregates the data by source, date, platform, account, and campaign, summing up clicks, impressions, and spend.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__campaign_report AS (
WITH twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__campaign_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__campaign_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__campaign_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__campaign_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__campaign_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__campaign_group_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__campaign_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__campaign_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__campaign_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__campaign_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__campaign_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__campaign_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates keyword report data from multiple advertising platforms (Amazon Ads, Apple Search Ads, Google Ads, Microsoft Ads, Pinterest Ads, and Twitter Ads) into a single unified table. It standardizes column names and data types across platforms, performs type casting for consistency, and then aggregates the data by various dimensions such as date, platform, account, campaign, ad group, and keyword information. The query calculates total clicks, impressions, and spend for each unique combination of these dimensions.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__keyword_report AS (
WITH apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__keyword_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(criterion_id AS TEXT) AS keyword_id,
CAST(keyword_match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__keyword_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_name AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__keyword_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_value AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__keyword_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(NULL AS TEXT) AS keyword_match_type,
CAST(keyword AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__keyword_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__keyword_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM twitter_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__keyword_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
ad_group_id,
ad_group_name,
keyword_id,
keyword_text,
keyword_match_type,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
keyword_text | text | The keyword text. |
keyword_match_type | text | The keyword match type associated with this record. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates search report data from three different advertising platforms (Microsoft Ads, Apple Search Ads, and Amazon Ads) into a unified format. It standardizes column names and data types across the platforms, combines the data using UNION ALL, and then aggregates the results by various dimensions such as date, platform, account, campaign, ad group, keyword, and search query. The final output includes summed metrics for clicks, impressions, and spend.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__search_report AS (
WITH microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_name AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_query AS TEXT) AS search_query,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__search_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_term_text AS TEXT) AS search_query,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__search_term_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_term AS TEXT) AS search_query,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__search_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM microsoft_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__search_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
ad_group_id,
ad_group_name,
keyword_id,
keyword_text,
search_query,
search_match_type,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
keyword_text | text | The keyword text. |
search_query | text | The search query text. |
search_match_type | text | The search match type associated with this record. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates ad reporting data from multiple advertising platforms (Facebook, Google, LinkedIn, Microsoft, Pinterest, Snapchat, TikTok, Twitter, and Reddit) into a single unified report. It standardizes the column names and data types across all platforms, combines the data using UNION ALL, and then aggregates the metrics (clicks, impressions, and spend) by various dimensions such as date, platform, account, campaign, ad group, and URL components.
IntegrationCleaningAggregationWITH __dbt__cte__int_ad_reporting__url_report AS (
WITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_google_ads.google_ads__url_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__url_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_facebook_ads.facebook_ads__url_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__url_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_pinterest.pinterest_ads__url_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(ad_squad_id AS TEXT) AS ad_group_id,
CAST(ad_squad_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__url_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__url_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_twitter_ads.twitter_ads__url_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_reddit_ads.reddit_ads__url_report
), unioned AS (
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
), base AS (
SELECT
*
FROM __dbt__cte__int_ad_reporting__url_report
), aggregated AS (
SELECT
source_relation,
date_day,
platform,
account_id,
account_name,
campaign_id,
campaign_name,
ad_group_id,
ad_group_name,
base_url,
url_host,
url_path,
utm_source,
utm_medium,
utm_campaign,
utm_content,
utm_term,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend
FROM base
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17
)
SELECT
*
FROM aggregated
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 report. |
platform | text | The ad platform associated with this record. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
base_url | text | The base url of the ad. |
url_host | text | The URL host of the ad. |
url_path | text | The URL path of the ad. |
utm_source | text | The utm_source parameter of the ad. |
utm_medium | text | The utm_medium parameter of the ad. |
utm_campaign | text | The utm_campaign parameter of the ad. |
utm_content | text | The utm_content parameter of the ad. |
utm_term | text | The utm_term parameter of the ad. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates advertising data from multiple platforms (such as Twitter, Facebook, Google, Microsoft, Apple, LinkedIn, Pinterest, Snapchat, TikTok, Amazon, and Reddit) into a single unified table. It standardizes the data structure across all platforms by selecting common fields, casting them to consistent data types, and renaming some platform-specific fields to match a common schema. The query uses Common Table Expressions (CTEs) for each platform and then unions all the data together.
IntegrationCleaningWITH twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__account_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__account_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__account_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__account_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__organization_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__account_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__advertiser_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__account_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__advertiser_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__account_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__account_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query combines ad reporting data from multiple advertising platforms (including Amazon, Apple Search, Facebook, Google, LinkedIn, Microsoft, Pinterest, Snapchat, TikTok, Twitter, and Reddit) into a unified format. It standardizes column names, data types, and structures across these diverse sources, creating a consolidated view of ad performance metrics such as clicks, impressions, and spend across different platforms, accounts, campaigns, and ad groups.
CleaningIntegrationWITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__ad_group_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__ad_group_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__ad_group_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__campaign_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__ad_set_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__ad_group_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(ad_squad_id AS TEXT) AS ad_group_id,
CAST(ad_squad_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__ad_squad_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__ad_group_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__line_item_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__ad_group_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__ad_group_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query integrates ad reporting data from multiple advertising platforms (such as Google Ads, Microsoft Ads, Apple Search Ads, Facebook Ads, LinkedIn Ads, Pinterest Ads, Snapchat Ads, TikTok Ads, Twitter Ads, Amazon Ads, and Reddit Ads) into a unified format. It standardizes column names and data types across all platforms, ensuring consistency in the final output. The query uses CTEs to transform data from each platform separately before combining them all using UNION ALL operations.
IntegrationCleaningWITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__ad_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__ad_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__ad_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__ad_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(creative_id AS TEXT) AS ad_id,
CAST(NULL AS TEXT) AS ad_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__creative_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(pin_promotion_id AS TEXT) AS ad_id,
CAST(pin_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__pin_promotion_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__ad_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__ad_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(promoted_tweet_id AS TEXT) AS ad_id,
CAST(tweet_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__promoted_tweet_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(advertised_asin AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__ad_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_name AS TEXT) AS ad_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__ad_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query integrates campaign report data from multiple advertising platforms (e.g., Twitter, Facebook, Google, Microsoft, Apple Search, LinkedIn, Pinterest, Snapchat, TikTok, Amazon, and Reddit) into a unified dataset. It standardizes the column names and data types across all platforms, ensuring consistency in the final output. The query uses CTEs to transform and union data from each platform, creating a comprehensive view of advertising campaign performance across various channels.
IntegrationCleaningWITH twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__campaign_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_facebook_ads.facebook_ads__campaign_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__campaign_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__campaign_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__campaign_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__campaign_group_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__campaign_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__campaign_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__campaign_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__campaign_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_reddit_ads.reddit_ads__campaign_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query integrates keyword report data from multiple advertising platforms (Amazon Ads, Apple Search Ads, Google Ads, Microsoft Ads, Pinterest Ads, and Twitter Ads) into a single unified table. It standardizes column names and data types across all platforms, ensuring consistency in the final output. The query uses Common Table Expressions (CTEs) to select and transform data from each platform separately before combining them using UNION ALL.
IntegrationCleaningWITH apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__keyword_report
), google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(criterion_id AS TEXT) AS keyword_id,
CAST(keyword_match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_google_ads.google_ads__keyword_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_name AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__keyword_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_value AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_pinterest.pinterest_ads__keyword_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(NULL AS TEXT) AS keyword_match_type,
CAST(keyword AS TEXT) AS keyword_text,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_twitter_ads.twitter_ads__keyword_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(match_type AS TEXT) AS keyword_match_type,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__keyword_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM twitter_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query combines search report data from three different advertising platforms (Microsoft Ads, Apple Search Ads, and Amazon Ads) into a unified format. It standardizes column names and data types across the platforms, ensuring consistency in the final output. The query uses CTEs to transform each platform's data separately before combining them using UNION ALL.
IntegrationCleaningWITH microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_name AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_query AS TEXT) AS search_query,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__search_report
), apple_search_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('apple_search_ads' AS TEXT) AS platform,
CAST(organization_id AS TEXT) AS account_id,
CAST(organization_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(taps AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_term_text AS TEXT) AS search_query,
CAST(spend AS FLOAT) AS spend
FROM TEST.PUBLIC_apple_search_ads.apple_search_ads__search_term_report
), amazon_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('amazon_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(keyword_id AS TEXT) AS keyword_id,
CAST(keyword_text AS TEXT) AS keyword_text,
CAST(match_type AS TEXT) AS search_match_type,
CAST(search_term AS TEXT) AS search_query,
CAST(cost AS FLOAT) AS spend
FROM TEST.PUBLIC_amazon_ads.amazon_ads__search_report
), unioned AS (
SELECT
*
FROM amazon_ads
UNION ALL
SELECT
*
FROM apple_search_ads
UNION ALL
SELECT
*
FROM microsoft_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query combines ad reporting data from multiple advertising platforms (Facebook, Google, LinkedIn, Microsoft, Pinterest, Snapchat, TikTok, Twitter, and Reddit) into a single unified table. It standardizes the column names and data types across all platforms, ensuring consistency in the output. The query uses Common Table Expressions (CTEs) to select and transform data from each platform's source table, and then unions all these CTEs together to create a comprehensive ad reporting dataset.
IntegrationCleaningWITH google_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('google_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_google_ads.google_ads__url_report
), microsoft_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('microsoft_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_microsoft_ads.microsoft_ads__url_report
), facebook_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('facebook_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(ad_set_id AS TEXT) AS ad_group_id,
CAST(ad_set_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_facebook_ads.facebook_ads__url_report
), linkedin_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('linkedin_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(campaign_id AS TEXT) AS ad_group_id,
CAST(campaign_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_group_id AS TEXT) AS campaign_id,
CAST(campaign_group_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(cost AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_linkedin_ads.linkedin_ads__url_report
), pinterest_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('pinterest_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_pinterest.pinterest_ads__url_report
), snapchat_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('snapchat_ads' AS TEXT) AS platform,
CAST(ad_account_id AS TEXT) AS account_id,
CAST(ad_account_name AS TEXT) AS account_name,
CAST(ad_squad_id AS TEXT) AS ad_group_id,
CAST(ad_squad_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(swipes AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_snapchat_ads.snapchat_ads__url_report
), tiktok_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('tiktok_ads' AS TEXT) AS platform,
CAST(advertiser_id AS TEXT) AS account_id,
CAST(advertiser_name AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_tiktok_ads.tiktok_ads__url_report
), twitter_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('twitter_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(account_name AS TEXT) AS account_name,
CAST(line_item_id AS TEXT) AS ad_group_id,
CAST(line_item_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_twitter_ads.twitter_ads__url_report
), reddit_ads AS (
SELECT
source_relation,
TO_DATE(TO_TIMESTAMP(date_day)) AS date_day,
CAST('reddit_ads' AS TEXT) AS platform,
CAST(account_id AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_name,
CAST(ad_group_id AS TEXT) AS ad_group_id,
CAST(ad_group_name AS TEXT) AS ad_group_name,
CAST(base_url AS TEXT) AS base_url,
CAST(campaign_id AS TEXT) AS campaign_id,
CAST(campaign_name AS TEXT) AS campaign_name,
CAST(clicks AS INT) AS clicks,
CAST(impressions AS INT) AS impressions,
CAST(spend AS FLOAT) AS spend,
CAST(url_host AS TEXT) AS url_host,
CAST(url_path AS TEXT) AS url_path,
CAST(utm_campaign AS TEXT) AS utm_campaign,
CAST(utm_content AS TEXT) AS utm_content,
CAST(utm_medium AS TEXT) AS utm_medium,
CAST(utm_source AS TEXT) AS utm_source,
CAST(utm_term AS TEXT) AS utm_term
FROM TEST.PUBLIC_reddit_ads.reddit_ads__url_report
), unioned AS (
SELECT
*
FROM facebook_ads
UNION ALL
SELECT
*
FROM google_ads
UNION ALL
SELECT
*
FROM linkedin_ads
UNION ALL
SELECT
*
FROM microsoft_ads
UNION ALL
SELECT
*
FROM pinterest_ads
UNION ALL
SELECT
*
FROM snapchat_ads
UNION ALL
SELECT
*
FROM tiktok_ads
UNION ALL
SELECT
*
FROM twitter_ads
UNION ALL
SELECT
*
FROM reddit_ads
)
SELECT
*
FROM unioned
Name | Type | Comment |
---|
This SQL query generates a time spine (a series of consecutive dates) starting from 10 years ago up to tomorrow in the America/Los_Angeles timezone. It uses a recursive CTE to generate numbers, converts them to dates, and then filters and formats the results. The final output is a table with a single column of dates.
FeaturizationFilteringCleaningWITH days AS (
WITH date_spine AS (
WITH rawdata AS (
WITH p AS (
SELECT
0 AS generated_number
UNION ALL
SELECT
1
), unioned AS (
SELECT
p0.generated_number * POWER(2, 0) + p1.generated_number * POWER(2, 1) + p2.generated_number * POWER(2, 2) + p3.generated_number * POWER(2, 3) + p4.generated_number * POWER(2, 4) + p5.generated_number * POWER(2, 5) + p6.generated_number * POWER(2, 6) + p7.generated_number * POWER(2, 7) + p8.generated_number * POWER(2, 8) + p9.generated_number * POWER(2, 9) + p10.generated_number * POWER(2, 10) + p11.generated_number * POWER(2, 11) + 1 AS generated_number
FROM p AS p0
CROSS JOIN p AS p1
CROSS JOIN p AS p2
CROSS JOIN p AS p3
CROSS JOIN p AS p4
CROSS JOIN p AS p5
CROSS JOIN p AS p6
CROSS JOIN p AS p7
CROSS JOIN p AS p8
CROSS JOIN p AS p9
CROSS JOIN p AS p10
CROSS JOIN p AS p11
)
SELECT
*
FROM unioned
WHERE
generated_number <= 3651
ORDER BY
generated_number
), all_periods AS (
SELECT
(
DATEADD(
day,
(
ROW_NUMBER() OVER (ORDER BY 1) - 1
),
DATEADD(
day,
-3650,
CAST(CONVERT_TIMEZONE(
'UTC',
'America/Los_Angeles',
CAST(CONVERT_TIMEZONE('UTC', CURRENT_TIMESTAMP()) AS TIMESTAMP)
) AS DATE)
)
)
) AS date_day
FROM rawdata
), filtered AS (
SELECT
*
FROM all_periods
WHERE
date_day <= CAST(DATEADD(
day,
1,
CAST(CONVERT_TIMEZONE(
'UTC',
'America/Los_Angeles',
CAST(CONVERT_TIMEZONE('UTC', CURRENT_TIMESTAMP()) AS TIMESTAMP)
) AS DATE)
) AS DATE)
)
SELECT
*
FROM filtered
)
SELECT
CAST(d.date_day AS TIMESTAMP) AS date_day
FROM date_spine AS d
), cast_to_date AS (
SELECT
CAST(date_day AS DATE) AS date_day
FROM days
)
SELECT
*
FROM cast_to_date
Name | Type | Comment |
---|---|---|
date_day | date | None |
This SQL query combines data from campaign-level reports, profile information, and campaign history to create an account-level report for Amazon Ads. It joins these tables, filters for non-deleted and most recent records, and aggregates metrics such as cost, clicks, and impressions by account, date, and other dimensions.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
/* use campaign report since account report not provided */
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_level_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__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,
SUM(report.cost) AS cost,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
/* use campaign report since portfolio report not provided */
FROM report
LEFT JOIN campaigns
ON campaigns.campaign_id = report.campaign_id
AND campaigns.source_relation = report.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6
)
SELECT
*
FROM fields
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 report. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
country_code | text | The code for a given country. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query integrates data from multiple Amazon Ads-related tables to create a comprehensive ad group report. It joins information from ad group reports, account profiles, portfolios, campaigns, and ad groups. The query filters for the most recent records in some tables, aggregates cost, clicks, and impressions data, and combines various attributes like account details, portfolio information, campaign data, and ad group specifics into a single result set.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_level_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads.int_amazon_ads__portfolio_history
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_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,
campaigns.campaign_name,
campaigns.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ad_groups.serving_status,
ad_groups.state,
ad_groups.default_bid,
report.campaign_bidding_strategy,
SUM(report.cost) AS cost,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN ad_groups
ON ad_groups.ad_group_id = report.ad_group_id
AND ad_groups.source_relation = report.source_relation
LEFT JOIN campaigns
ON campaigns.campaign_id = ad_groups.campaign_id
AND campaigns.source_relation = ad_groups.source_relation
LEFT JOIN portfolios
ON portfolios.portfolio_id = campaigns.portfolio_id
AND portfolios.source_relation = campaigns.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
ad_group_id | text | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the campaign, if present in the source data. |
campaign_bidding_strategy | text | The bidding strategy associated with a campaign. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
country_code | text | The code for a given country. |
default_bid | double precision | The date of creation of the record. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query combines data from multiple Amazon Ads-related tables to create a comprehensive ad performance report. It joins information from advertised product reports, account profiles, portfolios, campaigns, ad groups, and product ads. The query filters for the most recent records in some tables, aggregates metrics like cost, clicks, and impressions, and includes various identifiers and attributes related to the ads and their hierarchical structure.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__advertised_product_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads.int_amazon_ads__portfolio_history
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__product_ad_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,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
report.ad_id,
ads.serving_status,
ads.state,
report.advertised_asin,
report.advertised_sku,
report.campaign_budget_amount,
report.campaign_budget_currency_code,
report.campaign_budget_type,
SUM(report.cost) AS cost,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN ads
ON ads.ad_id = report.ad_id AND ads.source_relation = report.source_relation
LEFT JOIN ad_groups
ON ad_groups.ad_group_id = report.ad_group_id
AND ad_groups.source_relation = report.source_relation
LEFT JOIN campaigns
ON campaigns.campaign_id = report.campaign_id
AND campaigns.source_relation = report.source_relation
LEFT JOIN portfolios
ON portfolios.portfolio_id = campaigns.portfolio_id
AND portfolios.source_relation = campaigns.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
ad_id | text | The ID representing the ad, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the campaign, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
country_code | text | The code for a given country. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
advertised_asin | text | The ASIN associated to an advertised product. |
advertised_sku | text | The SKU being advertised. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query integrates data from multiple Amazon Ads-related tables to create a comprehensive campaign report. It joins campaign-level report data with account information, portfolio details, and campaign history. The query then aggregates metrics such as cost, clicks, and impressions at the campaign level, while including various attributes like account name, portfolio name, and campaign details.
IntegrationAggregationFilteringWITH report AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_level_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads.int_amazon_ads__portfolio_history
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__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,
campaigns.campaign_name,
report.campaign_id,
report.campaign_bidding_strategy,
report.campaign_budget_amount,
report.campaign_budget_currency_code,
report.campaign_budget_type,
SUM(report.cost) AS cost,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN campaigns
ON campaigns.campaign_id = report.campaign_id
AND campaigns.source_relation = report.source_relation
LEFT JOIN portfolios
ON portfolios.portfolio_id = campaigns.portfolio_id
AND portfolios.source_relation = campaigns.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
campaign_bidding_strategy | text | The bidding strategy associated with a campaign. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
country_code | text | The code for a given country. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query integrates data from multiple Amazon Ads-related tables to create a comprehensive keyword report. It joins information from targeting keyword reports, account profiles, portfolios, campaigns, ad groups, and keyword history. The query filters for the most recent records in some tables, combines relevant fields from all sources, and aggregates cost, clicks, and impressions data. The result is a detailed view of keyword performance across various organizational levels (account, portfolio, campaign, ad group) with associated metadata.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__targeting_keyword_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads.int_amazon_ads__portfolio_history
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), keywords AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__keyword_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,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
report.keyword_id,
keywords.keyword_text,
keywords.serving_status,
keywords.state,
report.keyword_bid,
report.keyword_type,
report.match_type,
SUM(report.cost) AS cost,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN keywords
ON keywords.keyword_id = report.keyword_id
AND keywords.source_relation = report.source_relation
LEFT JOIN ad_groups
ON ad_groups.ad_group_id = report.ad_group_id
AND ad_groups.source_relation = report.source_relation
LEFT JOIN campaigns
ON campaigns.campaign_id = report.campaign_id
AND campaigns.source_relation = report.source_relation
LEFT JOIN portfolios
ON portfolios.portfolio_id = campaigns.portfolio_id
AND portfolios.source_relation = campaigns.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
country_code | text | The code for a given country. |
keyword_bid | double precision | Bid associated with a keyword or targeting expression. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
keyword_text | text | The keyword text. |
keyword_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
match_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query combines data from multiple Amazon Ads-related tables to create a comprehensive portfolio report. It joins information from campaign reports, account profiles, portfolio history, and campaign history. The query filters for the most recent records in portfolio and campaign history, and aggregates cost, clicks, and impressions data at the portfolio level. The result provides a detailed view of portfolio performance including budget information, serving status, and associated account details.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
/* use campaign report since portfolio report not provided */
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_level_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__portfolio_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__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
/* use campaign report since portfolio report not provided */
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
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
budget_amount | integer | The budget amount associated with the portfolio. Cannot be null. |
budget_currency_code | integer | The currency used for all monetary values for entities under this profile. Cannot be null. |
budget_end_date | integer | The end date after which the budget is no longer applied. Optional if policy is set to dateRange or monthlyRecurring. |
budget_start_date | integer | The starting date in YYYYMMDD format to which the budget is applied. Required if policy is set to dateRange. Not specified if policy is set to monthlyRecurring. Note that the starting date for monthlyRecurring is the date when the policy is set. |
budget_policy | integer | The budget policy. Set to dateRange to specify a budget for a specific period of time. Set to monthlyRecurring to specify a budget that is automatically renewed at the beginning of each month. Cannot be null. |
country_code | text | The code for a given country. |
in_budget | boolean | Indicates the current budget status of the portfolio. Set to true if the portfolio is in budget, set to false if the portfolio is out of budget. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query integrates data from multiple Amazon Ads-related tables to create a comprehensive search term report. It joins information from search term reports, account profiles, portfolios, campaigns, ad groups, and keywords. The query filters for the most recent records in some tables, aggregates cost, clicks, and impressions data, and provides a detailed view of search term performance across various levels of the account structure.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__search_term_ad_keyword_report
), account_info AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile
WHERE
_fivetran_deleted = FALSE
), portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads.int_amazon_ads__portfolio_history
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), keywords AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__keyword_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,
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
FROM report
LEFT JOIN keywords
ON keywords.keyword_id = report.keyword_id
AND keywords.source_relation = report.source_relation
LEFT JOIN ad_groups
ON ad_groups.ad_group_id = report.ad_group_id
AND ad_groups.source_relation = report.source_relation
LEFT JOIN campaigns
ON campaigns.campaign_id = report.campaign_id
AND campaigns.source_relation = report.source_relation
LEFT JOIN portfolios
ON portfolios.portfolio_id = campaigns.portfolio_id
AND portfolios.source_relation = campaigns.source_relation
LEFT JOIN account_info
ON account_info.profile_id = campaigns.profile_id
AND account_info.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
)
SELECT
*
FROM fields
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 report. |
account_id | text | The ID representing the account. |
account_name | text | The name of the account, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
country_code | text | The code for a given country. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
keyword_text | text | The keyword text. |
match_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
search_term | text | The search term used by the customer. |
state | text | The state of the record (enabled, paused, or archived). |
targeting | text | A string representation of the expression object used in the targeting clause. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query selects all columns from the most recent records in the amazon_ads_source.stg_amazon_ads__portfolio_history table. It filters the data to include only the rows where 'is_most_recent_record' is TRUE, effectively retrieving the latest portfolio history information.
FilteringWITH portfolios AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__portfolio_history
WHERE
is_most_recent_record = TRUE
)
SELECT
*
FROM portfolios
Name | Type | Comment |
---|---|---|
source_relation | text | None |
portfolio_id | text | None |
budget_amount | integer | None |
budget_currency_code | integer | None |
budget_end_date | integer | None |
budget_policy | integer | None |
budget_start_date | integer | None |
creation_date | text | None |
in_budget | boolean | None |
last_updated_date | text | None |
portfolio_name | text | None |
profile_id | text | None |
serving_status | text | None |
state | text | None |
is_most_recent_record | boolean | None |
This SQL query stages data from an Amazon Ads ad group history source table. It casts and renames several columns, adds a source_relation column, and creates an is_most_recent_record flag to identify the most recent record for each ad group. The query also performs type casting to ensure data consistency.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS creation_date,
CAST(NULL AS FLOAT) AS default_bid,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TIMESTAMP) AS last_updated_date,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS TEXT) AS state,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS ad_group_id,
CAST(campaign_id AS TEXT) AS campaign_id,
creation_date,
default_bid,
last_updated_date,
name AS ad_group_name,
serving_status,
state,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_updated_date DESC) = 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_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
creation_date | text | The date of creation of the record. |
default_bid | double precision | The date of creation of the record. |
last_updated_date | text | Date of last update to record. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
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 development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_updated_date | text | None |
_fivetran_synced | text | None |
campaign_id | integer | None |
creation_date | text | None |
default_bid | double precision | None |
name | text | None |
serving_status | text | None |
state | text | None |
This SQL query performs data type casting and column renaming on a staging table for Amazon Ads ad group level reports. It creates a base CTE from a temporary table, defines a fields CTE with explicit data types for each column (mostly set to NULL or empty values), and then casts and renames columns in the final CTE. The query doesn't filter, deduplicate, or aggregate data, but focuses on data type standardization and column naming.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__ad_group_level_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS campaign_bidding_strategy,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS FLOAT) AS cost,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_group_id AS TEXT) AS ad_group_id,
campaign_bidding_strategy,
clicks,
cost,
date AS date_day,
impressions
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. |
date_day | date | The date of the report. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
campaign_bidding_strategy | text | The bidding strategy associated with a campaign. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a template or placeholder query, possibly for testing or initializing a structure without actually populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | integer | None |
date | date | None |
_fivetran_synced | text | None |
campaign_bidding_strategy | text | None |
clicks | integer | None |
cost | double precision | None |
impressions | integer | None |
This SQL query performs a series of data type conversions and column renamings on the 'stg_amazon_ads__advertised_product_report' table. It starts by selecting all columns from a temporary table, then creates a fields CTE with NULL or empty values for all columns. Finally, it casts several columns to different data types, renames the 'date' column to 'date_day', and selects all columns from the final CTE.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__advertised_product_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS TEXT) AS advertised_asin,
CAST(NULL AS TEXT) AS advertised_sku,
CAST(NULL AS FLOAT) AS campaign_budget_amount,
CAST(NULL AS TEXT) AS campaign_budget_currency_code,
CAST(NULL AS TEXT) AS campaign_budget_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS FLOAT) AS cost,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_id AS TEXT) AS ad_id,
CAST(ad_group_id AS TEXT) AS ad_group_id,
advertised_asin,
advertised_sku,
campaign_budget_amount,
campaign_budget_currency_code,
campaign_budget_type,
CAST(campaign_id AS TEXT) AS campaign_id,
clicks,
cost,
date AS date_day,
impressions
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. |
date_day | date | The date of the report. |
ad_id | text | The ID representing the ad group, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
advertised_asin | text | The ASIN associated to an advertised product. |
advertised_sku | text | The SKU being advertised. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
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 a staging table in a dbt (data build tool) project, specifically for Amazon Ads data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | integer | None |
ad_id | integer | None |
campaign_id | integer | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
campaign_budget_amount | double precision | None |
campaign_budget_currency_code | text | None |
campaign_budget_type | text | None |
clicks | integer | None |
cost | double precision | None |
impressions | integer | None |
advertised_asin | integer | None |
advertised_sku | integer | None |
This SQL query stages data from an Amazon Ads campaign history source. It first creates a base CTE from a temporary table, then defines a fields CTE with nullable columns of specific data types. The final CTE casts some columns, renames others, and adds a flag for the most recent record per campaign. The query then selects all columns from this final CTE.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS bidding_strategy,
CAST(NULL AS TIMESTAMP) AS creation_date,
CAST(NULL AS DATE) AS end_date,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TIMESTAMP) AS last_updated_date,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS portfolio_id,
CAST(NULL AS INT) AS profile_id,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS DATE) AS start_date,
CAST(NULL AS TEXT) AS state,
CAST(NULL AS TEXT) AS targeting_type,
CAST(NULL AS FLOAT) AS budget,
CAST(NULL AS TEXT) AS budget_type,
CAST(NULL AS FLOAT) AS effective_budget,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS campaign_id,
last_updated_date,
bidding_strategy,
creation_date,
end_date,
name AS campaign_name,
CAST(portfolio_id AS TEXT) AS portfolio_id,
CAST(profile_id AS TEXT) AS profile_id,
serving_status,
start_date,
state,
targeting_type,
budget,
budget_type,
effective_budget,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_updated_date DESC) = 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 | text | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
creation_date | text | The date of creation of the record. |
last_updated_date | text | Date of last update to record. |
portfolio_id | text | The ID of the Portfolio. |
bidding_strategy | text | The bidding strategy associated with a campaign (legacyForSales, autoForSales, or manual.) |
budget | integer | The budget for the campaign. |
budget_type | text | One of: daily or lifetime. |
effective_budget | integer | Adjusted budget for the campaign that has been impacted by a budget rule. |
end_date | integer | The end date of the campaign. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
start_date | date | The start date of the campaign. |
state | text | The state of the record (enabled, paused, or archived). |
targeting_type | text | The type of targeting used for the campaign, either manual or auto. |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
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 development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_updated_date | text | None |
_fivetran_synced | text | None |
bidding_strategy | text | None |
creation_date | text | None |
budget | integer | None |
end_date | integer | None |
name | text | None |
portfolio_id | integer | None |
profile_id | integer | None |
serving_status | text | None |
start_date | date | None |
state | text | None |
targeting_type | text | None |
budget_type | text | None |
effective_budget | integer | None |
This SQL query creates a staging table for Amazon Ads campaign-level reports. It starts with a base table, then defines a fields CTE that casts all columns to specific data types, including several NULL values. The final CTE renames some columns and casts the campaign_id to TEXT. The query primarily focuses on data type standardization and column renaming.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__campaign_level_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS campaign_applicable_budget_rule_id,
CAST(NULL AS TEXT) AS campaign_applicable_budget_rule_name,
CAST(NULL AS TEXT) AS campaign_bidding_strategy,
CAST(NULL AS FLOAT) AS campaign_budget_amount,
CAST(NULL AS TEXT) AS campaign_budget_currency_code,
CAST(NULL AS TEXT) AS campaign_budget_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS FLOAT) AS campaign_rule_based_budget_amount,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS FLOAT) AS cost,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
campaign_applicable_budget_rule_id,
campaign_applicable_budget_rule_name,
campaign_bidding_strategy,
campaign_budget_amount,
campaign_budget_currency_code,
campaign_budget_type,
CAST(campaign_id AS TEXT) AS campaign_id,
campaign_rule_based_budget_amount,
clicks,
cost,
date AS date_day,
impressions
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. |
date_day | date | The date of the report. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
campaign_applicable_budget_rule_id | integer | The ID associated to the active budget rule for a campaign. |
campaign_applicable_budget_rule_name | integer | The name associated to the active budget rule for a campaign. |
campaign_bidding_strategy | text | The bidding strategy associated with a campaign. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
campaign_rule_based_budget_amount | integer | The value of the rule-based budget for a campaign. |
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 data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
campaign_id | integer | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
campaign_applicable_budget_rule_id | integer | None |
campaign_applicable_budget_rule_name | integer | None |
campaign_bidding_strategy | text | None |
campaign_budget_amount | double precision | None |
campaign_budget_currency_code | text | None |
campaign_budget_type | text | None |
clicks | integer | None |
cost | double precision | None |
impressions | integer | None |
campaign_rule_based_budget_amount | integer | None |
This SQL query stages data from a temporary table, casts various fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each keyword. It also includes a 'source_relation' field, which appears to be empty in this case.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__keyword_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS FLOAT) AS bid,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS creation_date,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS keyword_text,
CAST(NULL AS TIMESTAMP) AS last_updated_date,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS TEXT) AS native_language_keyword,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS TEXT) AS state,
CAST(NULL AS TEXT) AS native_language_locale,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS keyword_id,
CAST(ad_group_id AS TEXT) AS ad_group_id,
bid,
CAST(campaign_id AS TEXT) AS campaign_id,
creation_date,
keyword_text,
last_updated_date,
match_type,
native_language_keyword,
serving_status,
state,
native_language_locale,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_updated_date DESC) = 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. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
last_updated_date | text | Date of last update to record. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
bid | double precision | Bid associated with this keyword. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
creation_date | text | The date of creation of the record. |
keyword_text | text | The exact text for the keyword. |
match_type | text | The keyword match type associated with this record. |
native_language_keyword | integer | The unlocalized keyword text in the preferred locale of the advertiser. |
native_language_locale | integer | The preferred language locale of the advertiser. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
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 LIMIT 0 clause ensures no rows are returned. This query appears to be a placeholder or template for defining the structure of a staging table in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_updated_date | text | None |
_fivetran_synced | text | None |
ad_group_id | integer | None |
bid | double precision | None |
campaign_id | integer | None |
creation_date | text | None |
keyword_text | text | None |
match_type | text | None |
native_language_keyword | integer | None |
serving_status | text | None |
state | text | None |
native_language_locale | integer | None |
This SQL query stages data from a temporary table, casts various fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each portfolio. It also includes data type conversions for certain fields.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__portfolio_history_tmp
), fields AS (
SELECT
CAST(NULL AS FLOAT) AS budget_amount,
CAST(NULL AS TEXT) AS budget_currency_code,
CAST(NULL AS DATE) AS budget_end_date,
CAST(NULL AS TEXT) AS budget_policy,
CAST(NULL AS DATE) AS budget_start_date,
CAST(NULL AS TIMESTAMP) AS creation_date,
CAST(NULL AS INT) AS id,
CAST(NULL AS BOOLEAN) AS in_budget,
CAST(NULL AS TIMESTAMP) AS last_updated_date,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS profile_id,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS TEXT) AS state,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS portfolio_id,
budget_amount,
budget_currency_code,
budget_end_date,
budget_policy,
budget_start_date,
creation_date,
in_budget,
last_updated_date,
name AS portfolio_name,
CAST(profile_id AS TEXT) AS profile_id,
serving_status,
state,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_updated_date DESC) = 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. |
portfolio_id | text | The ID of the Portfolio. |
portfolio_name | text | The name of the Portfolio. |
last_updated_date | text | Date of last update to record. |
creation_date | text | The date of creation of the record. |
budget_amount | integer | The budget amount associated with the portfolio. Cannot be null. |
budget_currency_code | integer | The currency used for all monetary values for entities under this profile. Cannot be null. |
budget_end_date | integer | The end date after which the budget is no longer applied. Optional if policy is set to dateRange or monthlyRecurring. |
budget_start_date | integer | The starting date in YYYYMMDD format to which the budget is applied. Required if policy is set to dateRange. Not specified if policy is set to monthlyRecurring. Note that the starting date for monthlyRecurring is the date when the policy is set. |
budget_policy | integer | The budget policy. Set to dateRange to specify a budget for a specific period of time. Set to monthlyRecurring to specify a budget that is automatically renewed at the beginning of each month. Cannot be null. |
in_budget | boolean | Indicates the current budget status of the portfolio. Set to true if the portfolio is in budget, set to false if the portfolio is out of budget. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT type. It's likely used as a placeholder or template for further development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_updated_date | text | None |
_fivetran_synced | text | None |
budget_amount | integer | None |
budget_currency_code | integer | None |
budget_end_date | integer | None |
budget_policy | integer | None |
budget_start_date | integer | None |
creation_date | text | None |
in_budget | boolean | None |
name | text | None |
profile_id | integer | None |
serving_status | text | None |
state | text | None |
This SQL query stages data from a temporary table, casts various fields to specific data types, and creates a final table with transformed columns. It also adds a boolean column to identify the most recent record for each unique combination of source_relation and id.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__product_ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS asin,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS creation_date,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TIMESTAMP) AS last_updated_date,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS TEXT) AS sku,
CAST(NULL AS TEXT) AS state,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS ad_id,
CAST(ad_group_id AS TEXT) AS ad_group_id,
asin,
CAST(campaign_id AS TEXT) AS campaign_id,
creation_date,
last_updated_date,
serving_status,
sku,
state,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_updated_date DESC) = 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 | text | The ID representing the ad, if present in the source data. |
last_updated_date | text | Date of last update to record. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
asin | text | The ASIN associated with the ad. |
creation_date | text | The date of creation of the record. |
sku | integer | The product SKU associated with the ad. |
serving_status | text | The current serving status of the record. |
state | text | The state of the record (enabled, paused, or archived). |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
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 | integer | None |
last_updated_date | text | None |
_fivetran_synced | text | None |
ad_group_id | integer | None |
asin | text | None |
campaign_id | integer | None |
creation_date | text | None |
serving_status | text | None |
sku | integer | None |
state | text | None |
This SQL query stages data from a temporary table for Amazon Ads profiles. It defines a set of fields with specific data types, casts some fields, and renames the 'id' field to 'profile_id'. The query also adds a 'source_relation' field and ensures all fields are properly typed for downstream use.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__profile_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS account_marketplace_string_id,
CAST(NULL AS TEXT) AS account_name,
CAST(NULL AS TEXT) AS account_sub_type,
CAST(NULL AS TEXT) AS account_type,
CAST(NULL AS BOOLEAN) AS account_valid_payment_method,
CAST(NULL AS TEXT) AS country_code,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS INT) AS daily_budget,
CAST(NULL AS TEXT) AS timezone,
CAST(NULL AS BOOLEAN) AS _fivetran_deleted,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS profile_id,
CAST(account_id AS TEXT) AS account_id,
account_marketplace_string_id,
account_name,
account_sub_type,
account_type,
account_valid_payment_method,
country_code,
currency_code,
daily_budget,
timezone,
_fivetran_deleted
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_deleted | boolean | Boolean created by Fivetran to indicate whether the record has been deleted. |
profile_id | text | The profile ID associated with your Amazon Ads account. Advertisers who operate in more than one marketplace (for example, Amazon.com, Amazon.co.uk, Amazon.co.jp) will have one profile associated with each marketplace. |
account_id | text | The ID representing the account. |
account_marketplace_string_id | text | The identifier of the marketplace to which the account is associated. |
account_name | text | The name of the account, if present in the source data. |
account_sub_type | integer | The account subtype. |
account_type | text | One of seller, vendor, or agency. |
account_valid_payment_method | boolean | For Vendors, this returns if the Advertiser has set up a valid payment method. |
country_code | text | The code for a given country. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
daily_budget | integer | Daily budget for Sponsored Product campaigns for seller type accounts. |
timezone | text | The time zone used for all date-based campaign management and reporting. |
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 development or to establish a schema structure without actually populating data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
_fivetran_deleted | boolean | None |
_fivetran_synced | text | None |
account_id | text | None |
account_marketplace_string_id | text | None |
account_name | text | None |
account_sub_type | integer | None |
account_type | text | None |
account_valid_payment_method | boolean | None |
country_code | text | None |
currency_code | text | None |
daily_budget | integer | None |
timezone | text | None |
This SQL query stages data from a temporary table, casts various fields to specific data types, and renames some columns. It primarily serves to clean and standardize the data structure for the Amazon Ads search term ad keyword report.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__search_term_ad_keyword_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_keyword_status,
CAST(NULL AS FLOAT) AS campaign_budget_amount,
CAST(NULL AS TEXT) AS campaign_budget_currency_code,
CAST(NULL AS TEXT) AS campaign_budget_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS FLOAT) AS cost,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS FLOAT) AS keyword_bid,
CAST(NULL AS INT) AS keyword_id,
CAST(NULL AS TEXT) AS search_term,
CAST(NULL AS TEXT) AS targeting,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_group_id AS TEXT) AS ad_group_id,
ad_keyword_status,
campaign_budget_amount,
campaign_budget_currency_code,
campaign_budget_type,
CAST(campaign_id AS TEXT) AS campaign_id,
clicks,
cost,
date AS date_day,
impressions,
keyword_bid,
CAST(keyword_id AS TEXT) AS keyword_id,
search_term,
targeting
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. |
date_day | date | The date of the report. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_keyword_status | text | Current status of a keyword. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
keyword_bid | double precision | Bid associated with a keyword or targeting expression. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
search_term | text | The search term used by the customer. |
targeting | text | A string representation of the expression object used in the targeting clause. |
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 a staging table in a dbt (data build tool) project, specifically for Amazon Ads search term and keyword report data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | integer | None |
campaign_id | integer | None |
date | date | None |
keyword_id | integer | None |
_fivetran_synced | timestamp without time zone | None |
campaign_budget_amount | double precision | None |
campaign_budget_currency_code | text | None |
campaign_budget_type | text | None |
clicks | integer | None |
cost | double precision | None |
impressions | integer | None |
keyword_bid | double precision | None |
search_term | text | None |
targeting | text | None |
This SQL query performs data type casting and column renaming on a staging table for Amazon Ads targeting keyword report data. It creates a CTE with null values for all fields, then casts some fields to different data types in the final select statement. The query also adds a source_relation column and renames the 'date' column to 'date_day'.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_amazon_ads_source.stg_amazon_ads__targeting_keyword_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_keyword_status,
CAST(NULL AS FLOAT) AS campaign_budget_amount,
CAST(NULL AS TEXT) AS campaign_budget_currency_code,
CAST(NULL AS TEXT) AS campaign_budget_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS FLOAT) AS cost,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS FLOAT) AS keyword_bid,
CAST(NULL AS INT) AS keyword_id,
CAST(NULL AS TEXT) AS keyword_type,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS TEXT) AS targeting,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_group_id AS TEXT) AS ad_group_id,
ad_keyword_status,
campaign_budget_amount,
campaign_budget_currency_code,
campaign_budget_type,
CAST(campaign_id AS TEXT) AS campaign_id,
clicks,
cost,
date AS date_day,
impressions,
keyword_bid,
CAST(keyword_id AS TEXT) AS keyword_id,
keyword_type,
match_type,
targeting
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. |
date_day | date | The date of the report. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_keyword_status | text | Current status of a keyword. |
campaign_budget_amount | double precision | Total budget allocated to the campaign. |
campaign_budget_currency_code | text | The currency code associated with the campaign. |
campaign_budget_type | text | One of: daily or lifetime. |
campaign_id | text | The ID representing the campaign, if present in the source data. |
keyword_bid | double precision | Bid associated with a keyword or targeting expression. |
keyword_id | text | The ID representing the keyword, if present in the source data. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
cost | double precision | Total cost of ad clicks. |
keyword_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
match_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
targeting | text | A string representation of the expression object used in the targeting clause. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, initialized to NULL. The query is limited to 0 rows, effectively creating a schema-only representation of the table without any data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | integer | None |
campaign_id | integer | None |
date | date | None |
keyword_id | integer | None |
_fivetran_synced | timestamp without time zone | None |
ad_keyword_status | text | None |
campaign_budget_amount | double precision | None |
campaign_budget_currency_code | text | None |
campaign_budget_type | text | None |
clicks | integer | None |
cost | double precision | None |
impressions | integer | None |
keyword_bid | double precision | None |
keyword_type | text | None |
match_type | text | None |
targeting | text | None |
This SQL query integrates data from multiple tables related to Apple Search Ads, including ad group reports, ad group history, campaign history, and organization information. It joins these tables based on common identifiers and source relations, filters for the most recent records in history tables, and aggregates metrics such as taps, downloads, impressions, and spend. The result is a comprehensive view of ad performance data grouped by various dimensions like organization, campaign, ad group, and date.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_report
), ad_group AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
campaign.campaign_id,
campaign.campaign_name,
ad_group.ad_group_id,
ad_group.ad_group_name,
report.currency,
ad_group.ad_group_status,
ad_group.start_at,
ad_group.end_at,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN ad_group
ON report.ad_group_id = ad_group.ad_group_id
AND report.source_relation = ad_group.source_relation
JOIN campaign
ON ad_group.campaign_id = campaign.campaign_id
AND ad_group.source_relation = campaign.source_relation
JOIN organization
ON ad_group.organization_id = organization.organization_id
AND ad_group.source_relation = organization.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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
campaign_id | integer | Campaign ID associated with this record. |
campaign_name | text | Campaign name associated with this record. |
ad_group_id | integer | Ad group ID associated with this record. |
ad_group_name | text | Ad group name associated with this record. |
ad_group_status | text | The status of the ad group associated with this record. |
start_at | timestamp without time zone | The start timestamp for this ad group. |
end_at | timestamp without time zone | The end timestamp for this ad group. |
date_day | date | Date of the report. |
currency | text | This currency value should match the respective organization's currency value. |
taps | bigint | Number of taps on ad group on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of ad group in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of ad group in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of ad group in a given day. |
spend | double precision | The spend on ad group in the given day. |
This SQL query integrates data from multiple Apple Search Ads related tables (ad report, ad history, ad group history, campaign history, and organization) to create a comprehensive ad performance report. It joins these tables based on various ID fields and the source relation, filters for the most recent records in history tables, and aggregates metrics such as taps, downloads, impressions, and spend. The result is a detailed view of ad performance across different organizational levels (organization, campaign, ad group, and individual ad) for each date and currency.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_report
), ad AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_group AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
campaign.campaign_id,
campaign.campaign_name,
ad_group.ad_group_id,
ad_group.ad_group_name,
ad.ad_id,
ad.ad_name,
report.currency,
ad.ad_status,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN ad
ON report.ad_id = ad.ad_id AND report.source_relation = ad.source_relation
JOIN ad_group
ON report.ad_group_id = ad_group.ad_group_id
AND report.source_relation = ad_group.source_relation
JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
JOIN organization
ON ad.organization_id = organization.organization_id
AND ad.source_relation = organization.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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
campaign_id | integer | Campaign ID associated with this record. |
campaign_name | text | Campaign name associated with this record. |
ad_group_id | integer | Ad group ID associated with this record. |
ad_group_name | text | Ad group name associated with this record. |
ad_id | integer | Ad ID associated with this record. |
ad_name | text | Ad name associated with this record. |
ad_status | text | The status of the ad associated with this record. |
date_day | date | Date of the report. |
currency | text | This currency value should match the respective organization's currency value. |
taps | bigint | Number of taps on ad group on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of ad group in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of ad group in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of ad group in a given day. |
spend | numeric | The spend on ad group in the given day. |
This SQL query integrates data from three tables: campaign report, campaign history, and organization. It joins these tables based on campaign_id and organization_id, filtering for the most recent campaign records. The query then aggregates data at the campaign level, calculating metrics such as taps, downloads, impressions, and spend. The result is a comprehensive campaign report with organizational details and key performance indicators.
IntegrationFilteringAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_report
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
campaign.organization_id,
organization.organization_name,
campaign.campaign_id,
campaign.campaign_name,
report.currency,
campaign.campaign_status,
campaign.start_at,
campaign.end_at,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
JOIN organization
ON campaign.organization_id = organization.organization_id
AND campaign.source_relation = organization.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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
campaign_id | integer | Campaign ID associated with this record. |
campaign_name | text | Campaign name associated with this record. |
campaign_status | text | The status of the campaign associted with this record. |
start_at | timestamp without time zone | The start timestamp of this campaign. |
end_at | timestamp without time zone | The end timestamp of this campaign. |
date_day | date | Date of the report. |
currency | text | This currency value should match the respective organization's currency value. |
taps | bigint | Number of taps on campaign on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of campaign in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of campaign in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of campaign in a given day. |
spend | double precision | The spend on campaign in the given day. |
This SQL query joins multiple staging tables (keyword report, keyword history, ad group history, campaign history, and organization) to create a comprehensive report on Apple Search Ads performance. It filters for the most recent records in history tables, integrates data across different levels (organization, campaign, ad group, keyword), and aggregates metrics such as taps, downloads, impressions, and spend. The result is a detailed view of keyword performance within the context of ad groups, campaigns, and organizations.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__keyword_report
), keyword AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__keyword_history
WHERE
is_most_recent_record = TRUE
), ad_group AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
campaign.campaign_id,
campaign.campaign_name,
ad_group.ad_group_id,
ad_group.ad_group_name,
keyword.keyword_id,
keyword.keyword_text,
keyword.match_type,
report.currency,
keyword.keyword_status,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN keyword
ON report.keyword_id = keyword.keyword_id
AND report.source_relation = keyword.source_relation
JOIN ad_group
ON keyword.ad_group_id = ad_group.ad_group_id
AND keyword.source_relation = ad_group.source_relation
JOIN campaign
ON ad_group.campaign_id = campaign.campaign_id
AND ad_group.source_relation = campaign.source_relation
JOIN organization
ON ad_group.organization_id = organization.organization_id
AND ad_group.source_relation = organization.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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
campaign_id | integer | Campaign ID associated with this record. |
campaign_name | text | Campaign name associated with this record. |
ad_group_id | integer | Ad group ID associated with this record. |
ad_group_name | text | Ad group name associated with this record. |
keyword_id | integer | Creative set name associatd with this record. |
keyword_text | text | Creative set ID associated with this record. |
match_type | text | Controls how ads are matched to user searches; EXACT or BROAD. |
date_day | date | Date of the report. |
currency | text | This currency value should match the respective organization's currency value. |
keyword_status | text | The status of the keyword associated with this record. |
taps | bigint | Number of taps on keyword on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of keyword in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of keyword in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of keyword in a given day. |
spend | double precision | The spend on keyword in the given day. |
This query integrates data from three sources: campaign reports, campaign history, and organization information. It joins these tables, filters for the most recent campaign records, and then aggregates various metrics (taps, downloads, impressions, and spend) at the organization level, grouped by date and organization details.
IntegrationFilteringAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_report
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
organization.currency,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
JOIN organization
ON campaign.organization_id = organization.organization_id
AND campaign.source_relation = organization.source_relation
GROUP BY
1,
2,
3,
4,
5
)
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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
date_day | date | Date of the report. |
currency | text | This currency value should match the respective organization's currency value. |
taps | bigint | Number of taps on organization on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of organization in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of organization in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of organization in a given day. |
spend | double precision | The spend on organization in the given day. |
This SQL query integrates data from three tables: search term report, campaign history, and organization. It joins these tables based on specific conditions, filters out null search terms, and aggregates various metrics (taps, downloads, impressions, spend) grouped by several dimensions including date, organization, campaign, ad group, keyword, and search term. The query provides a comprehensive view of search term performance across different organizational and campaign structures.
IntegrationFilteringAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__search_term_report
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), organization AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization
), joined AS (
SELECT
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
campaign.campaign_id,
campaign.campaign_name,
report.ad_group_id,
report.ad_group_name,
report.keyword_id,
report.keyword_text,
report.search_term_text,
report.match_type,
report.currency,
SUM(report.taps) AS taps,
SUM(report.new_downloads) AS new_downloads,
SUM(report.redownloads) AS redownloads,
SUM(report.new_downloads + report.redownloads) AS total_downloads,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
JOIN organization
ON campaign.organization_id = organization.organization_id
AND campaign.source_relation = organization.source_relation
WHERE
NOT report.search_term_text IS NULL
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. |
organization_id | integer | Organization ID associated with this record. |
organization_name | text | Organization name associated with this record. |
campaign_id | integer | Campaign ID associated with this record. |
campaign_name | text | Campaign name associated with this record. |
ad_group_id | integer | Ad group ID associated with this record. |
ad_group_name | text | Ad group name associated with this record. |
match_type | text | Controls how ads are matched to user searches; EXACT or BROAD. |
date_day | date | Date of the report. |
search_term_text | text | Search term text. |
currency | text | This currency value should match the respective organization's currency value. |
taps | bigint | Number of taps on organization on given day. |
new_downloads | bigint | App downloads from new users who have never before installed app of organization in a given day. |
redownloads | bigint | Number of user downloads where user deletes app and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device of organization in a given day. |
total_downloads | bigint | The sum of new_downloads and redownloads. |
impressions | bigint | The number of impressions of organization in a given day. |
spend | double precision | The spend on organization in the given day. |
keyword_id | integer | None |
keyword_text | text | None |
This SQL query stages data from an Apple Search Ads source table. It casts columns to specific data types, renames some columns, and adds a flag to identify the most recent record for each ad group. The query doesn't perform any filtering or aggregation but focuses on structuring and preparing the data for further use.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS modification_time,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS organization_id,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
modification_time AS modified_at,
organization_id,
campaign_id,
name AS ad_group_name,
id AS ad_group_id,
status AS ad_group_status,
start_time AS start_at,
end_time AS end_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modification_time DESC) = 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. |
modified_at | timestamp without time zone | Timestamp of when the ad group object was last modified. |
is_most_recent_record | boolean | Boolean indicating whether record was the most recent instance. |
ad_group_id | integer | Ad group ID associated with the record. |
campaign_id | integer | Campaign ID associated with the record. |
start_at | timestamp without time zone | The designated ad group start time. |
end_at | timestamp without time zone | The designated ad group end time. |
ad_group_name | text | Ad group name. |
organization_id | integer | Organization ID associated with the record. |
status | None | The current status of the ad group, values include ENABLED and PAUSED. |
ad_group_status | text | None |
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 | integer | None |
modification_time | timestamp without time zone | None |
automated_keywords_opt_in | boolean | None |
campaign_id | integer | None |
cpa_goal_amount | integer | None |
cpa_goal_currency | integer | None |
default_cpc_bid_amount | integer | None |
default_cpc_bid_currency | integer | None |
deleted | boolean | None |
end_time | timestamp without time zone | None |
name | text | None |
organization_id | integer | None |
serving_state_reasons | integer | None |
serving_status | text | None |
start_time | timestamp without time zone | None |
status | text | None |
storefronts | integer | None |
This SQL query creates a staging table for Apple Search Ads ad group reports. It starts with a base table, then defines a set of fields with specific data types (mostly set to NULL or empty values). Finally, it selects and renames some of these fields, preparing the data for further processing or analysis.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_group_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS local_spend_amount,
CAST(NULL AS TEXT) AS local_spend_currency,
CAST(NULL AS INT) AS new_downloads,
CAST(NULL AS INT) AS redownloads,
CAST(NULL AS INT) AS taps,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
ad_group_id,
impressions,
local_spend_amount AS spend,
local_spend_currency AS currency,
new_downloads,
redownloads,
taps
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_group_id | integer | Ad group ID associated with the record. |
date_day | date | Date for daily report aggregation. |
impressions | integer | Number of times your ad appeared in App Store search results. |
spend | double precision | The sum of costs associated with the number of impressions served. Spend is measured in the currency used in the campaign. |
currency | text | This currency value should match the respective organization's currency value. |
new_downloads | integer | App downloads from new users who have never before installed the respective app. |
redownloads | integer | Occurs when a user downloads respective app, deletes it, and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device. |
taps | integer | The number of times ad was tapped by users within the reporting time period. |
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 LIMIT 0 clause ensures no rows are returned. This query appears to be a placeholder or template for further development.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | integer | None |
date | date | None |
avg_cpa_amount | double precision | None |
avg_cpa_currency | text | None |
avg_cpt_amount | double precision | None |
avg_cpt_currency | text | None |
conversion_rate | double precision | None |
conversions | integer | None |
impressions | integer | None |
lat_off_installs | integer | None |
lat_on_installs | integer | None |
local_spend_amount | double precision | None |
local_spend_currency | text | None |
new_downloads | integer | None |
redownloads | integer | None |
tap_through_rate | double precision | None |
taps | integer | None |
This SQL query performs a series of transformations on data from the 'stg_apple_search_ads__ad_history_tmp' table. It casts several columns to specific data types, renames some columns, and adds a flag to identify the most recent record for each ad. The query also includes a 'source_relation' column, though it's set to an empty string in this case.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS creation_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS modification_time,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS org_id,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
creation_time AS created_at,
modification_time AS modified_at,
org_id AS organization_id,
campaign_id,
ad_group_id,
name AS ad_name,
id AS ad_id,
status AS ad_status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modification_time DESC) = 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. |
created_at | timestamp without time zone | Timestamp of when ad was created. |
modified_at | timestamp without time zone | Timestamp of when the ad object was last modified. |
organization_id | integer | Organization ID associated with the record. |
campaign_id | integer | Campaign ID associated with the record. |
ad_group_id | integer | Ad group ID associated with the record. |
ad_name | text | Ad name associated with the record. |
ad_id | integer | Ad ID associated with the record. |
ad_status | text | The current status of the ad, values include ENABLED and PAUSED. |
is_most_recent_record | boolean | None |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for further development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
creation_time | timestamp without time zone | None |
modification_time | timestamp without time zone | None |
org_id | integer | None |
campaign_id | integer | None |
ad_group_id | integer | None |
name | text | None |
id | integer | None |
creative_id | integer | None |
creative_type | text | None |
status | text | None |
This SQL query creates a staging table for Apple Search Ads ad report data. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types (mostly casting to NULL or empty values). Finally, it selects and renames some of these fields, structuring the data for further use in the data pipeline.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__ad_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS local_spend_amount,
CAST(NULL AS TEXT) AS local_spend_currency,
CAST(NULL AS INT) AS new_downloads,
CAST(NULL AS INT) AS redownloads,
CAST(NULL AS INT) AS taps,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
campaign_id,
ad_group_id,
ad_id,
impressions,
local_spend_amount AS spend,
local_spend_currency AS currency,
new_downloads,
redownloads,
taps
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. |
date_day | date | Date for daily report aggregation. |
campaign_id | integer | Campaign ID associated with the record. |
ad_group_id | integer | Ad group ID associated with the record. |
ad_id | integer | Ad ID associated with the record. |
impressions | integer | Number of times your ad appeared in App Store search results. |
spend | numeric(28,6) | The sum of costs associated with the number of impressions served; Spend is measured in the currency used in the campaign. |
currency | text | This currency value should match the respective organization's currency value. |
new_downloads | integer | App downloads from new users who have never before installed the respective app. |
redownloads | integer | Occurs when a user downloads respective app, deletes it, and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device. |
taps | integer | The number of times ad was tapped by users within the reporting time period. |
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 |
---|---|---|
_dbt_source_relation | text | None |
This SQL query stages data from a temporary table, casts several fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each campaign. It prepares the data for further processing or analysis by structuring and cleaning the raw data from the source.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS modification_time,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS organization_id,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
modification_time AS modified_at,
organization_id,
name AS campaign_name,
id AS campaign_id,
status AS campaign_status,
start_time AS start_at,
end_time AS end_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modification_time DESC) = 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. |
modified_at | timestamp without time zone | Timestamp of when the campaign object was last modified. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
campaign_id | integer | Campaign ID associated with the record. |
start_at | timestamp without time zone | The designated ad group start time (ad group with the earliest start time associated with this campaign). |
end_at | timestamp without time zone | The designated campaign end time (ad group with the latest start time associated with this campaign). |
campaign_name | text | Campaign name for the record. |
organization_id | integer | Organization ID associated with the record. |
status | None | The current status of the campaign, values include ENABLED and PAUSED. |
campaign_status | text | None |
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 a staging table in a dbt (data build tool) project, specifically for Apple Search Ads campaign history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
modification_time | timestamp without time zone | None |
adam_id | integer | None |
budget_amount | integer | None |
budget_currency | text | None |
budget_orders | text | None |
daily_budget_amount | integer | None |
daily_budget_currency | text | None |
deleted | boolean | None |
end_time | timestamp without time zone | None |
loc_invoice_detail_buyer_email | integer | None |
loc_invoice_detail_buyer_name | integer | None |
loc_invoice_detail_client_name | integer | None |
loc_invoice_detail_order_number | integer | None |
name | text | None |
organiation_id | integer | None |
payment_model | text | None |
serving_state_reasons | text | None |
serving_status | text | None |
start_time | timestamp without time zone | None |
status | text | None |
This SQL query creates a staging table for Apple Search Ads campaign reports. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types and null values. Finally, it renames and reorganizes these fields into a final structure. The query doesn't perform any data transformation or filtering; it's primarily setting up a structure for the data.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__campaign_report_tmp
), fields AS (
SELECT
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS local_spend_amount,
CAST(NULL AS TEXT) AS local_spend_currency,
CAST(NULL AS INT) AS new_downloads,
CAST(NULL AS INT) AS redownloads,
CAST(NULL AS INT) AS taps,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
id AS campaign_id,
impressions,
local_spend_amount AS spend,
local_spend_currency AS currency,
new_downloads,
redownloads,
taps
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 | integer | Campaign ID associated with the record. |
date_day | date | Date for daily report aggregation. |
impressions | integer | Number of times your ad appeared in App Store search results. |
spend | double precision | The sum of costs associated with the number of impressions served; Spend is measured in the currency used in the campaign. |
currency | text | This currency value should match the respective organization's currency value. |
new_downloads | integer | App downloads from new users who have never before installed the respective app. |
redownloads | integer | Occurs when a user downloads respective app, deletes it, and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device. |
taps | integer | The number of times ad was tapped by users within the reporting time period. |
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 LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used for initializing a temporary table structure in a data modeling process.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
id | integer | None |
avg_cpa_amount | double precision | None |
avg_cpa_currency | text | None |
avg_cpt_amount | double precision | None |
avg_cpt_currency | text | None |
conversion_rate | double precision | None |
conversions | integer | None |
impressions | integer | None |
lat_off_installs | integer | None |
lat_on_installs | integer | None |
local_spend_amount | double precision | None |
local_spend_currency | text | None |
new_downloads | integer | None |
redownloads | integer | None |
tap_through_rate | double precision | None |
taps | integer | None |
This SQL query processes data from the 'apple_search_ads_source.stg_apple_search_ads__keyword_history' model. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types. The final select statement renames some columns, calculates a 'is_most_recent_record' flag using a window function, and selects all fields from this final transformation.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__keyword_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS DECIMAL(28, 6)) AS bid_amount,
CAST(NULL AS TEXT) AS bid_currency,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS TIMESTAMP) AS modification_time,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS text,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
modification_time AS modified_at,
campaign_id,
ad_group_id,
id AS keyword_id,
bid_amount,
bid_currency,
match_type,
status AS keyword_status,
text AS keyword_text,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modification_time DESC) = 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. |
modified_at | timestamp without time zone | Timestamp of when the keyword object was last modified. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
keyword_id | integer | Keyword ID associated with the record. |
campaign_id | integer | Campaign ID associated with the record. |
ad_group_id | integer | Ad group ID associated with the record. |
bid_amount | double precision | The bid amount to display your ad for the matching text value. |
bid_currency | text | This currency value should match the respective organization's currency value. |
match_type | text | Controls how ads are matched to user searches; EXACT or BROAD. |
status | None | The current status of the keyword, values include ENABLED and PAUSED. |
keyword_text | text | The word or phrase to match in user searches, to show respective ad. |
keyword_status | text | None |
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 doesn't retrieve any actual data as it has a LIMIT 0 clause.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
modification_time | timestamp without time zone | None |
ad_group_id | integer | None |
bid_amount | double precision | None |
bid_currency | text | None |
campaign_id | integer | None |
deleted | boolean | None |
match_type | text | None |
status | text | None |
text | text | None |
This SQL query performs a data transformation on the 'stg_apple_search_ads__keyword_report' model. It starts by selecting all columns from a temporary table, then creates a fields CTE with null or empty values for specific columns. Finally, it renames and reorganizes these columns in the final CTE. The query doesn't perform any actual data manipulation or filtering, but rather sets up a structure for the data.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__keyword_report_tmp
), fields AS (
SELECT
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS local_spend_amount,
CAST(NULL AS TEXT) AS local_spend_currency,
CAST(NULL AS INT) AS new_downloads,
CAST(NULL AS INT) AS redownloads,
CAST(NULL AS INT) AS taps,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
id AS keyword_id,
impressions,
local_spend_amount AS spend,
local_spend_currency AS currency,
new_downloads,
redownloads,
taps
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. |
keyword_id | integer | Keyword ID associated with the record. |
date_day | date | Date for daily report aggregation. |
impressions | integer | Number of times your ad appeared in App Store search results. |
spend | double precision | The sum of costs associated with the number of impressions served; Spend is measured in the currency used in the campaign. |
currency | text | This currency value should match the respective organization's currency value. |
new_downloads | integer | App downloads from new users who have never before installed the respective app. |
redownloads | integer | Occurs when a user downloads respective app, deletes it, and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device. |
taps | integer | The number of times ad was tapped by users within the reporting time period. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. The query doesn't select any actual data and limits the result to 0 rows. This type of query is often used as a placeholder or to define a schema without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
id | integer | None |
avg_cpa_amount | double precision | None |
avg_cpa_currency | text | None |
avg_cpt_amount | double precision | None |
avg_cpt_currency | text | None |
conversion_rate | double precision | None |
conversions | integer | None |
impressions | integer | None |
lat_off_installs | integer | None |
lat_on_installs | integer | None |
local_spend_amount | double precision | None |
local_spend_currency | text | None |
new_downloads | integer | None |
redownloads | integer | None |
tap_through_rate | double precision | None |
taps | integer | None |
This SQL query creates a staging table for Apple Search Ads organization data. It starts with a base table, then creates a fields CTE with null or empty values for specific columns. The final CTE renames and reorganizes these columns. The query doesn't perform any data manipulation or transformation on actual values; it's setting up a structure for the staging table with placeholder values.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__organization_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS payment_model,
CAST(NULL AS TEXT) AS time_zone,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS organization_id,
currency,
payment_model,
name AS organization_name,
time_zone
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. |
organization_id | integer | Organization ID associated with the record. |
currency | text | Specified currency for respective organization. |
payment_model | text | Values include 'LOC', 'PAYG' or |
organization_name | text | Name of organization. |
time_zone | text | Organization default timezone; values can be ORTZ or UTC. |
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 | integer | None |
currency | text | None |
name | text | None |
payment_model | text | None |
role_names | text | None |
time_zone | text | None |
This SQL query stages data from an Apple Search Ads source table. It casts various fields to specific data types, renames some columns, and selects a subset of columns for the final output. The query doesn't perform any filtering, deduplication, or aggregation, but focuses on cleaning and restructuring the data for further use.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_apple_search_ads_source.stg_apple_search_ads__search_term_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS BOOLEAN) AS ad_group_deleted,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_group_name,
CAST(NULL AS DECIMAL(28, 6)) AS bid_amount_amount,
CAST(NULL AS TEXT) AS bid_amount_currency,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS BOOLEAN) AS deleted,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS keyword,
CAST(NULL AS TEXT) AS keyword_display_status,
CAST(NULL AS INT) AS keyword_id,
CAST(NULL AS DECIMAL(28, 6)) AS local_spend_amount,
CAST(NULL AS TEXT) AS local_spend_currency,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS INT) AS new_downloads,
CAST(NULL AS INT) AS redownloads,
CAST(NULL AS TEXT) AS search_term_source,
CAST(NULL AS TEXT) AS search_term_text,
CAST(NULL AS INT) AS taps,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
_fivetran_id,
campaign_id,
ad_group_id,
ad_group_name,
bid_amount_amount AS bid_amount,
bid_amount_currency AS bid_currency,
keyword AS keyword_text,
keyword_display_status,
keyword_id,
local_spend_amount AS spend,
local_spend_currency AS currency,
match_type,
search_term_source,
search_term_text,
impressions,
taps,
new_downloads,
redownloads
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 | A Fivetran internal unique id that helps us avoid duplicate rows in primary keyless tables. |
ad_group_id | integer | Ad group ID associated with the record. |
ad_format | None | Format of creative set. |
campaign_id | integer | Campaign ID associated with the record. |
date_day | date | Date for daily report aggregation. |
ad_group_name | text | Ad group name. |
bid_amount | double precision | The bid amount to display your ad for the matching text value. |
bid_currency | text | This currency value should match the respective organization's currency value. |
keyword_text | text | The word or phrase to match in user searches, to show respective ad. |
keyword_display_status | text | The keyword display status can either be ACTIVE or PAUSED. |
keyword_id | integer | Keyword ID associated with the record. |
spend | double precision | The sum of costs associated with the number of impressions served; Spend is measured in the currency used in the campaign. |
currency | text | This currency value should match the respective organization's currency value. |
match_type | text | Controls how ads are matched to user searches; values include EXACT, BROAD or AUTO. |
search_term_source | text | The search term source can either be TARGETED or AUTO; if this value is AUTO so will match_type. |
search_term_text | text | The word or phrase to match of user searches. |
impressions | integer | Number of times your ad appeared in App Store search results. |
taps | integer | The number of times ad was tapped by users within the reporting time period. |
new_downloads | integer | App downloads from new users who have never before installed the respective app. |
redownloads | integer | Occurs when a user downloads respective app, deletes it, and downloads the same app again following a tap on an ad on the App Store, or downloads the same app to an additional device. |
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 template or schema for the '_dbt_source_relation' column without actually returning any data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_id | text | None |
ad_group_id | integer | None |
campaign_id | integer | None |
date | date | None |
ad_group_deleted | boolean | None |
ad_group_name | text | None |
avg_cpa_amount | double precision | None |
avg_cpa_currency | text | None |
avg_cpt_amount | double precision | None |
avg_cpt_currency | text | None |
bid_amount_amount | double precision | None |
bid_amount_currency | text | None |
conversion_rate | double precision | None |
conversions | integer | None |
deleted | boolean | None |
impressions | integer | None |
keyword | text | None |
keyword_display_status | text | None |
keyword_id | integer | None |
lat_off_installs | integer | None |
lat_on_installs | integer | None |
local_spend_amount | double precision | None |
local_spend_currency | text | None |
match_type | text | None |
new_downloads | integer | None |
redownloads | integer | None |
search_term_source | text | None |
search_term_text | text | None |
tap_through_rate | double precision | None |
taps | integer | None |
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 | character varying | 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 | character varying | 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 | character varying | 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 | character varying | 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 | character varying | 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 | character varying | 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 | text | 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 | character varying | None |
_fivetran_synced | text | 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 | text | 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 | text | 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 |
This SQL query combines data from Google Ads account statistics and account history. It filters the account history to only include the most recent records, joins this with the account statistics, and then aggregates various metrics (spend, clicks, impressions, conversions) at the account level. The result provides a comprehensive view of each account's performance and characteristics.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
stats.account_id,
accounts.currency_code,
accounts.auto_tagging_enabled,
accounts.time_zone,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(stats.conversions) AS conversions,
SUM(stats.conversions_value) AS conversions_value,
SUM(stats.view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN accounts
ON stats.account_id = accounts.account_id
AND stats.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | integer | The Customer ID. |
time_zone | integer | The time zone which the account is located. |
currency_code | text | The currency which the account uses. |
auto_tagging_enabled | boolean | Boolean indicating if the account uses auto tagging within ad urls. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | bigint | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query integrates data from multiple Google Ads-related tables (ad group stats, account history, campaign history, and ad group history) to create a comprehensive ad group report. It joins these tables, filters for the most recent records in the history tables, and aggregates metrics such as spend, clicks, impressions, and conversions at the ad group level. The result provides a detailed view of ad group performance with associated account, campaign, and ad group attributes.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
accounts.account_id,
accounts.currency_code,
campaigns.campaign_name,
campaigns.campaign_id,
ad_groups.ad_group_name,
stats.ad_group_id,
ad_groups.ad_group_status,
ad_groups.ad_group_type,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(conversions) AS conversions,
SUM(conversions_value) AS conversions_value,
SUM(view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN ad_groups
ON stats.ad_group_id = ad_groups.ad_group_id
AND stats.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON campaigns.account_id = accounts.account_id
AND campaigns.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | integer | The Customer ID. |
currency_code | text | The currency which the account uses. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_group_status | text | The status of the ad group. |
ad_group_type | text | The type of ad group which is serving ads. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | double precision | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query combines data from multiple Google Ads-related tables (ad stats, accounts, campaigns, ad groups, and ads) to create a comprehensive ad performance report. It joins these tables, filters for the most recent records in some cases, and aggregates metrics such as spend, clicks, impressions, and conversions. The result is a detailed view of ad performance across various dimensions like account, campaign, ad group, and individual ad level.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
accounts.account_id,
accounts.currency_code,
campaigns.campaign_name,
campaigns.campaign_id,
ad_groups.ad_group_name,
stats.ad_group_id,
stats.ad_id,
ads.ad_name,
ads.ad_status,
ads.ad_type,
ads.display_url,
ads.source_final_urls,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(conversions) AS conversions,
SUM(conversions_value) AS conversions_value,
SUM(view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN ads
ON stats.ad_id = ads.ad_id
AND stats.source_relation = ads.source_relation
AND stats.ad_group_id = ads.ad_group_id
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON campaigns.account_id = accounts.account_id
AND campaigns.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | integer | The Customer ID. |
currency_code | text | The currency which the account uses. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The unique identifier of the ad. |
display_url | integer | The url which is displayed with the ad. |
source_final_urls | text | The final urls that are used within the ad. |
ad_status | text | The status of the ad. |
ad_type | integer | The type of the ad that is being served. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | bigint | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
ad_name | integer | None |
This SQL query integrates data from three different staging tables (campaign stats, account history, and campaign history) to create a comprehensive campaign report. It joins these tables, filters for the most recent records in the history tables, and aggregates various metrics like spend, clicks, impressions, and conversions. The result is a detailed view of campaign performance across different accounts and campaigns.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
accounts.account_id,
accounts.currency_code,
campaigns.campaign_name,
stats.campaign_id,
campaigns.advertising_channel_type,
campaigns.advertising_channel_subtype,
campaigns.status,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(conversions) AS conversions,
SUM(conversions_value) AS conversions_value,
SUM(view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN campaigns
ON stats.campaign_id = campaigns.campaign_id
AND stats.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON campaigns.account_id = accounts.account_id
AND campaigns.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | integer | The Customer ID. |
currency_code | text | The currency which the account uses. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
advertising_channel_type | text | The channel type of the ads being served within the campaign. |
advertising_channel_subtype | text | The channel subtype of the ads being served within the campaign. |
status | text | The status of the campaign. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | bigint | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query integrates data from multiple Google Ads-related tables (keyword stats, account history, campaign history, ad group history, and ad group criterion history) to create a comprehensive keyword report. It joins these tables based on various IDs and the source relation, filters for the most recent records in the history tables, and aggregates metrics such as spend, clicks, impressions, and conversions. The result is a detailed view of keyword performance across different levels of the account structure (account, campaign, ad group) with associated metadata.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__keyword_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), criterions AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_criterion_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
stats.account_id,
accounts.currency_code,
campaigns.campaign_name,
stats.campaign_id,
ad_groups.ad_group_name,
stats.ad_group_id,
stats.criterion_id,
criterions.type,
criterions.status,
criterions.keyword_match_type,
criterions.keyword_text,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(conversions) AS conversions,
SUM(conversions_value) AS conversions_value,
SUM(view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN criterions
ON stats.criterion_id = criterions.criterion_id
AND stats.source_relation = criterions.source_relation
LEFT JOIN ad_groups
ON stats.ad_group_id = ad_groups.ad_group_id
AND stats.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON stats.campaign_id = campaigns.campaign_id
AND stats.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON stats.account_id = accounts.account_id
AND stats.source_relation = accounts.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | bigint | The Customer ID. |
currency_code | text | The currency which the account uses. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
criterion_id | bigint | The unique identifier of the criterion being served. |
type | text | The type of keyword ad being served. |
status | text | The status of the keyword ads. |
keyword_match_type | text | The match type criteria used for the keyword ads. |
keyword_text | text | The specific keyword text that is used. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | bigint | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query integrates data from multiple Google Ads-related tables (ad stats, accounts, campaigns, ad groups, and ads) to create a comprehensive URL report. It filters for the most recent records in history tables, joins these tables together, and aggregates various metrics (spend, clicks, impressions, conversions) grouped by different dimensions such as account, campaign, ad group, and URL components. The query also includes UTM parameters for tracking purposes.
FilteringIntegrationAggregationWITH stats AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_stats
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
stats.source_relation,
stats.date_day,
accounts.account_name,
accounts.account_id,
accounts.currency_code,
campaigns.campaign_name,
campaigns.campaign_id,
ad_groups.ad_group_name,
stats.ad_group_id,
stats.ad_id,
ads.base_url,
ads.url_host,
ads.url_path,
ads.utm_source,
ads.utm_medium,
ads.utm_campaign,
ads.utm_content,
ads.utm_term,
SUM(stats.spend) AS spend,
SUM(stats.clicks) AS clicks,
SUM(stats.impressions) AS impressions,
SUM(conversions) AS conversions,
SUM(conversions_value) AS conversions_value,
SUM(view_through_conversions) AS view_through_conversions
FROM stats
LEFT JOIN ads
ON stats.ad_id = ads.ad_id
AND stats.source_relation = ads.source_relation
AND stats.ad_group_id = ads.ad_group_id
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON campaigns.account_id = accounts.account_id
AND campaigns.source_relation = accounts.source_relation
WHERE
NOT ads.source_final_urls IS NULL
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18
)
SELECT
*
FROM fields
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 being reported on. |
account_name | integer | The descriptive name of the Customer account. |
account_id | integer | The Customer ID. |
currency_code | text | The currency which the account uses. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The unique identifier of the ad. |
base_url | text | The base url of the ad. |
url_host | text | The URL host of the ad. |
url_path | text | The URL path of the ad. |
utm_source | text | The utm_source parameter of the ad. |
utm_medium | text | The utm_medium parameter of the ad. |
utm_campaign | text | The utm_campaign parameter of the ad. |
utm_content | text | The utm_content parameter of the ad. |
utm_term | text | The utm_term parameter of the ad. |
spend | numeric | Total cost of ad clicks. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
conversions | bigint | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | bigint | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | bigint | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query processes Google Ads account history data. It starts by selecting all columns from a temporary staging table, then defines a set of fields with specific data types. The final selection includes various account details, adds a source relation field, and determines the most recent record for each account. The query also filters out inactive records.
CleaningDeduplicationFilteringWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS BOOLEAN) AS auto_tagging_enabled,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS TEXT) AS descriptive_name,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS time_zone,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS BOOLEAN) AS _fivetran_active,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS account_id,
updated_at,
currency_code,
auto_tagging_enabled,
time_zone,
descriptive_name AS account_name,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_most_recent_record
FROM fields
WHERE
COALESCE(_fivetran_active, TRUE)
)
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 | integer | The ID representing the account. |
currency_code | text | The currency of the spend reported. |
auto_tagging_enabled | boolean | Boolean indicating if auto tagging is enabled on the account ads. |
time_zone | integer | The time zone of the account ads. |
account_name | integer | The descriptive name of the Customer account. |
updated_at | timestamp without time zone | Timestamp of when a record was last synced. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, initialized as NULL. The query is likely used as a placeholder or template for further development, or to establish a schema structure without populating data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
updated_at | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
auto_tagging_enabled | boolean | None |
currency_code | text | None |
descriptive_name | integer | None |
final_url_suffix | boolean | None |
hidden | boolean | None |
manager | integer | None |
manager_customer_id | double precision | None |
optimization_score | text | None |
pay_per_conversion_eligibility_failure_reasons | boolean | None |
test_account | text | None |
time_zone | integer | None |
tracking_url_template | integer | None |
This SQL query stages and transforms data from a Google Ads account statistics source table. It casts columns to specific data types, renames some columns, and performs basic data cleaning operations such as coalescing null values to zeros or default values. The query also converts the cost from micros to a decimal value. The final output includes selected and transformed columns from the source data.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__account_stats_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_network_type,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS INT) AS cost_micros,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS device,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS conversions,
CAST(NULL AS INT) AS conversions_value,
CAST(NULL AS INT) AS view_through_conversions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
customer_id AS account_id,
date AS date_day,
ad_network_type,
device,
COALESCE(clicks, 0) AS clicks,
COALESCE(cost_micros, 0) / 1000000.0 AS spend,
COALESCE(impressions, 0) AS impressions,
COALESCE(conversions, 0) AS conversions,
COALESCE(conversions_value, 0) AS conversions_value,
COALESCE(view_through_conversions, 0) AS view_through_conversions
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 | integer | The Customer ID. |
date_day | date | The date being reported on. |
ad_network_type | text | The Google Ad network type used across the account. |
device | text | Account ad performance per unique device where the ads were served. |
clicks | integer | The count of clicks. |
spend | numeric | The sum of your cost-per-click (CPC) and cost-per-thousand impressions (CPM) costs during this period. |
impressions | integer | The count of impressions. |
conversions | integer | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | integer | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | integer | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, initialized to NULL. The LIMIT 0 clause ensures no rows are returned. This query is likely used as a placeholder or template for generating a schema without data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_id | text | None |
customer_id | integer | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
active_view_impressions | integer | None |
active_view_measurability | integer | None |
active_view_measurable_cost_micros | integer | None |
active_view_measurable_impressions | integer | None |
active_view_viewability | integer | None |
ad_network_type | text | None |
clicks | integer | None |
conversions | integer | None |
conversions_value | integer | None |
cost_micros | integer | None |
device | text | None |
impressions | integer | None |
interaction_event_types | text | None |
interactions | integer | None |
view_through_conversions | integer | None |
This SQL query stages data from a Google Ads source table, casting various fields to specific data types, and then selects and transforms these fields. It adds a 'source_relation' field, renames 'id' to 'criterion_id', and adds an 'is_most_recent_record' flag based on the most recent 'updated_at' timestamp for each unique combination of 'source_relation' and 'id'. The query also filters out records where '_fivetran_active' is false.
CleaningDeduplicationFilteringWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_criterion_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS base_campaign_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS keyword_match_type,
CAST(NULL AS TEXT) AS keyword_text,
CAST(NULL AS BOOLEAN) AS _fivetran_active,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS criterion_id,
CAST(ad_group_id AS TEXT) AS ad_group_id,
base_campaign_id,
updated_at,
type,
status,
keyword_match_type,
keyword_text,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_most_recent_record
FROM fields
WHERE
COALESCE(_fivetran_active, TRUE)
)
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. |
criterion_id | bigint | Unique identifier of the ad group criterion. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
base_campaign_id | bigint | The ID representing the campaign, if present in the source data. |
updated_at | timestamp without time zone | Timestamp of when the record was last updated in Google Ads. |
type | text | The type of ad group criterion. |
status | text | The current status of the ad group criterion. |
keyword_match_type | text | The match type which dictate how closely the keyword needs to match with the user’s search query so that the ad can be considered for the auction. |
keyword_text | text | The text used within the keyword criterion that is being matched against. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
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.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
ad_group_id | bigint | None |
base_campaign_id | bigint | None |
updated_at | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
type | text | None |
status | text | None |
keyword_match_type | text | None |
keyword_text | text | None |
This SQL query processes data from a Google Ads ad group history table. It casts fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each ad group. The query also filters out inactive records and selects specific columns for the final output.
CleaningDeduplicationFilteringWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TEXT) AS campaign_name,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS BOOLEAN) AS _fivetran_active,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(id AS TEXT) AS ad_group_id,
updated_at,
type AS ad_group_type,
campaign_id,
campaign_name,
name AS ad_group_name,
status AS ad_group_status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_most_recent_record
FROM fields
WHERE
COALESCE(_fivetran_active, TRUE)
)
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_group_id | text | The ID representing the ad group, if present in the source data. |
updated_at | timestamp without time zone | Timestamp of when the record was last updated in Google Ads. |
ad_group_type | text | The type of the ad group in Google Ads. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_status | text | Status of the ad group. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
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 |
updated_at | timestamp without time zone | None |
type | text | None |
campaign_id | bigint | None |
campaign_name | text | None |
name | text | None |
status | text | None |
This SQL query stages and transforms data from a Google Ads source table. It casts columns to specific data types, renames some columns, and performs simple calculations. The query also applies COALESCE to replace NULL values with zeros for numeric columns and divides the cost_micros by 1,000,000 to convert it to a standard currency unit.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_group_stats_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_network_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS INT) AS cost_micros,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS device,
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS conversions,
CAST(NULL AS INT) AS conversions_value,
CAST(NULL AS INT) AS view_through_conversions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
customer_id AS account_id,
date AS date_day,
CAST(id AS TEXT) AS ad_group_id,
campaign_id,
device,
ad_network_type,
COALESCE(clicks, 0) AS clicks,
COALESCE(cost_micros, 0) / 1000000.0 AS spend,
COALESCE(impressions, 0) AS impressions,
COALESCE(conversions, 0) AS conversions,
COALESCE(conversions_value, 0) AS conversions_value,
COALESCE(view_through_conversions, 0) AS view_through_conversions
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 Customer ID. |
date_day | date | The date being reported on. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_network_type | text | The Google Ad network type used across the account. |
device | text | Account ad performance per unique device where the ads were served. |
clicks | integer | The count of clicks. |
spend | numeric | The sum of your cost-per-click (CPC) and cost-per-thousand impressions (CPM) costs during this period. |
impressions | integer | The count of impressions. |
conversions | double precision | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | integer | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | integer | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
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 LIMIT 0 ensures no rows are returned. This query appears to be 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 |
---|---|---|
_fivetran_id | text | None |
customer_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
active_view_impressions | integer | None |
active_view_measurability | integer | None |
active_view_measurable_cost_micros | integer | None |
active_view_measurable_impressions | integer | None |
active_view_viewability | integer | None |
ad_network_type | text | None |
base_ad_group | text | None |
campaign_base_campaign | text | None |
campaign_id | bigint | None |
clicks | integer | None |
conversions | double precision | None |
conversions_value | integer | None |
cost_micros | integer | None |
device | text | None |
id | bigint | None |
impressions | integer | None |
interaction_event_types | text | None |
interactions | integer | None |
view_through_conversions | integer | None |
This SQL query processes and transforms data from a Google Ads source table. It casts columns to specific data types, filters for active records, deduplicates to keep the most recent record for each ad, and extracts various URL components and UTM parameters from the final_urls field. The query also cleans and standardizes the final_urls data by removing brackets and splitting it into individual URLs.
CleaningDeduplicationFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS display_url,
CAST(NULL AS TEXT) AS final_urls,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS BOOLEAN) AS _fivetran_active,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
CAST(ad_group_id AS TEXT) AS ad_group_id,
id AS ad_id,
name AS ad_name,
updated_at,
type AS ad_type,
status AS ad_status,
display_url,
final_urls AS source_final_urls,
REPLACE(REPLACE(final_urls, '[', ''), ']', '') AS final_urls,
ROW_NUMBER() OVER (PARTITION BY source_relation, id, ad_group_id ORDER BY updated_at DESC) = 1 AS is_most_recent_record
FROM fields
WHERE
COALESCE(_fivetran_active, TRUE)
), final_urls AS (
SELECT
*,
SPLIT_PART(final_urls, ',', 1) AS final_url /* Extract the first url within the list of urls provided within the final_urls field */
FROM final
), url_fields AS (
SELECT
*,
SPLIT_PART(final_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(final_url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(final_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(final_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(final_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(final_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(final_url, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(final_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(final_url, 'utm_term=', 2), '&', 1), '') AS utm_term
FROM final_urls
)
SELECT
*
FROM url_fields
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_group_id | text | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
updated_at | timestamp without time zone | Timestamp of when the record was last updated in Google Ads. |
display_url | integer | The display url of the ad that is being served. |
ad_type | integer | The type of the ad in Google Ads. |
ad_status | text | Status of the Ad. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
source_final_urls | text | The original list of final urls expressed as an array. Please be aware the test used on this field is intended to warn you if you have fields with multiple urls. If you do, the `final_url` field will filter down the urls within the array to just the first. Therefore, this package will only leverage one of possibly many urls within this field array. |
final_url | text | The first url in the list of the urls within the `final_urls` source field. |
base_url | text | The base url of the ad. |
url_host | text | The URL host of the ad. |
url_path | text | The URL path of the ad. |
utm_source | text | The utm_source parameter of the ad. |
utm_medium | text | The utm_medium parameter of the ad. |
utm_campaign | text | The utm_campaign parameter of the ad. |
utm_content | text | The utm_content parameter of the ad. |
utm_term | text | The utm_term parameter of the ad. |
ad_name | integer | None |
final_urls | text | None |
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 data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | bigint | None |
id | bigint | None |
name | integer | None |
updated_at | timestamp without time zone | None |
type | integer | None |
status | text | None |
display_url | integer | None |
final_urls | text | None |
This SQL query stages data from a Google Ads source table. It casts fields to specific data types, applies some data transformations (like converting cost_micros to dollars), and renames some columns. The query also includes logic to handle potentially null values and extracts ad_group_id from a string when necessary.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__ad_stats_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS ad_group,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS TEXT) AS ad_network_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS INT) AS cost_micros,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS device,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS keyword_ad_group_criterion,
CAST(NULL AS INT) AS conversions,
CAST(NULL AS INT) AS conversions_value,
CAST(NULL AS INT) AS view_through_conversions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
customer_id AS account_id,
date AS date_day,
COALESCE(CAST(ad_group_id AS TEXT), SPLIT_PART(ad_group, 'adGroups/', 2)) AS ad_group_id,
keyword_ad_group_criterion,
ad_network_type,
device,
ad_id,
campaign_id,
COALESCE(clicks, 0) AS clicks,
COALESCE(cost_micros, 0) / 1000000.0 AS spend,
COALESCE(impressions, 0) AS impressions,
COALESCE(conversions, 0) AS conversions,
COALESCE(conversions_value, 0) AS conversions_value,
COALESCE(view_through_conversions, 0) AS view_through_conversions
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 Customer ID. |
date_day | date | The date being reported on. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_network_type | text | The Google Ad network type used across the account. |
keyword_ad_group_criterion | text | The ad group which the keyword criterion resides. |
device | text | Account ad performance per unique device where the ads were served. |
clicks | integer | The count of clicks. |
spend | numeric | The sum of your cost-per-click (CPC) and cost-per-thousand impressions (CPM) costs during this period. |
impressions | integer | The count of impressions. |
conversions | integer | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | integer | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | integer | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
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) project, specifically for the 'model.google_ads_source.stg_google_ads__ad_stats_tmp' model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
customer_id | bigint | None |
date | date | None |
ad_group_id | bigint | None |
ad_group | text | None |
keyword_ad_group_criterion | text | None |
ad_network_type | text | None |
device | text | None |
ad_id | bigint | None |
campaign_id | bigint | None |
clicks | integer | None |
cost_micros | integer | None |
impressions | integer | None |
This SQL query stages data for a Google Ads campaign history model. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types. The query then selects and renames certain columns, adds a source relation column, and creates an 'is_most_recent_record' flag using a window function. Finally, it filters for active records.
CleaningDeduplicationFilteringWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS advertising_channel_subtype,
CAST(NULL AS TEXT) AS advertising_channel_type,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS TEXT) AS end_date,
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS serving_status,
CAST(NULL AS TEXT) AS start_date,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS tracking_url_template,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS BOOLEAN) AS _fivetran_active,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS campaign_id,
updated_at,
name AS campaign_name,
customer_id AS account_id,
advertising_channel_type,
advertising_channel_subtype,
start_date,
end_date,
serving_status,
status,
tracking_url_template,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_most_recent_record
FROM fields
WHERE
COALESCE(_fivetran_active, TRUE)
)
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 representing the campaign, if present in the source data. |
updated_at | timestamp without time zone | Timestamp of when the record was last updated in Google Ads. |
advertising_channel_type | text | The type of advertising channel being used by the campaign. |
advertising_channel_subtype | text | The advertising channel subtype that is being used by the campaign. |
start_date | date | The start date of the campaign. |
end_date | date | The end date of the campaign. |
serving_status | text | Status of the ads and how they are currently being served. |
status | text | General status of the campaign. |
tracking_url_template | integer | The tracking url template being used throughout the campaign ads. |
campaign_name | text | The name of the campaign, if present in the source data. |
account_id | bigint | The Customer ID. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, initialized to NULL. The LIMIT 0 clause ensures no rows are returned. This query appears to be a placeholder or template, possibly used to define the structure of a temporary staging table in a dbt (data build tool) project for Google Ads data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
updated_at | timestamp without time zone | None |
name | text | None |
customer_id | bigint | None |
advertising_channel_type | text | None |
advertising_channel_subtype | text | None |
start_date | date | None |
end_date | date | None |
serving_status | text | None |
status | text | None |
tracking_url_template | integer | None |
This SQL query stages and transforms data from a Google Ads campaign stats source table. It casts columns to specific data types, renames some columns, and performs basic data cleaning by replacing NULL values with defaults for numeric columns. The query also calculates the spend by dividing cost_micros by 1,000,000. Finally, it selects and restructures the data into a final output format.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__campaign_stats_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_network_type,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS INT) AS cost_micros,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS device,
CAST(NULL AS INT) AS id,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS conversions,
CAST(NULL AS INT) AS conversions_value,
CAST(NULL AS INT) AS view_through_conversions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
customer_id AS account_id,
date AS date_day,
id AS campaign_id,
ad_network_type,
device,
COALESCE(clicks, 0) AS clicks,
COALESCE(cost_micros, 0) / 1000000.0 AS spend,
COALESCE(impressions, 0) AS impressions,
COALESCE(conversions, 0) AS conversions,
COALESCE(conversions_value, 0) AS conversions_value,
COALESCE(view_through_conversions, 0) AS view_through_conversions
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 Customer ID. |
date_day | date | The date being reported on. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_network_type | text | The Google Ad network type used across the account. |
device | text | Account ad performance per unique device where the ads were served. |
clicks | integer | The count of clicks. |
spend | numeric | The sum of your cost-per-click (CPC) and cost-per-thousand impressions (CPM) costs during this period. |
impressions | integer | The count of impressions. |
conversions | integer | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | integer | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | integer | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
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 output without any actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_id | text | None |
customer_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
active_view_impressions | integer | None |
active_view_measurability | integer | None |
active_view_measurable_cost_micros | integer | None |
active_view_measurable_impressions | integer | None |
active_view_viewability | integer | None |
ad_network_type | text | None |
base_campaign | text | None |
clicks | integer | None |
conversions | integer | None |
conversions_value | integer | None |
cost_micros | integer | None |
device | text | None |
id | bigint | None |
impressions | integer | None |
interaction_event_types | text | None |
interactions | integer | None |
view_through_conversions | integer | None |
This SQL query performs data transformation and cleaning on the Google Ads keyword stats data. It casts various fields to specific data types, renames some columns, and performs basic calculations such as converting cost from micros to regular units. The query also applies COALESCE to replace NULL values with zeros for numeric fields.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_google_ads_source.stg_google_ads__keyword_stats_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS INT) AS ad_group_criterion_criterion_id,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_network_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS INT) AS cost_micros,
CAST(NULL AS INT) AS customer_id,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS device,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS conversions,
CAST(NULL AS INT) AS conversions_value,
CAST(NULL AS INT) AS view_through_conversions,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
_fivetran_id AS keyword_id,
customer_id AS account_id,
date AS date_day,
CAST(ad_group_id AS TEXT) AS ad_group_id,
ad_group_criterion_criterion_id AS criterion_id,
campaign_id,
COALESCE(clicks, 0) AS clicks,
COALESCE(cost_micros, 0) / 1000000.0 AS spend,
COALESCE(impressions, 0) AS impressions,
COALESCE(conversions, 0) AS conversions,
COALESCE(conversions_value, 0) AS conversions_value,
COALESCE(view_through_conversions, 0) AS view_through_conversions
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 Customer ID. |
date_day | date | The date being reported on. |
ad_group_id | text | The ID representing the ad group, if present in the source data. |
keyword_id | text | The unique ID of the keyword record. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
criterion_id | bigint | Reference to the ad group criterion used for the keyword. |
clicks | integer | The count of clicks. |
spend | numeric | The sum of your cost-per-click (CPC) and cost-per-thousand impressions (CPM) costs during this period. |
impressions | integer | The count of impressions. |
conversions | integer | The number of conversions you've received, across your conversion actions. Conversions are measured with conversion tracking and may include [modeled](https://support.google.com/google-ads/answer/10081327?sjid=12862894247631803415-NC) conversions in cases where you are not able to observe all conversions that took place. You can use this column to see how often your ads led customers to actions that you’ve defined as valuable for your business. |
conversions_value | integer | The sum of monetary values for your `conversions`. You have to enter a value in the Google Ads UI for your conversion actions to make this metric useful. |
view_through_conversions | integer | For video campaigns, view-through conversions tell you when an _impression_ of your video ad leads to a conversion on your site. The last impression of a video ad will get credit for the view-through conversion. Keep in mind: An impression is different than a “view” of a video ad. A “view” is counted when someone watches 30 seconds (or the whole ad if it’s shorter than 30 seconds) or clicks on a part of the ad. A “view” that leads to a conversion is counted in the `conversions` column. |
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 LIMIT 0 clause ensures no rows are returned. This query is likely used as a template or placeholder in a dbt model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_id | text | None |
customer_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
active_view_impressions | integer | None |
active_view_measurability | integer | None |
active_view_measurable_cost_micros | integer | None |
active_view_measurable_impressions | integer | None |
active_view_viewability | integer | None |
ad_group_base_ad_group | text | None |
ad_group_criterion_criterion_id | bigint | None |
ad_group_id | bigint | None |
ad_network_type | text | None |
campaign_base_campaign | text | None |
campaign_id | bigint | None |
clicks | integer | None |
conversions | integer | None |
conversions_value | integer | None |
cost_micros | integer | None |
device | text | None |
impressions | integer | None |
interaction_event_types | text | None |
interactions | integer | None |
view_through_conversions | integer | None |
This SQL query creates a comprehensive account report for LinkedIn Ads. It combines data from account history, campaign history, and ad analytics. The query filters for the latest versions of accounts and campaigns, calculates total conversions, and then aggregates various metrics (conversions, clicks, impressions, cost, etc.) at the account level. It joins the analytics data with account and campaign information to provide a detailed view of ad performance for each account on a daily basis.
FilteringIntegrationFeaturizationAggregationWITH account AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history
WHERE
is_latest_version
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history
WHERE
is_latest_version
), report AS (
SELECT
*,
external_website_conversions + one_click_leads AS total_conversions
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_campaign
), final AS (
SELECT
report.source_relation,
report.date_day,
account.account_id,
account.account_name,
account.version_tag,
account.currency,
account.status,
account.type,
account.last_modified_at,
account.created_at,
SUM(report.total_conversions) AS total_conversions,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.cost) AS cost,
SUM(COALESCE(report.conversion_value_in_local_currency, 0)) AS conversion_value_in_local_currency,
SUM(COALESCE(external_website_conversions, 0)) AS external_website_conversions,
SUM(COALESCE(one_click_leads, 0)) AS one_click_leads
FROM report
LEFT JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
LEFT JOIN account
ON campaign.account_id = account.account_id
AND campaign.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
)
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_name | text | A label for the account. |
account_id | integer | Unique internal ID representing the account. |
date_day | timestamp without time zone | The day of the activity. |
clicks | bigint | The count of chargeable clicks. |
impressions | bigint | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | double precision | The cost of the ads in the local currency or USD. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the account was created. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
version_tag | numeric | An integer representation of the version of the account. |
status | text | ACTIVE - Account is active; this is the default state CANCELLED - Account has been permanently cancelled DRAFT - Account is in draft status, meaning it's not yet fully set up and it is not serving PENDING_DELETION - Denotes that the account has been requested to be deleted that is currently pending REMOVED - Denotes that the account was deleted, but must remain fetchable due to the existence of performance data. |
type | text | BUSINESS - This is the only value allowed when creating accounts through the API. ENTERPRISE - This value cannot be used to create accounts through the API and is reserved for accounts created by LinkedIn's internal ad operations systems. |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency, on the given day. |
total_conversions | numeric | The total conversions being brought in the conversion fields you select in your `linkedin_ads__conversion_fields`. |
external_website_conversions | numeric | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads. |
one_click_leads | numeric | Leads submitted after clicking on LinkedIn ads. |
This SQL query creates a comprehensive report of LinkedIn ad campaign group performance. It combines data from multiple staging tables, including campaign groups, campaigns, accounts, and ad analytics. The query filters for the latest versions of historical data, calculates total conversions, and aggregates various performance metrics (clicks, impressions, cost, conversions) at the campaign group level. The final output includes campaign group details, account information, and aggregated performance metrics for each day.
FilteringIntegrationFeaturizationAggregationWITH campaign_group AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_group_history
WHERE
is_latest_version
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history
WHERE
is_latest_version
), account AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history
WHERE
is_latest_version
), report AS (
SELECT
*,
external_website_conversions + one_click_leads AS total_conversions
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_campaign
), final AS (
SELECT
report.source_relation,
report.date_day,
campaign_group.campaign_group_id,
campaign_group.campaign_group_name,
account.account_id,
account.account_name,
campaign_group.status,
account.currency,
campaign_group.is_backfilled,
campaign_group.run_schedule_start_at,
campaign_group.run_schedule_end_at,
campaign_group.last_modified_at,
campaign_group.created_at,
SUM(report.total_conversions) AS total_conversions,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.cost) AS cost,
SUM(COALESCE(report.conversion_value_in_local_currency, 0)) AS conversion_value_in_local_currency,
SUM(COALESCE(external_website_conversions, 0)) AS external_website_conversions,
SUM(COALESCE(one_click_leads, 0)) AS one_click_leads
FROM report
LEFT JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
LEFT JOIN campaign_group
ON campaign.campaign_group_id = campaign_group.campaign_group_id
AND campaign.source_relation = campaign_group.source_relation
LEFT JOIN account
ON campaign.account_id = account.account_id
AND campaign.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
)
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_name | text | A label for the account. |
account_id | integer | Unique internal ID representing the account. |
date_day | timestamp without time zone | The day of the activity. |
clicks | bigint | The count of chargeable clicks. |
impressions | bigint | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | double precision | The cost of the ads in the local currency or USD. |
campaign_group_name | text | A label for the campaign group. |
campaign_group_id | integer | Unique internal ID representing the campaign group. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the campaign group was created. |
is_backfilled | boolean | Flag that denotes whether the campaign group was created organically or to backfill existing campaigns. This is a read-only field set by the system. Default = false. |
run_schedule_start_at | timestamp without time zone | Represents the inclusive (greater than or equal to) date when to start running the associated campaigns under this campaign group. This field is required. |
run_schedule_end_at | timestamp without time zone | Represents the exclusive (strictly less than) date when to stop running the associated campaigns under this campaign group. If this field is unset, it indicates an open range with no end date. |
status | text | Status of campaign group. Possible values are: ACTIVE - Denotes that the campaign group is capable of serving ads, subject to run date and budget limitations (as well as any other limitations at the account or campaign level). ARCHIVED - Denotes that the campaign group is presently inactive, and should mostly be hidden in the UI until un-archived. CANCELLED - Denotes that the campaign group has been permanently canceled and cannot be reactivated. Not a settable status. DRAFT - Denotes that the campaign group is in a preliminary state and should temporarily not be served. PAUSED - Denotes that the campaign group meets all requirements to be served, but temporarily should not be. PENDING_DELETION - Denotes that the campaign group has been requested to be deleted that is currently pending. REMOVED - Denoted that the campaign group was deleted, but must remain fetchable due to the existence of performance data. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency, on the given day. |
total_conversions | numeric | The total conversions being brought in the conversion fields you select in your `linkedin_ads__conversion_fields`. |
external_website_conversions | numeric | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads. |
one_click_leads | numeric | Leads submitted after clicking on LinkedIn ads. |
This SQL query integrates data from multiple LinkedIn Ads related tables (campaign, campaign group, account, and ad analytics) to create a comprehensive campaign report. It filters for the latest versions of historical data, performs joins to combine information from different sources, calculates total conversions, and aggregates various metrics (clicks, impressions, cost, conversions) at the campaign level. The final output includes detailed campaign information along with performance metrics.
FilteringIntegrationFeaturizationAggregationWITH campaign AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history
WHERE
is_latest_version
), campaign_group AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_group_history
WHERE
is_latest_version
), account AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history
WHERE
is_latest_version
), report AS (
SELECT
*,
external_website_conversions + one_click_leads AS total_conversions
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_campaign
), final AS (
SELECT
report.source_relation,
report.date_day,
report.campaign_id,
campaign.campaign_name,
campaign.version_tag,
campaign_group.campaign_group_id,
campaign_group.campaign_group_name,
account.account_id,
account.account_name,
campaign.status AS campaign_status,
campaign_group.status AS campaign_group_status,
campaign.type,
campaign.cost_type,
campaign.creative_selection,
campaign.daily_budget_amount,
campaign.daily_budget_currency_code,
campaign.unit_cost_amount,
campaign.unit_cost_currency_code,
account.currency,
campaign.format,
campaign.locale_country,
campaign.locale_language,
campaign.objective_type,
campaign.optimization_target_type,
campaign.is_audience_expansion_enabled,
campaign.is_offsite_delivery_enabled,
campaign.run_schedule_start_at,
campaign.run_schedule_end_at,
campaign.last_modified_at,
campaign.created_at,
SUM(report.total_conversions) AS total_conversions,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.cost) AS cost,
SUM(COALESCE(report.conversion_value_in_local_currency, 0)) AS conversion_value_in_local_currency,
SUM(COALESCE(external_website_conversions, 0)) AS external_website_conversions,
SUM(COALESCE(one_click_leads, 0)) AS one_click_leads
FROM report
LEFT JOIN campaign
ON report.campaign_id = campaign.campaign_id
AND report.source_relation = campaign.source_relation
LEFT JOIN campaign_group
ON campaign.campaign_group_id = campaign_group.campaign_group_id
AND campaign.source_relation = campaign_group.source_relation
LEFT JOIN account
ON campaign.account_id = account.account_id
AND campaign.source_relation = account.source_relation
GROUP BY
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
)
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_name | text | A label for the account. |
account_id | integer | Unique internal ID representing the account. |
date_day | timestamp without time zone | The day of the activity. |
clicks | bigint | The count of chargeable clicks. |
impressions | bigint | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | double precision | The cost of the ads in the local currency or USD. |
campaign_group_name | text | A label for the campaign group. |
campaign_group_id | integer | Unique internal ID representing the campaign group. |
campaign_name | text | A label for the campaign. |
campaign_id | integer | Unique internal ID representing the campaign. |
campaign_group_status | text | Status of campaign's campaign group. Possible values are: ACTIVE - Denotes that the campaign group is capable of serving ads, subject to run date and budget limitations (as well as any other limitations at the account or campaign level). ARCHIVED - Denotes that the campaign group is presently inactive, and should mostly be hidden in the UI until un-archived. CANCELLED - Denotes that the campaign group has been permanently canceled and cannot be reactivated. Not a settable status. DRAFT - Denotes that the campaign group is in a preliminary state and should temporarily not be served. PAUSED - Denotes that the campaign group meets all requirements to be served, but temporarily should not be. PENDING_DELETION - Denotes that the campaign group has been requested to be deleted that is currently pending. REMOVED - Denoted that the campaign group was deleted, but must remain fetchable due to the existence of performance data. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the campaign was created. |
version_tag | numeric | An integer representation of the version of the campaign. |
is_audience_expansion_enabled | boolean | Boolean (default=false) representing whether Audience Expansion is enabled for the campaign provides query expansion for certain targeting criteria. |
cost_type | text | CPM - Cost per thousand advertising impressions. If type=SPONSORED_INMAILS; cost per send(CPS) is measured as CPM x 1000. CPC- Cost per individual click on the associated link. CPV- Cost per view for video ads. |
creative_selection | text | ROUND_ROBIN - Rotate through available creatives to serve them as evenly as possible. OPTIMIZED (default) - Bias selection taking into account such as expected performance. Not available for Message and Conversation Ads (type=SPONSORED_INMAILS). |
daily_budget_amount | double precision | Maximum amount to spend per day UTC. The amount of money as a real number string. |
daily_budget_currency_code | text | ISO currency code. The currency must match that of the parent account. |
format | text | The ad format on the campaign level. See https://docs.microsoft.com/en-us/linkedin/marketing/integrations/ads/campaign-formats?view=li-lms-2022-07 |
locale_country | text | Locale of the campaign. An uppercase two-letter country code as defined by ISO-3166. |
locale_language | text | Locale of the campaign. A lowercase two-letter language code as defined by ISO-639. |
objective_type | text | Campaign Objective type values. Click [here](https://docs.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/ad-budget-pricing-type-combinations?view=li-lms-2022-07) for Campaign Objective descriptions. One of: - BRAND_AWARENESS - ENGAGEMENT - JOB_APPLICANTS - LEAD_GENERATION - WEBSITE_CONVERSIONS - WEBSITE_VISITS - VIDEO_VIEWS |
is_offsite_delivery_enabled | boolean | Boolean indicating if this campaign to allowed to be served on the LinkedIn Audience Network to extend the reach of your campaign by delivering ads beyond the LinkedIn feed to members on third-party apps and sites. (default will be "True" from June 30, 2022) |
optimization_target_type | text | Determines how this campaign is optimized for spending. If this is not set, there is no optimization. Refer to the documentation here. Default = 'NONE'. |
run_schedule_start_at | timestamp without time zone | Scheduled date range to run associated creatives. The start date must be non-null. Represents the inclusive (greater than or equal to) value in which to start the range. |
run_schedule_end_at | timestamp without time zone | Scheduled date range to run associated creatives. The start date must be non-null. Represents the exclusive (strictly less than) value in which to end the range, if provided. |
campaign_status | text | ACTIVE - Denotes that the campaign is fully servable. PAUSED - Denotes that the campaign meets all requirements to be served, but temporarily should not be. ARCHIVED - Denotes that the campaign is presently inactive, and should mostly be hidden in the UI until un-archived. COMPLETED - Denotes that the campaign has reached a specified budgetary or chronological limit. CANCELED - Denotes that the campaign has been permanently canceled, such as when an advertising account is permanently closed. DRAFT - Denotes that the campaign is still being edited and not eligible for serving. Some validation will be postponed until the campaign is activated. PENDING_DELETION - Denotes that the campaign has been requested to be deleted that is currently pending. REMOVED - Denotes that the campaign was deleted, but must remain fetchable due to the existence of performance data. |
type | text | TEXT_AD - Text-based ads that show up in the right column or top of the page on LinkedIn. SPONSORED_UPDATES - Native ads that promote a company's content updates in the LinkedIn feed. SPONSORED_INMAILS - Personalized messages with a call-to-action button delivered to a LinkedIn's member inbox. DYNAMIC - Ads that are dynamically personalized. |
unit_cost_amount | double precision | This value is used as one of the following: amount to bid (for manual bidding), amount which is the target cost (for target cost bidding) per click, impression, or other event depending on the pricing model, or cost cap (for cost cap bidding). The amount of money as a real number string. The amount should be non-negative if the bidding strategy is manual, target cost, or cost cap bidding. The default is 0 with the currency code set to match that of the associated account. |
unit_cost_currency_code | text | Amount to bid per click, impression, or other event depending on the pricing model. The default is 0 with the currency code set to match that of the associated account. ISO currency code. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency, on the given day. |
total_conversions | numeric | The total conversions being brought in the conversion fields you select in your `linkedin_ads__conversion_fields`. |
external_website_conversions | numeric | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads. |
one_click_leads | numeric | Leads submitted after clicking on LinkedIn ads. |
This SQL query creates a comprehensive report of LinkedIn ad performance by combining data from multiple sources. It joins creative, campaign, campaign group, and account data with ad analytics, calculates total conversions, and aggregates metrics such as clicks, impressions, cost, and conversion values. The final result provides a detailed view of ad performance across different levels of the advertising hierarchy.
IntegrationAggregationFeaturizationWITH creative AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__creative_history
WHERE
is_latest_version
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history
WHERE
is_latest_version
), campaign_group AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_group_history
WHERE
is_latest_version
), account AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history
WHERE
is_latest_version
), report AS (
SELECT
*,
external_website_conversions + one_click_leads AS total_conversions
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_creative
), final AS (
SELECT
report.source_relation,
report.date_day,
report.creative_id,
campaign.campaign_id,
campaign.campaign_name,
campaign_group.campaign_group_id,
campaign_group.campaign_group_name,
account.account_id,
account.account_name,
creative.click_uri,
creative.status AS creative_status,
campaign.status AS campaign_status,
campaign_group.status AS campaign_group_status,
account.currency,
creative.last_modified_at,
creative.created_at,
SUM(report.total_conversions) AS total_conversions,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.cost) AS cost,
SUM(COALESCE(report.conversion_value_in_local_currency, 0)) AS conversion_value_in_local_currency,
SUM(COALESCE(external_website_conversions, 0)) AS external_website_conversions,
SUM(COALESCE(one_click_leads, 0)) AS one_click_leads
FROM report
LEFT JOIN creative
ON report.creative_id = creative.creative_id
AND report.source_relation = creative.source_relation
LEFT JOIN campaign
ON creative.campaign_id = campaign.campaign_id
AND creative.source_relation = campaign.source_relation
LEFT JOIN campaign_group
ON campaign.campaign_group_id = campaign_group.campaign_group_id
AND campaign.source_relation = campaign_group.source_relation
LEFT JOIN account
ON campaign.account_id = account.account_id
AND campaign.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
)
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_name | text | A label for the account. |
account_id | integer | Unique internal ID representing the account. |
date_day | timestamp without time zone | The day of the activity. |
clicks | bigint | The count of chargeable clicks. |
impressions | bigint | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | bigint | The cost of the ads in the local currency or USD. |
campaign_group_name | text | A label for the campaign group. |
campaign_group_id | integer | Unique internal ID representing the campaign group. |
campaign_name | text | A label for the creative's campaign. |
campaign_id | integer | Unique internal ID representing the creative's campaign. |
creative_id | integer | Unique internal ID representing the creative. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the creative was created. |
creative_status | text | ACTIVE - Creative creation is complete and creative is available for review and serve. PAUSED - Creative creation is complete and creative is current, but should temporarily not be served. This status is not valid upon creation. DRAFT - Creative creation is incomplete and may still be edited. ARCHIVED - Creative creation is complete, but creative should not be served and should be separated from non-archived creatives in any UI. CANCELED - Creative is permanently canceled. |
click_uri | text | Required when creative type is TEXT_AD. Maximum character limit of 500 after URL encoding all special characters. Limit does not include the "https://" prefix. |
campaign_group_status | text | Status of campaign's campaign group. Possible values are: ACTIVE - Denotes that the campaign group is capable of serving ads, subject to run date and budget limitations (as well as any other limitations at the account or campaign level). ARCHIVED - Denotes that the campaign group is presently inactive, and should mostly be hidden in the UI until un-archived. CANCELLED - Denotes that the campaign group has been permanently canceled and cannot be reactivated. Not a settable status. DRAFT - Denotes that the campaign group is in a preliminary state and should temporarily not be served. PAUSED - Denotes that the campaign group meets all requirements to be served, but temporarily should not be. PENDING_DELETION - Denotes that the campaign group has been requested to be deleted that is currently pending. REMOVED - Denoted that the campaign group was deleted, but must remain fetchable due to the existence of performance data. |
campaign_status | text | ACTIVE - Denotes that the campaign is fully servable. PAUSED - Denotes that the campaign meets all requirements to be served, but temporarily should not be. ARCHIVED - Denotes that the campaign is presently inactive, and should mostly be hidden in the UI until un-archived. COMPLETED - Denotes that the campaign has reached a specified budgetary or chronological limit. CANCELED - Denotes that the campaign has been permanently canceled, such as when an advertising account is permanently closed. DRAFT - Denotes that the campaign is still being edited and not eligible for serving. Some validation will be postponed until the campaign is activated. PENDING_DELETION - Denotes that the campaign has been requested to be deleted that is currently pending. REMOVED - Denotes that the campaign was deleted, but must remain fetchable due to the existence of performance data. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency, on the given day. |
total_conversions | numeric | The total conversions being brought in the conversion fields you select in your `linkedin_ads__conversion_fields`. |
external_website_conversions | numeric | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads. |
one_click_leads | numeric | Leads submitted after clicking on LinkedIn ads. |
This SQL query integrates data from multiple LinkedIn Ads-related tables (creative, campaign, campaign_group, account, and ad analytics) to create a comprehensive URL report. It joins these tables, calculates total conversions, and aggregates various metrics (conversions, clicks, impressions, cost) by date, URL details, campaign information, and account details. The query filters out records where the click_uri is null and groups the results by relevant dimensions.
IntegrationAggregationFilteringFeaturizationWITH creative AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__creative_history
WHERE
is_latest_version
), campaign AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history
WHERE
is_latest_version
), campaign_group AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_group_history
WHERE
is_latest_version
), account AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history
WHERE
is_latest_version
), report AS (
SELECT
*,
external_website_conversions + one_click_leads AS total_conversions
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_creative
), final AS (
SELECT
report.source_relation,
report.date_day,
creative.click_uri,
creative.base_url,
creative.url_host,
creative.url_path,
creative.utm_source,
creative.utm_medium,
creative.utm_campaign,
creative.utm_content,
creative.utm_term,
report.creative_id,
campaign.campaign_id,
campaign.campaign_name,
campaign_group.campaign_group_id,
campaign_group.campaign_group_name,
account.account_id,
account.account_name,
account.currency,
SUM(report.total_conversions) AS total_conversions,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.cost) AS cost,
SUM(COALESCE(report.conversion_value_in_local_currency, 0)) AS conversion_value_in_local_currency,
SUM(COALESCE(external_website_conversions, 0)) AS external_website_conversions,
SUM(COALESCE(one_click_leads, 0)) AS one_click_leads
FROM report
LEFT JOIN creative
ON report.creative_id = creative.creative_id
AND report.source_relation = creative.source_relation
LEFT JOIN campaign
ON creative.campaign_id = campaign.campaign_id
AND creative.source_relation = campaign.source_relation
LEFT JOIN campaign_group
ON campaign.campaign_group_id = campaign_group.campaign_group_id
AND campaign.source_relation = campaign_group.source_relation
LEFT JOIN account
ON campaign.account_id = account.account_id
AND campaign.source_relation = account.source_relation
WHERE
NOT creative.click_uri IS NULL
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
)
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_name | text | A label for the account. |
account_id | integer | Unique internal ID representing the account. |
date_day | timestamp without time zone | The day of the activity. |
clicks | bigint | The count of chargeable clicks. |
impressions | bigint | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | bigint | The cost of the ads in the local currency or USD. |
campaign_group_name | text | A label for the campaign group. |
campaign_group_id | integer | Unique internal ID representing the campaign group. |
creative_id | integer | Unique internal ID representing the creative. |
click_uri | text | Required when creative type is TEXT_AD. Maximum character limit of 500 after URL encoding all special characters. Limit does not include the "https://" prefix. |
base_url | text | The base URL of the ad, extracted from the `click_uri`. |
url_host | text | The URL host of the ad, extracted from the `click_uri`. |
url_path | text | The URL path of the ad, extracted from the `click_uri`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `click_uri`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `click_uri`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `click_uri`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `click_uri`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `click_uri`. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
campaign_name | text | A label for the creative's campaign. |
campaign_id | integer | Unique internal ID representing the creative's campaign. |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency, on the given day. |
total_conversions | numeric | The total conversions being brought in the conversion fields you select in your `linkedin_ads__conversion_fields`. |
external_website_conversions | numeric | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads. |
one_click_leads | numeric | Leads submitted after clicking on LinkedIn ads. |
This SQL query performs several operations on LinkedIn ads account history data. It starts by creating a base CTE from a temporary staging table. Then, it defines a macro CTE with null or empty values for all columns. The fields CTE then selects and casts various columns from the macro CTE, renames some columns, and adds an is_latest_version column using a window function. Finally, it selects all columns from the fields CTE.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__account_history_tmp
), macro AS (
SELECT
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS last_modified_time,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TEXT) AS version_tag,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
id AS account_id,
name AS account_name,
currency,
CAST(version_tag AS DECIMAL) AS version_tag,
status,
type,
CAST(last_modified_time AS TIMESTAMP) AS last_modified_at,
CAST(created_time AS TIMESTAMP) AS created_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_modified_time DESC) = 1 AS is_latest_version
FROM macro
)
SELECT
*
FROM fields
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 | integer | Unique internal ID representing the account. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the account was created. |
account_name | text | A label for the account. |
currency | text | The ISO 4217 currency code of the account, such as 'USD' for United States Dollar (default). |
version_tag | numeric | An integer representation of the version of the account. |
status | text | ACTIVE - Account is active; this is the default state CANCELLED - Account has been permanently cancelled DRAFT - Account is in draft status, meaning it's not yet fully set up and it is not serving PENDING_DELETION - Denotes that the account has been requested to be deleted that is currently pending REMOVED - Denotes that the account was deleted, but must remain fetchable due to the existence of performance data. |
type | text | BUSINESS - This is the only value allowed when creating accounts through the API. ENTERPRISE - This value cannot be used to create accounts through the API and is reserved for accounts created by LinkedIn's internal ad operations systems. |
is_latest_version | boolean | Boolean of whether the record is the latest version of the account. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, set to NULL. The query is likely used as a placeholder or template for further development, or to initialize a temporary table structure without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_modified_time | timestamp without time zone | None |
created_time | timestamp without time zone | None |
name | text | None |
currency | text | None |
version_tag | integer | None |
This SQL query performs data cleaning and featurization on LinkedIn ads analytics data. It casts various fields to specific data types, truncates the 'day' field to represent only the date, and calculates or transforms several metrics such as cost, conversion value, and lead counts. The query also adds a source_relation field and renames some columns for clarity.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_campaign_tmp
), macro AS (
SELECT
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DECIMAL(28, 6)) AS cost_in_local_currency,
CAST(NULL AS DECIMAL(28, 6)) AS cost_in_usd,
CAST(NULL AS TIMESTAMP) AS day,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS conversion_value_in_local_currency,
CAST(NULL AS TEXT) AS external_website_conversions,
CAST(NULL AS TEXT) AS one_click_leads,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
DATE_TRUNC('DAY', day) AS date_day,
campaign_id,
clicks,
impressions,
cost_in_usd AS cost,
COALESCE(CAST(conversion_value_in_local_currency AS FLOAT), 0) AS conversion_value_in_local_currency,
COALESCE(CAST(external_website_conversions AS BIGINT), 0) AS external_website_conversions,
COALESCE(CAST(one_click_leads AS BIGINT), 0) AS one_click_leads
FROM macro
)
SELECT
*
FROM fields
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 | integer | The ID of the related creative. |
date_day | timestamp without time zone | The day of the activity. |
clicks | integer | The count of chargeable clicks. |
impressions | integer | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | double precision | The cost of the ads in the local currency or USD. |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency. |
external_website_conversions | bigint | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads |
one_click_leads | bigint | Leads submitted after clicking on LinkedIn ads. |
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 LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
campaign_id | integer | None |
day | timestamp without time zone | None |
_fivetran_synced | text | None |
action_clicks | integer | None |
ad_unit_clicks | integer | None |
approximate_unique_impressions | integer | None |
card_clicks | integer | None |
card_impressions | integer | None |
clicks | integer | None |
comment_likes | integer | None |
comments | integer | None |
company_page_clicks | integer | None |
conversion_value_in_local_currency | integer | None |
cost_in_local_currency | double precision | None |
cost_in_usd | double precision | None |
external_website_conversions | integer | None |
external_website_post_click_conversions | integer | None |
external_website_post_view_conversions | integer | None |
follows | integer | None |
full_screen_plays | integer | None |
impressions | integer | None |
landing_page_clicks | integer | None |
lead_generation_mail_contact_info_shares | integer | None |
lead_generation_mail_interested_clicks | integer | None |
likes | integer | None |
one_click_lead_form_opens | integer | None |
one_click_leads | integer | None |
opens | integer | None |
other_engagements | integer | None |
shares | integer | None |
text_url_clicks | integer | None |
total_engagements | integer | None |
video_completions | integer | None |
video_first_quartile_completions | integer | None |
video_midpoint_completions | integer | None |
video_starts | integer | None |
video_third_quartile_completions | integer | None |
video_views | integer | None |
viral_card_clicks | integer | None |
viral_card_impressions | integer | None |
viral_clicks | integer | None |
viral_comment_likes | integer | None |
viral_comments | integer | None |
viral_company_page_clicks | integer | None |
viral_external_website_conversions | integer | None |
viral_external_website_post_click_conversions | integer | None |
viral_external_website_post_view_conversions | integer | None |
viral_follows | integer | None |
viral_full_screen_plays | integer | None |
viral_impressions | integer | None |
viral_landing_page_clicks | integer | None |
viral_likes | integer | None |
viral_one_click_lead_form_opens | integer | None |
viral_one_click_leads | integer | None |
viral_other_engagements | integer | None |
viral_shares | integer | None |
viral_total_engagements | integer | None |
viral_video_completions | integer | None |
viral_video_first_quartile_completions | integer | None |
viral_video_midpoint_completions | integer | None |
viral_video_starts | integer | None |
viral_video_third_quartile_completions | integer | None |
viral_video_views | integer | None |
This SQL query stages data from a LinkedIn Ads source table. It performs type casting, date truncation, and column renaming. The query also applies COALESCE functions to handle potential NULL values in certain columns, converting them to default values (0 for numeric columns). The final SELECT statement retrieves all columns from the 'fields' CTE.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__ad_analytics_by_creative_tmp
), macro AS (
SELECT
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DECIMAL(28, 6)) AS cost_in_local_currency,
CAST(NULL AS DECIMAL(28, 6)) AS cost_in_usd,
CAST(NULL AS INT) AS creative_id,
CAST(NULL AS TIMESTAMP) AS day,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS conversion_value_in_local_currency,
CAST(NULL AS TEXT) AS external_website_conversions,
CAST(NULL AS TEXT) AS one_click_leads,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
DATE_TRUNC('DAY', day) AS date_day,
creative_id,
clicks,
impressions,
cost_in_usd AS cost,
COALESCE(CAST(conversion_value_in_local_currency AS FLOAT), 0) AS conversion_value_in_local_currency,
COALESCE(CAST(external_website_conversions AS BIGINT), 0) AS external_website_conversions,
COALESCE(CAST(one_click_leads AS BIGINT), 0) AS one_click_leads
FROM macro
)
SELECT
*
FROM fields
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. |
creative_id | integer | The ID of the related creative. |
date_day | timestamp without time zone | The day of the activity. |
clicks | integer | The count of chargeable clicks. |
impressions | integer | This is the count of "impressions" for Direct Ads and Sponsored Updates and "sends" for InMails. |
cost | integer | The cost of the ads in the local currency or USD. |
conversion_value_in_local_currency | double precision | The value generated by your conversions, displayed in your LinkedIn account's local currency. |
external_website_conversions | bigint | The actions taken on your website that you've defined as valuable to your business after clicking on LinkedIn ads |
one_click_leads | bigint | Leads submitted after clicking on LinkedIn ads. |
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 LIMIT 0 clause ensures that no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
creative_id | integer | None |
day | timestamp without time zone | None |
clicks | integer | None |
impressions | integer | None |
cost_in_local_currency | integer | None |
cost_in_usd | integer | None |
This SQL query stages data from a LinkedIn Ads campaign group history source. It casts and renames columns, converts timestamp fields, adds a flag for the latest version of each campaign group, and prepares the data for further processing or analysis.
CleaningDeduplicationFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_group_history_tmp
), macro AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS BOOLEAN) AS backfilled,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS last_modified_time,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS run_schedule_end,
CAST(NULL AS TIMESTAMP) AS run_schedule_start,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
id AS campaign_group_id,
name AS campaign_group_name,
account_id,
status,
backfilled AS is_backfilled,
CAST(run_schedule_start AS TIMESTAMP) AS run_schedule_start_at,
CAST(run_schedule_end AS TIMESTAMP) AS run_schedule_end_at,
CAST(last_modified_time AS TIMESTAMP) AS last_modified_at,
CAST(created_time AS TIMESTAMP) AS created_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_modified_time DESC) = 1 AS is_latest_version
FROM macro
)
SELECT
*
FROM fields
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_group_id | integer | Unique internal ID representing the campaign group. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
account_id | bigint | The ID of the account the campaign group belongs to. |
created_at | timestamp without time zone | The timestamp of when the campaign group was created. |
campaign_group_name | text | A label for the campaign group. |
is_backfilled | boolean | Flag that denotes whether the campaign group was created organically or to backfill existing campaigns. This is a read-only field set by the system. Default = false. |
run_schedule_start_at | timestamp without time zone | Represents the inclusive (greater than or equal to) date when to start running the associated campaigns under this campaign group. This field is required. |
run_schedule_end_at | timestamp without time zone | Represents the exclusive (strictly less than) date when to stop running the associated campaigns under this campaign group. If this field is unset, it indicates an open range with no end date. |
status | text | Status of campaign group. Possible values are: ACTIVE - Denotes that the campaign group is capable of serving ads, subject to run date and budget limitations (as well as any other limitations at the account or campaign level). ARCHIVED - Denotes that the campaign group is presently inactive, and should mostly be hidden in the UI until un-archived. CANCELLED - Denotes that the campaign group has been permanently canceled and cannot be reactivated. Not a settable status. DRAFT - Denotes that the campaign group is in a preliminary state and should temporarily not be served. PAUSED - Denotes that the campaign group meets all requirements to be served, but temporarily should not be. PENDING_DELETION - Denotes that the campaign group has been requested to be deleted that is currently pending. REMOVED - Denoted that the campaign group was deleted, but must remain fetchable due to the existence of performance data. |
is_latest_version | boolean | Boolean of whether the record is the latest version of the campaign group. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, set to NULL. The query is likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for LinkedIn ads campaign group history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_modified_time | timestamp without time zone | None |
account_id | bigint | None |
created_time | timestamp without time zone | None |
name | text | None |
This SQL query stages data from a LinkedIn Ads campaign history source. It begins by selecting all columns from a temporary staging table, then defines a set of columns with specific data types using a CTE named 'macro'. The 'fields' CTE then selects and renames columns, casts some to appropriate data types, and adds a flag to identify the latest version of each campaign. The final SELECT statement returns all columns from the 'fields' CTE.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__campaign_history_tmp
), macro AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS BOOLEAN) AS audience_expansion_enabled,
CAST(NULL AS INT) AS campaign_group_id,
CAST(NULL AS TEXT) AS cost_type,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TEXT) AS creative_selection,
CAST(NULL AS FLOAT) AS daily_budget_amount,
CAST(NULL AS TEXT) AS daily_budget_currency_code,
CAST(NULL AS TEXT) AS format,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS last_modified_time,
CAST(NULL AS TEXT) AS locale_country,
CAST(NULL AS TEXT) AS locale_language,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS objective_type,
CAST(NULL AS BOOLEAN) AS offsite_delivery_enabled,
CAST(NULL AS TEXT) AS optimization_target_type,
CAST(NULL AS TIMESTAMP) AS run_schedule_end,
CAST(NULL AS TIMESTAMP) AS run_schedule_start,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS FLOAT) AS unit_cost_amount,
CAST(NULL AS TEXT) AS unit_cost_currency_code,
CAST(NULL AS TEXT) AS version_tag,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
id AS campaign_id,
name AS campaign_name,
CAST(version_tag AS DECIMAL) AS version_tag,
campaign_group_id,
account_id,
status,
type,
cost_type,
creative_selection,
daily_budget_amount,
daily_budget_currency_code,
unit_cost_amount,
unit_cost_currency_code,
format,
locale_country,
locale_language,
objective_type,
optimization_target_type,
audience_expansion_enabled AS is_audience_expansion_enabled,
offsite_delivery_enabled AS is_offsite_delivery_enabled,
CAST(run_schedule_start AS TIMESTAMP) AS run_schedule_start_at,
CAST(run_schedule_end AS TIMESTAMP) AS run_schedule_end_at,
CAST(last_modified_time AS TIMESTAMP) AS last_modified_at,
CAST(created_time AS TIMESTAMP) AS created_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_modified_time DESC) = 1 AS is_latest_version
FROM macro
)
SELECT
*
FROM fields
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 | integer | Unique internal ID representing the campaign. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
account_id | bigint | The ID of the account the campaign belongs to. |
campaign_group_id | integer | The ID of the campaign group the campaign belongs to. |
created_at | timestamp without time zone | The timestamp of when the campaign was created. |
campaign_name | text | A label for the campaign. |
version_tag | numeric | An integer representation of the version of the campaign. |
is_audience_expansion_enabled | boolean | Boolean (default=false) representing whether Audience Expansion is enabled for the campaign provides query expansion for certain targeting criteria. |
cost_type | text | CPM - Cost per thousand advertising impressions. If type=SPONSORED_INMAILS; cost per send(CPS) is measured as CPM x 1000. CPC- Cost per individual click on the associated link. CPV- Cost per view for video ads. |
creative_selection | text | ROUND_ROBIN - Rotate through available creatives to serve them as evenly as possible. OPTIMIZED (default) - Bias selection taking into account such as expected performance. Not available for Message and Conversation Ads (type=SPONSORED_INMAILS). |
daily_budget_amount | double precision | Maximum amount to spend per day UTC. The amount of money as a real number string. |
daily_budget_currency_code | text | ISO currency code. The currency must match that of the parent account. |
format | text | The ad format on the campaign level. See https://docs.microsoft.com/en-us/linkedin/marketing/integrations/ads/campaign-formats?view=li-lms-2022-07 |
locale_country | text | Locale of the campaign. An uppercase two-letter country code as defined by ISO-3166. |
locale_language | text | Locale of the campaign. A lowercase two-letter language code as defined by ISO-639. |
objective_type | text | Campaign Objective type values. Click [here](https://docs.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/ad-budget-pricing-type-combinations?view=li-lms-2022-07) for Campaign Objective descriptions. One of: - BRAND_AWARENESS - ENGAGEMENT - JOB_APPLICANTS - LEAD_GENERATION - WEBSITE_CONVERSIONS - WEBSITE_VISITS - VIDEO_VIEWS |
is_offsite_delivery_enabled | boolean | Boolean indicating if this campaign to allowed to be served on the LinkedIn Audience Network to extend the reach of your campaign by delivering ads beyond the LinkedIn feed to members on third-party apps and sites. (default will be "True" from June 30, 2022) |
optimization_target_type | text | Determines how this campaign is optimized for spending. If this is not set, there is no optimization. Refer to the documentation here. Default = 'NONE'. |
run_schedule_start_at | timestamp without time zone | Scheduled date range to run associated creatives. The start date must be non-null. Represents the inclusive (greater than or equal to) value in which to start the range. |
run_schedule_end_at | timestamp without time zone | Scheduled date range to run associated creatives. The start date must be non-null. Represents the exclusive (strictly less than) value in which to end the range, if provided. |
status | text | ACTIVE - Denotes that the campaign is fully servable. PAUSED - Denotes that the campaign meets all requirements to be served, but temporarily should not be. ARCHIVED - Denotes that the campaign is presently inactive, and should mostly be hidden in the UI until un-archived. COMPLETED - Denotes that the campaign has reached a specified budgetary or chronological limit. CANCELED - Denotes that the campaign has been permanently canceled, such as when an advertising account is permanently closed. DRAFT - Denotes that the campaign is still being edited and not eligible for serving. Some validation will be postponed until the campaign is activated. PENDING_DELETION - Denotes that the campaign has been requested to be deleted that is currently pending. REMOVED - Denotes that the campaign was deleted, but must remain fetchable due to the existence of performance data. |
type | text | TEXT_AD - Text-based ads that show up in the right column or top of the page on LinkedIn. SPONSORED_UPDATES - Native ads that promote a company's content updates in the LinkedIn feed. SPONSORED_INMAILS - Personalized messages with a call-to-action button delivered to a LinkedIn's member inbox. DYNAMIC - Ads that are dynamically personalized. |
unit_cost_amount | double precision | This value is used as one of the following: amount to bid (for manual bidding), amount which is the target cost (for target cost bidding) per click, impression, or other event depending on the pricing model, or cost cap (for cost cap bidding). The amount of money as a real number string. The amount should be non-negative if the bidding strategy is manual, target cost, or cost cap bidding. The default is 0 with the currency code set to match that of the associated account. |
unit_cost_currency_code | text | Amount to bid per click, impression, or other event depending on the pricing model. The default is 0 with the currency code set to match that of the associated account. ISO currency code. |
is_latest_version | boolean | Boolean of whether the record is the latest version of the campaign. |
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 development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_modified_time | timestamp without time zone | None |
account_id | bigint | None |
campaign_group_id | integer | None |
created_time | timestamp without time zone | None |
name | text | None |
version_tag | integer | None |
This SQL query processes data from a LinkedIn Ads creative history table. It performs several operations: 1. Casts and renames columns for consistency 2. Selects the latest version of each creative based on last modified time 3. Extracts various URL components and UTM parameters from the click_uri field 4. Cleans and standardizes data formats
CleaningDeduplicationFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_linkedin_ads_source.stg_linkedin_ads__creative_history_tmp
), macro AS (
SELECT
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TEXT) AS click_uri,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS INT) AS id,
CAST(NULL AS TIMESTAMP) AS last_modified_time,
CAST(NULL AS TIMESTAMP) AS last_modified_at,
CAST(NULL AS TEXT) AS intended_status,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), fields AS (
SELECT
source_relation,
id AS creative_id,
campaign_id,
COALESCE(status, intended_status) AS status,
click_uri,
CAST(COALESCE(last_modified_time, last_modified_at) AS TIMESTAMP) AS last_modified_at,
CAST(COALESCE(created_time, created_at) AS TIMESTAMP) AS created_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY COALESCE(last_modified_time, last_modified_at) DESC) = 1 AS is_latest_version
FROM macro
), url_fields AS (
SELECT
*,
SPLIT_PART(click_uri, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(click_uri, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(click_uri, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(click_uri, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(click_uri, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(click_uri, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(click_uri, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(click_uri, 'utm_term=', 2), '&', 1), '') AS utm_term
FROM fields
)
SELECT
*
FROM url_fields
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. |
creative_id | integer | Unique internal ID representing the creative. |
last_modified_at | timestamp without time zone | The timestamp of when the version was updated. |
created_at | timestamp without time zone | The timestamp of when the creative was created. |
campaign_id | integer | The ID of the campaign the creative belongs to. |
status | text | ACTIVE - Creative creation is complete and creative is available for review and serve. PAUSED - Creative creation is complete and creative is current, but should temporarily not be served. This status is not valid upon creation. DRAFT - Creative creation is incomplete and may still be edited. ARCHIVED - Creative creation is complete, but creative should not be served and should be separated from non-archived creatives in any UI. CANCELED - Creative is permanently canceled. |
click_uri | text | Required when creative type is TEXT_AD. Maximum character limit of 500 after URL encoding all special characters. Limit does not include the "https://" prefix. |
base_url | text | The base URL of the ad, extracted from the `click_uri`. |
url_host | text | The URL host of the ad, extracted from the `click_uri`. |
url_path | text | The URL path of the ad, extracted from the `click_uri`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `click_uri`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `click_uri`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `click_uri`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `click_uri`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `click_uri`. |
is_latest_version | boolean | Boolean of whether the record is the latest version of the creative. |
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 dbt (data build tool).
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
last_modified_time | timestamp without time zone | None |
created_time | timestamp without time zone | None |
campaign_id | integer | None |
type | text | None |
version_tag | integer | None |
status | text | None |
click_uri | text | None |
This SQL query combines daily account report data with the most recent account history information. It joins these two datasets, groups the results by various dimensions (including date, account details, device information, and network), and calculates aggregate metrics such as clicks, impressions, and spend. The query provides a comprehensive view of account performance with relevant account details.
IntegrationAggregationFilteringWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_daily_report
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
accounts.time_zone AS account_timezone,
report.device_os,
report.device_type,
report.network,
report.currency_code,
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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
account_timezone | text | The time zone associated with this account. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query combines data from multiple staging tables to create a comprehensive ad group report. It joins ad group daily report data with the most recent records from ad groups, campaigns, and accounts. The query then aggregates metrics like clicks, impressions, and spend, grouping them by various dimensions such as date, account, campaign, ad group, device, and network information.
IntegrationAggregationFilteringWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_daily_report
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
report.device_os,
report.device_type,
report.network,
report.currency_code,
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 campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates data from multiple Microsoft Ads related tables (ad daily report, ad history, ad group history, campaign history, and account history) to create a comprehensive ad performance report. It filters for the most recent records in the history tables, joins these tables with the daily report data, and aggregates metrics such as clicks, impressions, and spend. The result is a detailed view of ad performance across various dimensions including account, campaign, ad group, ad, device, and network.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_daily_report
), ads AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ads.ad_name,
report.ad_id,
ads.type AS ad_type,
report.device_os,
report.device_type,
report.network,
report.currency_code,
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 ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
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,
10,
11,
12,
13,
14,
15
)
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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
ad_type | text | The type of the ad in Google Ads. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates data from three sources: campaign daily reports, campaign history, and account history. It joins these tables based on account and campaign IDs, filtering for the most recent records in the history tables. The query then aggregates data, calculating sums for clicks, impressions, and spend, while grouping by various dimensions such as date, account, campaign, device, and network information. The result is a comprehensive report that combines campaign performance metrics with the latest account and campaign details.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_daily_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
campaigns.type AS campaign_type,
campaigns.time_zone AS campaign_timezone,
campaigns.status AS campaign_status,
report.device_os,
report.device_type,
report.network,
report.currency_code,
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 campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_type | text | The campaign type associated with this record; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/campaigntype?view=bingads-13) for more information on values and definitions. |
campaign_timezone | text | The time zone associated with this campaign. |
campaign_status | text | Status of the Campaign. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query combines data from multiple Microsoft Ads-related tables to create a comprehensive keyword report. It joins daily keyword report data with the most recent records from keyword, ad, ad group, campaign, and account history tables. The query then aggregates the data, summarizing clicks, impressions, and spend while grouping by various dimensions such as date, account, campaign, ad group, ad, keyword, and device information.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__keyword_daily_report
), keywords AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__keyword_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ads.ad_name,
report.ad_id,
keywords.keyword_name,
report.keyword_id,
keywords.match_type,
report.device_os,
report.device_type,
report.network,
report.currency_code,
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 ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN keywords
ON report.keyword_id = keywords.keyword_id
AND report.source_relation = keywords.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17
)
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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
keyword_name | text | The keyword(s) associated with this record. |
keyword_id | bigint | The ID representing the keyword, if present in the source data. |
match_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates data from multiple Microsoft Ads-related tables to create a comprehensive search report. It joins historical data from accounts, campaigns, ad groups, ads, and keywords with daily search report data. The query filters for the most recent records in the historical tables, combines this information with the daily report, and aggregates metrics such as clicks, impressions, and spend. The result is a detailed view of search performance across various dimensions like account, campaign, ad group, keyword, and device.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__search_daily_report
), keywords AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__keyword_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ads.ad_name,
report.ad_id,
report.keyword_id,
keywords.keyword_name,
COALESCE(report.delivered_match_type, report.bid_match_type) AS match_type,
report.search_query,
report.device_os,
report.device_type,
report.network,
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 ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN keywords
ON report.keyword_id = keywords.keyword_id
AND report.source_relation = keywords.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17
)
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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
keyword_name | text | The keyword(s) associated with this record. |
keyword_id | bigint | The ID representing the keyword, if present in the source data. |
match_type | text | Type of matching for the keyword used in bid. One of: BROAD, PHRASE, or EXACT. |
search_query | text | The search query text. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query integrates data from multiple Microsoft Ads related tables (ad daily report, ad history, ad group history, campaign history, and account history) to create a comprehensive URL report. It cleans and extracts various URL components (base URL, host, path) and UTM parameters from the final URL. The query also performs aggregations on metrics like clicks, impressions, and spend. Finally, it filters out records where the base URL is null.
IntegrationCleaningFeaturizationAggregationFilteringWITH report AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_daily_report
), ads AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
accounts.account_name,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ads.ad_name,
report.ad_id,
report.device_os,
report.device_type,
report.network,
report.currency_code,
SPLIT_PART(ads.final_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(ads.final_url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(ads.final_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(ads.final_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(ads.final_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(ads.final_url, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(ads.final_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(ads.final_url, 'utm_term=', 2), '&', 1), '') AS 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 ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
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,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22
), filtered AS (
SELECT
*
FROM joined
WHERE
NOT base_url IS NULL
)
SELECT
*
FROM filtered
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 report. |
account_name | character varying | The name of the account, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency used for all monetary values for entities under this profile. |
base_url | text | The base URL of the ad, extracted from the `final_url`. |
url_host | text | The URL host of the ad, extracted from the `final_url`. |
url_path | text | The URL path of the ad, extracted from the `final_url`. |
utm_source | text | The site that sent traffic to your page. Microsoft Advertising sets this to Bing; extracted from the `final_url`. |
utm_medium | text | Which channel was used. Microsoft Advertising sets this to cp; extracted from the `final_url`. |
utm_campaign | text | Which campaign the keyword came from; extracted from the `final_url`. |
utm_content | text | Which ad group the keyword came from; extracted from the `final_url`. |
utm_term | text | Which keyword brought people to your website; extracted from the `final_url`. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query stages data from a temporary table, casts columns to specific data types, renames some columns, and selects a subset of columns for the final output. It appears to be preparing data from a Microsoft Ads account daily report for further use.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS ad_distribution,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
device_os,
device_type,
network,
currency_code,
ad_distribution,
bid_match_type,
delivered_match_type,
top_vs_other,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
ad_distribution | text | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT. The query doesn't retrieve any actual data; it's likely used as a placeholder or template for structure definition.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
device_os | text | None |
device_type | text | None |
network | text | None |
currency_code | text | None |
ad_distribution | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query creates a staged version of Microsoft Ads account history data. It starts with a base table, creates a fields CTE with NULL or empty values for specific columns, and then constructs a final CTE that selects and renames columns from the fields CTE. The query also adds a flag to identify the most recent record for each account using a window function. The final SELECT statement retrieves all columns from the final CTE.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__account_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS last_modified_time,
CAST(NULL AS TEXT) AS time_zone,
CAST(NULL AS TEXT) AS currency_code,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS account_id,
name AS account_name,
last_modified_time AS modified_at,
time_zone,
currency_code,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY last_modified_time DESC) = 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 | integer | The ID representing the account. |
account_name | character varying | The name of the account, if present in the source data. |
modified_at | timestamp without time zone | The time each version of the object was last modified, i.e. when that version of the object was 'created'. |
time_zone | text | The time zone associated with this record. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Microsoft Ads account history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
name | character varying | None |
last_modified_time | timestamp without time zone | None |
time_zone | text | None |
currency_code | text | None |
This SQL query creates a staging table for Microsoft Ads daily ad reports. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types (mostly set to NULL or empty strings), and finally reorganizes and renames some columns in the final select statement. The query appears to be setting up a structure for data that will be populated later, rather than actually transforming or aggregating data.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS ad_distribution,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS language,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
campaign_id,
ad_group_id,
ad_id,
currency_code,
device_os,
device_type,
network,
language,
ad_distribution,
bid_match_type,
delivered_match_type,
top_vs_other,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
language | text | The language that the associated ad was viewed in. |
ad_distribution | text | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
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 testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
campaign_id | bigint | None |
ad_group_id | bigint | None |
ad_id | bigint | None |
currency_code | text | None |
device_os | text | None |
device_type | text | None |
network | text | None |
language | text | None |
ad_distribution | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query is designed to create a staging table for Microsoft Ads ad group daily reports. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types (mostly set to NULL or empty string), and finally reorganizes and renames some columns in the final select statement. The query doesn't perform any actual data transformation or filtering, suggesting it's primarily used to set up a consistent structure for the staging table.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS ad_distribution,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS language,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
campaign_id,
ad_group_id,
currency_code,
device_os,
device_type,
network,
language,
ad_distribution,
bid_match_type,
delivered_match_type,
top_vs_other,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
language | text | The language that the associated ad was viewed in. |
ad_distribution | text | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, initialized to NULL. The LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used to define the structure of a temporary table or view without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
campaign_id | bigint | None |
ad_group_id | bigint | None |
currency_code | text | None |
device_os | text | None |
device_type | text | None |
network | text | None |
language | text | None |
ad_distribution | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query stages data from a temporary table for Microsoft Ads ad group history. It casts fields to specific data types, renames some columns, and adds a flag to identify the most recent record for each ad group across different source relations. The query doesn't actually filter or aggregate data but prepares it for further processing.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS modified_time,
CAST(NULL AS DATE) AS start_date,
CAST(NULL AS DATE) AS end_date,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_group_id,
name AS ad_group_name,
campaign_id,
modified_time AS modified_at,
start_date,
end_date,
status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modified_time DESC) = 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_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
modified_at | timestamp without time zone | The time each version of the object was last modified, i.e. when that version of the object was 'created'. |
start_date | date | The date in which an ad group starts running. |
end_date | integer | The date in which this ad group will no longer run. |
status | text | The status of this ad group; see the following [documentation](https://help.ads.microsoft.com/apex/index/3/en/53094) for more information on values and definitions. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 template or schema for the column without any actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
name | text | None |
campaign_id | bigint | None |
modified_time | timestamp without time zone | None |
start_date | date | None |
end_date | integer | None |
status | text | None |
This SQL query creates a staging table for Microsoft Ads ad history data. It starts with a base CTE that selects all columns from a temporary table. Then, it defines a fields CTE with null or empty values for specific columns. Finally, it selects and renames columns from the fields CTE, adds a row number to identify the most recent record for each ad, and includes a source relation column.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS title_part_1,
CAST(NULL AS TEXT) AS final_url,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TIMESTAMP) AS modified_time,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS type,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_id,
title_part_1 AS ad_name,
final_url,
ad_group_id,
modified_time AS modified_at,
status,
type,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modified_time DESC) = 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 representing the ad, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
final_url | text | The full URL that the ad links to. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
modified_at | timestamp without time zone | The time each version of the object was last modified, i.e. when that version of the object was 'created'. |
status | text | The status of this ad; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/adstatus?view=bingads-13) for more information on values and definitions. |
type | text | The ad type associated with this record; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/adtype?view=bingads-13) for more information on values and definitions. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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) project, specifically for staging data from Microsoft Ads.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
title_part_1 | text | None |
final_url | text | None |
ad_group_id | bigint | None |
modified_time | timestamp without time zone | None |
status | text | None |
type | text | None |
This SQL query stages data from a temporary table, casts columns to specific data types, renames some columns, and reorganizes the column order. It doesn't perform any data transformation, filtering, or aggregation. The query appears to be preparing the data for further processing or analysis by standardizing the structure and data types.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS ad_distribution,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST(NULL AS TEXT) AS budget_association_status,
CAST(NULL AS TEXT) AS budget_name,
CAST(NULL AS TEXT) AS budget_status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
campaign_id,
currency_code,
device_os,
device_type,
network,
ad_distribution,
bid_match_type,
delivered_match_type,
top_vs_other,
budget_association_status,
budget_name,
budget_status,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
ad_distribution | text | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
budget_association_status | text | Indicates whether or not the campaign is currently spending from the budget mentioned in the BudgetName column. The possible values are `Current` and `Ended`. |
budget_name | text | The name of the budget. This column will be empty for unshared budgets. |
budget_status | text | The budget status. The possible values are `Active` and `Deleted`. This column will be empty for unshared budgets. |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
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 LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
campaign_id | bigint | None |
currency_code | text | None |
device_os | text | None |
device_type | text | None |
network | text | None |
ad_distribution | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
budget_association_status | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query creates a staging table for Microsoft Ads campaign history data. It starts with a base table, then defines a fields structure with specific data types. The final select statement renames some columns, adds a source_relation column, and includes a row number to identify the most recent record for each campaign. The query focuses on structuring and preparing the data for further use.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TIMESTAMP) AS modified_time,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TEXT) AS time_zone,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS campaign_id,
name AS campaign_name,
account_id,
modified_time AS modified_at,
type,
time_zone,
status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modified_time DESC) = 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 | integer | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
account_id | integer | The ID representing the account. |
modified_at | timestamp without time zone | The time each version of the object was last modified, i.e. when that version of the object was 'created'. |
type | text | The campaign type associated with this record; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/campaigntype?view=bingads-13) for more information on values and definitions. |
time_zone | text | The time zone associated with this record. |
status | text | The status of this campaign; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/campaignstatus?view=bingads-13) for more information on values and definitions. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 LIMIT 0 clause ensures no rows are returned. This is likely used as a template or placeholder for a staging table in a dbt (data build tool) project, specifically for Microsoft Ads campaign history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
name | text | None |
account_id | integer | None |
modified_time | timestamp without time zone | None |
type | text | None |
time_zone | text | None |
status | text | None |
This SQL query stages data for the Microsoft Ads keyword daily report. It selects data from a temporary table, casts columns to specific data types, renames some columns, and reorganizes the column order. The query doesn't perform any filtering, cleaning, deduplication, featurization, integration, or aggregation. It's primarily focused on structuring the data for further use.
OtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__keyword_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS TEXT) AS ad_distribution,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TEXT) AS currency_code,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS keyword_id,
CAST(NULL AS TEXT) AS language,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
campaign_id,
ad_group_id,
ad_id,
keyword_id,
currency_code,
device_os,
device_type,
network,
language,
ad_distribution,
bid_match_type,
delivered_match_type,
top_vs_other,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
keyword_id | bigint | The ID representing the keyword, if present in the source data. |
currency_code | text | The currency code associated with spend and, if applicable, other metrics associated with currency. |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
language | text | The language that the associated ad was viewed in. |
ad_distribution | text | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT. The query doesn't retrieve any actual data, as it uses LIMIT 0 to return no rows. This is likely used as a template or placeholder for a staging table in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
campaign_id | bigint | None |
ad_group_id | bigint | None |
ad_id | bigint | None |
keyword_id | bigint | None |
currency_code | text | None |
device_os | text | None |
device_type | text | None |
network | text | None |
language | text | None |
ad_distribution | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query creates a staging table for Microsoft Ads keyword history. It starts with a base table, then creates a fields CTE with NULL or empty values for specific columns. The final CTE selects and renames columns from the fields CTE, adds a source_relation column, and includes a row number to identify the most recent record for each keyword. The query ends by selecting all columns from the final CTE.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__keyword_history_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS modified_time,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS keyword_id,
name AS keyword_name,
modified_time AS modified_at,
ad_group_id,
match_type,
status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY modified_time DESC) = 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. |
keyword_id | bigint | The ID representing the keyword, if present in the source data. |
keyword_name | text | The keyword(s) associated with this record. |
modified_at | timestamp without time zone | The time each version of the object was last modified, i.e. when that version of the object was 'created'. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
match_type | text | The match type associated with this record; values contain but may not be limited to 'Broad', 'Exact', 'Phrase'. Please refer to Microsoft Ad's [documentation](https://help.ads.microsoft.com/#apex/ads/en/50822/1). |
status | text | The status of this keyword; see the following [documentation](https://docs.microsoft.com/en-us/advertising/campaign-management-service/keywordstatus?view=bingads-13) for more information on values and definitions. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 a staging table in a dbt (data build tool) project, specifically for Microsoft Ads keyword history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
name | text | None |
modified_time | timestamp without time zone | None |
ad_group_id | bigint | None |
match_type | text | None |
status | text | None |
This SQL query performs a data transformation and type casting operation on the 'microsoft_ads__search_daily_report' data. It selects all columns from a temporary table, casts them to specific data types, renames some columns, and reorganizes the column order in the final output. The query doesn't perform any filtering, aggregation, or complex transformations.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_microsoft_ads_source.stg_microsoft_ads__search_daily_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS account_id,
CAST(NULL AS INT) AS ad_group_id,
CAST(NULL AS INT) AS ad_id,
CAST(NULL AS TEXT) AS bid_match_type,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DATE) AS date,
CAST(NULL AS TEXT) AS delivered_match_type,
CAST(NULL AS TEXT) AS device_os,
CAST(NULL AS TEXT) AS device_type,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS keyword_id,
CAST(NULL AS TEXT) AS language,
CAST(NULL AS TEXT) AS network,
CAST(NULL AS TEXT) AS search_query,
CAST(NULL AS FLOAT) AS spend,
CAST(NULL AS TEXT) AS top_vs_other,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
date AS date_day,
account_id,
campaign_id,
ad_group_id,
ad_id,
keyword_id,
search_query,
device_os,
device_type,
network,
language,
bid_match_type,
delivered_match_type,
top_vs_other,
clicks,
impressions,
spend
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. |
date_day | date | The date of the report. |
account_id | bigint | The ID representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
keyword_id | bigint | The ID representing the keyword, if present in the source data. |
search_query | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
device_os | text | The device operating system associated with this record; values include but may not be limited to 'Windows', 'iOS', 'Android', 'Other', 'BlackBerry' and 'Unknown'. |
device_type | text | The device type associated with this record; values include but may not be limited to 'Computer', 'Smartphone', 'Tablet' and 'Unknown'. |
network | text | The network associated with this record. |
language | text | The language that the associated ad was viewed in. |
ad_distribution | None | The distribution medium associated with this record. |
bid_match_type | text | The bid match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
delivered_match_type | text | The delivered match type associated with this record; values include 'Broad', 'Exact', 'Phrase'. |
top_vs_other | text | The position of the ad associated with this record. For more information, refer to Microsoft [documentation](https://help.ads.microsoft.com/apex/index/22/en/14009). |
clicks | integer | The count of clicks. |
impressions | integer | The count of impressions. |
spend | double precision | The cost of the ads. |
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 LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | date | None |
account_id | bigint | None |
campaign_id | bigint | None |
ad_group_id | bigint | None |
ad_id | bigint | None |
keyword_id | bigint | None |
search_query | text | None |
device_os | text | None |
device_type | text | None |
network | text | None |
language | text | None |
bid_match_type | text | None |
delivered_match_type | text | None |
top_vs_other | text | None |
clicks | integer | None |
impressions | integer | None |
spend | double precision | None |
This SQL query integrates data from multiple Pinterest Ads-related tables (ad group report, advertisers, campaigns, and ad groups) to create a comprehensive view of ad performance. It joins these tables based on relevant IDs and source relations, filters for the most recent records in some tables, and aggregates spend, clicks, and impressions data at the ad group level. The result is a detailed report that includes information about advertisers, campaigns, ad groups, and their performance metrics.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_report
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
report.source_relation,
report.date_day,
advertisers.advertiser_name,
advertisers.advertiser_id,
campaigns.campaign_name,
campaigns.campaign_status,
campaigns.campaign_id,
ad_groups.ad_group_name,
report.ad_group_id,
ad_groups.created_at,
ad_groups.start_time,
ad_groups.end_time,
ad_groups.ad_group_status,
SUM(report.spend) AS spend,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
LEFT JOIN advertisers
ON campaigns.advertiser_id = advertisers.advertiser_id
AND campaigns.source_relation = advertisers.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
)
SELECT
*
FROM fields
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 | timestamp without time zone | The date of the report. |
advertiser_id | bigint | The ID of the related Advertiser. |
advertiser_name | text | Name of the advertiser. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the related Campaign. |
campaign_status | text | Status of the campaign. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the related Ad group. |
ad_group_status | text | Status of the ad group. |
created_at | text | Ad group creation time. |
start_time | text | Ad group start time. |
end_time | integer | Ad group end time. |
impressions | bigint | The count of impressions. |
clicks | bigint | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query combines advertiser report data with advertiser history data, joining them on advertiser_id and source_relation. It filters the advertiser history to only include the most recent records. The query then aggregates spend, clicks, and impressions by date, advertiser, currency, and country, providing a comprehensive summary of advertising performance for each advertiser.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_report
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
report.source_relation,
report.date_day,
advertisers.advertiser_name,
report.advertiser_id,
advertisers.currency_code,
advertisers.country,
SUM(report.spend) AS spend,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN advertisers
ON report.advertiser_id = advertisers.advertiser_id
AND report.source_relation = advertisers.source_relation
GROUP BY
1,
2,
3,
4,
5,
6
)
SELECT
*
FROM fields
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 | timestamp without time zone | The date of the report. |
advertiser_name | text | Name of the advertiser. |
advertiser_id | bigint | The ID of the related Advertiser. |
currency_code | text | The currency code which the advertiser is set up using. |
country | text | The country code where the advertiser is located. |
spend | numeric | The cost of the ads. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
This SQL query combines data from three tables (campaign report, campaign history, and advertiser history) to create a comprehensive report on Pinterest ad campaigns. It joins the most recent campaign and advertiser data with daily report data, aggregates spend, clicks, and impressions, and includes relevant information such as advertiser name, campaign name, and status.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
report.source_relation,
report.date_day,
advertisers.advertiser_name,
advertisers.advertiser_id,
campaigns.campaign_name,
report.campaign_id,
campaigns.campaign_status,
SUM(report.spend) AS spend,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN advertisers
ON campaigns.advertiser_id = advertisers.advertiser_id
AND campaigns.source_relation = advertisers.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM fields
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 | timestamp without time zone | The date of the report. |
advertiser_id | bigint | The ID of the related Advertiser. |
advertiser_name | text | Name of the advertiser. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the related Campaign. |
campaign_status | text | Status of the campaign. |
impressions | bigint | The count of impressions. |
clicks | bigint | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query combines data from multiple Pinterest Ads-related tables to create a comprehensive keyword report. It joins keyword performance data with advertiser, campaign, ad group, and keyword information. The query filters for the most recent records in the history tables, aggregates spend, clicks, and impressions data, and presents a detailed view of keyword performance across the advertising hierarchy.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__keyword_report
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), keywords AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__keyword_history
WHERE
is_most_recent_record = TRUE
), fields AS (
SELECT
report.source_relation,
report.date_day,
advertisers.advertiser_name,
advertisers.advertiser_id,
campaigns.campaign_name,
campaigns.campaign_id,
ad_groups.ad_group_name,
ad_groups.ad_group_id,
report.keyword_id,
keywords.match_type,
keywords.parent_type,
keywords.keyword_value,
SUM(report.spend) AS spend,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions
FROM report
LEFT JOIN keywords
ON report.keyword_id = keywords.keyword_id
AND report.source_relation = keywords.source_relation
LEFT JOIN ad_groups
ON keywords.ad_group_id = ad_groups.ad_group_id
AND keywords.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
LEFT JOIN advertisers
ON campaigns.advertiser_id = advertisers.advertiser_id
AND campaigns.source_relation = advertisers.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12
)
SELECT
*
FROM fields
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 | timestamp without time zone | The date of the report. |
advertiser_name | text | Name of the advertiser. |
advertiser_id | bigint | The ID of the related Advertiser. |
campaign_name | text | Name of the campaign. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | Name of the ad group. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
keyword_id | bigint | Unique identifier of the keyword. |
match_type | text | Type of match the keyword is tied to. Either Exact or Broad. |
parent_type | text | Identifier of what grain the parent type is. Ad group or campaign. |
keyword_value | text | The text value that makes upd the keyword. |
spend | numeric | The cost of the ads. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
This SQL query integrates data from multiple Pinterest Ads-related tables (pin promotion report, pin promotion history, ad group history, campaign history, and advertiser history) to create a comprehensive report. It joins these tables based on various IDs and source relations, filters for the most recent records in the history tables, and aggregates metrics such as clicks, impressions, and spend. The result is a detailed view of Pinterest ad performance across different levels of the advertising hierarchy (advertiser, campaign, ad group, and pin).
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_report
), pins AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
campaigns.advertiser_id,
advertisers.advertiser_name,
report.campaign_id,
campaigns.campaign_name,
campaigns.campaign_status,
report.ad_group_id,
ad_groups.ad_group_name,
ad_groups.ad_group_status,
pins.creative_type,
report.pin_promotion_id,
pins.pin_name,
pins.pin_status,
pins.destination_url,
pins.base_url,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN pins
ON report.pin_promotion_id = pins.pin_promotion_id
AND report.source_relation = pins.source_relation
LEFT JOIN ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN advertisers
ON campaigns.advertiser_id = advertisers.advertiser_id
AND campaigns.source_relation = advertisers.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
)
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 | timestamp without time zone | The date of the report. |
advertiser_id | bigint | The ID of the related Advertiser. |
advertiser_name | text | Name of the advertiser. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | Name of the campaign. |
campaign_status | text | Status of the campaign. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | Name of the ad group. |
ad_group_status | text | Status of the ad group. |
creative_type | text | The creative type. One of "APP", "APP_VIDEO", "BOARD", "CAROUSEL", "CINEMATIC", "COMMERCE", "MAX_VIDEO", "NATIVE_VIDEO", "REGULAR", "SEARCH_PROMINENCE", "SEARCH_PROMINENCE_CAROUSEL", "SHOPPING", "SHOP_THE_PIN", "THIRD_PARTY", or "VIDEO". |
pin_promotion_id | bigint | The ID of the related Pin promotion. |
pin_name | text | Name of the pin. |
pin_status | text | Status of the pin. |
destination_url | text | Pin destination URL. |
base_url | text | The base URL of the ad, extracted from the `destination_url`. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | numeric | The cost of the ads. |
This SQL query integrates data from multiple Pinterest advertising tables (pin promotion report, pin promotion history, ad group history, campaign history, and advertiser history) to create a comprehensive URL report. It joins these tables, filters for the most recent records in the history tables, and aggregates metrics like clicks, impressions, and spend. The query provides a detailed view of advertising performance across various dimensions such as advertiser, campaign, ad group, and pin levels, including URL-related information and UTM parameters.
IntegrationFilteringAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_report
), pins AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_history
WHERE
is_most_recent_record = TRUE
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), advertisers AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history
WHERE
is_most_recent_record = TRUE
), joined AS (
SELECT
report.source_relation,
report.date_day,
campaigns.advertiser_id,
advertisers.advertiser_name,
report.campaign_id,
campaigns.campaign_name,
campaigns.campaign_status,
report.ad_group_id,
ad_groups.ad_group_name,
ad_groups.ad_group_status,
pins.destination_url,
pins.creative_type,
report.pin_promotion_id,
pins.pin_name,
pins.pin_status,
pins.base_url,
pins.url_host,
pins.url_path,
pins.utm_source,
pins.utm_medium,
pins.utm_campaign,
pins.utm_content,
pins.utm_term,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN pins
ON report.pin_promotion_id = pins.pin_promotion_id
AND report.source_relation = pins.source_relation
LEFT JOIN ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
LEFT JOIN advertisers
ON campaigns.advertiser_id = advertisers.advertiser_id
AND campaigns.source_relation = advertisers.source_relation
WHERE
NOT pins.destination_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,
21,
22,
23
)
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 | timestamp without time zone | The date of the report. |
advertiser_id | bigint | The ID of the related Advertiser. |
advertiser_name | text | Name of the advertiser. |
campaign_status | text | Status of the campaign. |
ad_group_status | text | Status of the ad group. |
destination_url | text | Pin destination URL. |
pin_promotion_id | bigint | The ID of the related Pin promotion. |
pin_name | text | Pin promotion name. |
pin_status | text | The status of the Pin promotion. One of "ACTIVE", "ARCHIVED", "PAUSED" |
creative_type | text | The creative type. One of "APP", "APP_VIDEO", "BOARD", "CAROUSEL", "CINEMATIC", "COMMERCE", "MAX_VIDEO", "NATIVE_VIDEO", "REGULAR", "SEARCH_PROMINENCE", "SEARCH_PROMINENCE_CAROUSEL", "SHOPPING", "SHOP_THE_PIN", "THIRD_PARTY", or "VIDEO". |
base_url | text | The base URL of the ad, extracted from the `destination_url`. |
url_host | text | The URL host of the ad, extracted from the `destination_url`. |
url_path | text | The URL path of the ad, extracted from the `destination_url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `destination_url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `destination_url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `destination_url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `destination_url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `destination_url`. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | The name of the related Campaign. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | The name of the related Ad group. |
impressions | bigint | The count of impressions. |
clicks | bigint | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query performs a series of transformations on data from the 'pinterest_source.stg_pinterest_ads__ad_group_history' table. It starts by casting columns to specific data types, renames some columns, and adds a 'source_relation' column. The query then selects and reorganizes these fields, adding a 'is_most_recent_record' flag based on the most recent '_fivetran_synced' timestamp for each unique combination of 'source_relation' and 'id'.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS ad_account_id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS pacing_delivery_type,
CAST(NULL AS TEXT) AS placement_group,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS summary_status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_group_id,
name AS ad_group_name,
status AS ad_group_status,
ad_account_id AS advertiser_id,
_fivetran_synced,
campaign_id,
created_time AS created_at,
end_time,
pacing_delivery_type,
placement_group,
start_time,
summary_status,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_group_id | bigint | Ad group ID. |
campaign_id | bigint | Parent Campaign ID. |
created_at | text | Ad group creation time. |
ad_group_name | text | Ad group name. |
ad_group_status | text | The status of the Ad group. One of "ACTIVE", "ARCHIVED", "PAUSED" |
advertiser_id | bigint | The ID of the related Advertiser. |
start_time | text | Ad group start time. |
end_time | integer | Ad group end time. |
pacing_delivery_type | text | Ad group pacing delivery type. With ACCELERATED, an ad group budget is spent as fast as possible. With STANDARD, an ad group budget is spent smoothly over a day. When using CBO, only the STANDARD pacing delivery type is allowed. |
placement_group | text | The placement group. "ALL", "SEARCH", "BROWSE", or "OTHER" |
summary_status | text | Summary status. "RUNNING", "PAUSED", "NOT_STARTED", "COMPLETED", "ADVERTISER_DISABLED", "ARCHIVED" |
_fivetran_synced | text | Timestamp of when a record was last synced. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
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 development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
campaign_id | bigint | None |
created_time | text | None |
name | text | None |
status | text | None |
start_time | text | None |
end_time | integer | None |
_fivetran_synced | text | None |
pacing_delivery_type | text | None |
placement_group | text | None |
summary_status | text | None |
ad_account_id | bigint | None |
This SQL query transforms and cleans data from a Pinterest ads ad group report. It casts various fields to specific data types, combines impression and clickthrough data, calculates spend in dollars from micro-dollars, and truncates the date to day-level granularity. The query also adds a source_relation field and renames some columns for clarity.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__ad_group_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_group_name,
CAST(NULL AS TEXT) AS ad_group_status,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS INT) AS campaign_id,
CAST(NULL AS INT) AS clickthrough_1,
CAST(NULL AS INT) AS clickthrough_2,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impression_1,
CAST(NULL AS INT) AS impression_2,
CAST(NULL AS INT) AS spend_in_micro_dollar,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
ad_group_id,
ad_group_name,
ad_group_status,
campaign_id,
advertiser_id,
COALESCE(impression_1, 0) + COALESCE(impression_2, 0) AS impressions,
COALESCE(clickthrough_1, 0) + COALESCE(clickthrough_2, 0) AS clicks,
spend_in_micro_dollar / 1000000.0 AS spend
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. |
date_day | timestamp without time zone | The date of the report. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | Name of the ad group. |
ad_group_status | text | Status of the ad group. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
advertiser_id | bigint | The ID of the related Advertiser. |
impressions | integer | The count of impressions. |
clicks | integer | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, 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 |
---|---|---|
ad_group_id | bigint | None |
advertiser_id | bigint | None |
date | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
ad_group_name | text | None |
ad_group_status | text | None |
campaign_daily_spend_cap | integer | None |
campaign_id | bigint | None |
campaign_lifetime_spend_cap | integer | None |
campaign_name | text | None |
campaign_status | text | None |
clickthrough_1 | integer | None |
clickthrough_1_gross | integer | None |
cpc_in_micro_dollar | double precision | None |
cpm_in_micro_dollar | double precision | None |
ctr | double precision | None |
ecpc_in_micro_dollar | double precision | None |
ecpm_in_micro_dollar | double precision | None |
ectr | double precision | None |
engagement_1 | integer | None |
impression_1 | integer | None |
impression_1_gross | integer | None |
outbound_click_1 | integer | None |
paid_impression | integer | None |
spend_in_micro_dollar | integer | None |
total_engagement | integer | None |
total_impression_frequency | double precision | None |
total_impression_user | integer | None |
This SQL query stages data from a Pinterest ads advertiser history table. It casts columns to specific data types, renames some columns, and adds a flag to identify the most recent record for each advertiser. The query also adds a source_relation column and selects specific fields from the base table.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS country,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS owner_user_id,
CAST(NULL AS TEXT) AS owner_username,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS advertiser_permissions,
CAST(NULL AS TIMESTAMP) AS updated_time,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS advertiser_id,
name AS advertiser_name,
country,
created_time AS created_at,
currency AS currency_code,
owner_user_id,
owner_username,
advertiser_permissions, /* permissions was renamed in macro */
updated_time AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_time DESC) = 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. |
advertiser_id | bigint | The ID of the related Advertiser. |
advertiser_name | text | Name of the advertiser. |
country | text | The country code where the advertiser is located. |
created_at | timestamp without time zone | Timestamp of when a record was created. |
currency_code | text | The currency code which the advertiser is set up using. |
updated_at | timestamp without time zone | Timestamp of when a record was last updated. |
owner_username | text | Advertiser's username. |
owner_user_id | integer | Unique identifier of the owner user. |
advertiser_permissions | text | The permissions associated with this account. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
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 query, possibly for testing or initializing a structure without actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
updated_time | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
billing_profile_status | text | None |
billing_type | text | None |
country | text | None |
created_time | timestamp without time zone | None |
currency | text | None |
merchant_id | integer | None |
name | text | None |
owner_user_id | integer | None |
status | text | None |
owner_username | text | None |
permissions | text | None |
This SQL query performs several transformations on data from a Pinterest ads advertiser report. It starts by casting columns to specific data types, then combines impression and clickthrough data, converts spend from micro-dollars to dollars, and truncates the date to day level. The query also adds a source_relation column and renames some fields for clarity.
CleaningFeaturizationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__advertiser_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS INT) AS clickthrough_1,
CAST(NULL AS INT) AS clickthrough_2,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impression_1,
CAST(NULL AS INT) AS impression_2,
CAST(NULL AS INT) AS spend_in_micro_dollar,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
advertiser_id,
COALESCE(impression_1, 0) + COALESCE(impression_2, 0) AS impressions,
COALESCE(clickthrough_1, 0) + COALESCE(clickthrough_2, 0) AS clicks,
spend_in_micro_dollar / 1000000.0 AS spend
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. |
date_day | timestamp without time zone | The date of the report. |
advertiser_id | bigint | The ID of the related Advertiser. |
impressions | integer | The count of impressions. |
clicks | integer | The count of clicks. |
spend | numeric | The cost of the ads. |
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 LIMIT 0 ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
advertiser_id | bigint | None |
date | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
clickthrough_1 | integer | None |
clickthrough_1_gross | integer | None |
cpc_in_micro_dollar | double precision | None |
cpm_in_micro_dollar | double precision | None |
ctr | double precision | None |
ecpc_in_micro_dollar | double precision | None |
ecpm_in_micro_dollar | double precision | None |
ectr | double precision | None |
engagement_1 | integer | None |
impression_1 | integer | None |
impression_1_gross | integer | None |
outbound_click_1 | integer | None |
paid_impression | integer | None |
spend_in_micro_dollar | integer | None |
total_engagement | integer | None |
total_impression_frequency | double precision | None |
total_impression_user | integer | None |
This SQL query stages data from a temporary Pinterest 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 prepares the data for further processing or analysis by structuring it in a consistent format.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS INT) AS default_ad_group_budget_in_micro_currency,
CAST(NULL AS BOOLEAN) AS is_automated_campaign,
CAST(NULL AS BOOLEAN) AS is_campaign_budget_optimization,
CAST(NULL AS BOOLEAN) AS is_flexible_daily_budgets,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS status,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS campaign_id,
name AS campaign_name,
advertiser_id,
default_ad_group_budget_in_micro_currency,
is_automated_campaign,
is_campaign_budget_optimization,
is_flexible_daily_budgets,
status AS campaign_status,
_fivetran_synced,
created_time AS created_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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 representing the campaign, if present in the source data. |
created_at | text | Campaign creation time. |
advertiser_id | bigint | The ID of the related Advertiser. |
campaign_name | text | Campaign name. |
campaign_status | text | The status of the Campaign. One of "ACTIVE", "ARCHIVED", "PAUSED" |
default_ad_group_budget_in_micro_currency | integer | When transitioning from campaign budget optimization to non-campaign budget optimization, the default_ad_group_budget_in_micro_currency will propagate to each child ad groups daily budget. Unit is micro currency of the associated advertiser account. |
is_automated_campaign | boolean | Specifies whether the campaign was created in the automated campaign flow |
is_campaign_budget_optimization | boolean | Determines if a campaign automatically generate ad-group level budgets given a campaign budget to maximize campaign outcome. When transitioning from non-cbo to cbo, all previous child ad group budget will be cleared. |
is_flexible_daily_budgets | boolean | Determine if a campaign has flexible daily budgets setup. |
_fivetran_synced | text | Timestamp of when a record was last synced. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. The query doesn't select any actual data and limits the output to 0 rows, effectively creating a template or placeholder for the table structure.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
created_time | text | None |
name | text | None |
status | text | None |
_fivetran_synced | text | None |
advertiser_id | bigint | None |
default_ad_group_budget_in_micro_currency | integer | None |
is_automated_campaign | boolean | None |
is_campaign_budget_optimization | boolean | None |
is_flexible_daily_budgets | boolean | None |
This SQL query performs several operations on the Pinterest ads campaign report data. It starts by casting fields to specific data types, then combines impression and clickthrough data, calculates spend in dollars from micro-dollars, and truncates the date to day level. The query also renames some columns and selects specific fields for the final output.
CleaningFeaturizationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__campaign_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS campaign_name,
CAST(NULL AS TEXT) AS campaign_status,
CAST(NULL AS INT) AS clickthrough_1,
CAST(NULL AS INT) AS clickthrough_2,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impression_1,
CAST(NULL AS INT) AS impression_2,
CAST(NULL AS INT) AS spend_in_micro_dollar,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
campaign_id,
campaign_name,
campaign_status,
advertiser_id,
COALESCE(impression_1, 0) + COALESCE(impression_2, 0) AS impressions,
COALESCE(clickthrough_1, 0) + COALESCE(clickthrough_2, 0) AS clicks,
spend_in_micro_dollar / 1000000.0 AS spend
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. |
date_day | timestamp without time zone | The date of the report. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
campaign_name | text | Name of the campaign. |
campaign_status | text | Status of the campaign. |
advertiser_id | bigint | The ID of the related Advertiser. |
impressions | integer | The count of impressions. |
clicks | integer | The count of clicks. |
spend | numeric | The cost of the ads. |
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 LIMIT 0 clause ensures no rows are returned. This is likely used as a template or placeholder for a staging table in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
advertiser_id | bigint | None |
campaign_id | bigint | None |
date | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
campaign_daily_spend_cap | integer | None |
campaign_lifetime_spend_cap | integer | None |
campaign_name | text | None |
campaign_status | text | None |
clickthrough_1 | integer | None |
clickthrough_1_gross | integer | None |
cpc_in_micro_dollar | double precision | None |
cpm_in_micro_dollar | double precision | None |
ctr | double precision | None |
ecpc_in_micro_dollar | double precision | None |
ecpm_in_micro_dollar | double precision | None |
ectr | double precision | None |
engagement_1 | integer | None |
impression_1 | integer | None |
impression_1_gross | integer | None |
outbound_click_1 | integer | None |
paid_impression | integer | None |
spend_in_micro_dollar | integer | None |
total_engagement | integer | None |
total_impression_frequency | double precision | None |
total_impression_user | integer | None |
This SQL query performs a series of transformations on data from a Pinterest ads keyword history table. It starts by casting all fields to specific data types, then renames and reorganizes some columns. Finally, it adds a flag to identify the most recent record for each keyword using a window function.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__keyword_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS _fivetran_id,
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS BOOLEAN) AS archived,
CAST(NULL AS INT) AS bid,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS match_type,
CAST(NULL AS TEXT) AS parent_type,
CAST(NULL AS TEXT) AS value,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS keyword_id,
value AS keyword_value,
_fivetran_id,
_fivetran_synced,
ad_group_id,
advertiser_id,
archived,
bid,
campaign_id,
match_type,
parent_type,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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. |
keyword_id | bigint | Unique identifier of the keyword. |
keyword_value | text | The text value that makes upd the keyword. |
_fivetran_id | text | The unique identifier of the record within the Fivetran synced table. |
_fivetran_synced | timestamp without time zone | Timestamp of when a record was last synced. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
advertiser_id | bigint | The ID of the related Advertiser. |
archived | boolean | Boolean indicating if the keyword is archived. |
bid | integer | Bid amount set for the keyword. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
match_type | text | Type of match the keyword is tied to. Either Exact or Broad. |
parent_type | text | Identifier of what grain the parent type is. Ad group or campaign. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
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 limited to 0 rows, effectively creating a schema-only representation of the table without any data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_id | text | None |
_fivetran_synced | timestamp without time zone | None |
ad_group_id | bigint | None |
advertiser_id | bigint | None |
archived | boolean | None |
bid | integer | None |
campaign_id | bigint | None |
id | bigint | None |
match_type | text | None |
parent_type | text | None |
value | text | None |
This SQL query stages data from a Pinterest ads keyword report. It performs type casting on various fields, combines impression and clickthrough data, calculates spend in dollars from micro-dollars, and truncates the date to day level. The query also adds a source_relation field and renames some columns for clarity.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__keyword_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_group_name,
CAST(NULL AS TEXT) AS ad_group_status,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS INT) AS clickthrough_1,
CAST(NULL AS INT) AS clickthrough_2,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impression_1,
CAST(NULL AS INT) AS impression_2,
CAST(NULL AS TEXT) AS keyword_id,
CAST(NULL AS TEXT) AS pin_promotion_id,
CAST(NULL AS INT) AS spend_in_micro_dollar,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
keyword_id,
pin_promotion_id,
ad_group_id,
ad_group_name,
ad_group_status,
campaign_id,
advertiser_id,
COALESCE(impression_1, 0) + COALESCE(impression_2, 0) AS impressions,
COALESCE(clickthrough_1, 0) + COALESCE(clickthrough_2, 0) AS clicks,
spend_in_micro_dollar / 1000000.0 AS spend
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. |
date_day | timestamp without time zone | The date of the report. |
keyword_id | bigint | Unique identifier of the keyword. |
pin_promotion_id | bigint | The ID of the related Pin promotion. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
ad_group_name | text | Name of the ad group. |
ad_group_status | text | Status of the ad group. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
advertiser_id | bigint | The ID of the related Advertiser. |
impressions | integer | The count of impressions. |
clicks | integer | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Pinterest ads keyword report data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_group_id | bigint | None |
advertiser_id | bigint | None |
campaign_id | bigint | None |
date | timestamp without time zone | None |
keyword_id | bigint | None |
pin_id | bigint | None |
pin_promotion_id | bigint | None |
_fivetran_synced | timestamp without time zone | None |
ad_group_name | text | None |
ad_group_status | text | None |
campaign_daily_spend_cap | integer | None |
campaign_lifetime_spend_cap | integer | None |
campaign_name | text | None |
campaign_status | text | None |
clickthrough_1 | integer | None |
clickthrough_1_gross | integer | None |
cpc_in_micro_dollar | integer | None |
cpm_in_micro_dollar | double precision | None |
ctr | integer | None |
ecpc_in_micro_dollar | integer | None |
ecpm_in_micro_dollar | double precision | None |
ectr | integer | None |
engagement_1 | integer | None |
impression_1 | integer | None |
impression_1_gross | integer | None |
outbound_click_1 | integer | None |
paid_impression | integer | None |
pin_promotion_name | text | None |
pin_promotion_status | text | None |
spend_in_micro_dollar | integer | None |
targeting_type | text | None |
targeting_value | text | None |
total_engagement | integer | None |
This SQL query processes data from a Pinterest ads pin promotion history table. It performs several transformations on the data, including casting data types, extracting URL components, parsing UTM parameters, and creating a flag for the most recent record. The query also renames some columns and creates new features from existing data.
CleaningFeaturizationDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS ad_account_id,
CAST(NULL AS TEXT) AS android_deep_link,
CAST(NULL AS TEXT) AS click_tracking_url,
CAST(NULL AS TIMESTAMP) AS created_time,
CAST(NULL AS TEXT) AS creative_type,
CAST(NULL AS TEXT) AS destination_url,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS ios_deep_link,
CAST(NULL AS BOOLEAN) AS is_pin_deleted,
CAST(NULL AS BOOLEAN) AS is_removable,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS pin_id,
CAST(NULL AS TEXT) AS review_status,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TIMESTAMP) AS updated_time,
CAST(NULL AS TEXT) AS view_tracking_url,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS pin_promotion_id,
ad_account_id AS advertiser_id,
ad_group_id,
created_time AS created_at,
destination_url,
SPLIT_PART(destination_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(
REPLACE(REPLACE(destination_url, 'android-app://', ''), 'http://', ''),
'https://',
''
),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(destination_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(destination_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(destination_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(destination_url, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(destination_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(destination_url, 'utm_term=', 2), '&', 1), '') AS utm_term,
name AS pin_name,
pin_id,
status AS pin_status,
creative_type,
_fivetran_synced,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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. |
pin_promotion_id | bigint | Pin promotion ID. |
advertiser_id | bigint | The ID of the related Advertiser. |
ad_group_id | bigint | Pin promotion ad group ID. |
created_at | text | Pin creation time. |
destination_url | text | Pin destination URL. |
pin_name | text | Pin promotion name. |
pin_id | bigint | Original pin ID. |
pin_status | text | The status of the Pin promotion. One of "ACTIVE", "ARCHIVED", "PAUSED" |
creative_type | text | The creative type. One of "APP", "APP_VIDEO", "BOARD", "CAROUSEL", "CINEMATIC", "COMMERCE", "MAX_VIDEO", "NATIVE_VIDEO", "REGULAR", "SEARCH_PROMINENCE", "SEARCH_PROMINENCE_CAROUSEL", "SHOPPING", "SHOP_THE_PIN", "THIRD_PARTY", or "VIDEO". |
_fivetran_synced | text | Timestamp of when a record was last synced. |
is_most_recent_record | boolean | Boolean representing whether the record is the most recent version of the object. |
base_url | text | The base URL of the ad, extracted from the `destination_url`. |
url_host | text | The URL host of the ad, extracted from the `destination_url`. |
url_path | text | The URL path of the ad, extracted from the `destination_url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `destination_url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `destination_url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `destination_url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `destination_url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `destination_url`. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. The query doesn't fetch any actual data; it's likely used as a placeholder or template for further development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
ad_group_id | bigint | None |
created_time | text | None |
destination_url | text | None |
name | text | None |
pin_id | bigint | None |
status | text | None |
creative_type | text | None |
_fivetran_synced | text | None |
ad_account_id | bigint | None |
This SQL query performs data transformation on a Pinterest ads pin promotion report. It starts by casting columns to specific data types, then calculates total impressions and clicks by combining two separate impression and clickthrough fields. It also converts the spend from micro-dollars to dollars. The query truncates the date to day level and selects specific fields for the final output.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_pinterest_source.stg_pinterest_ads__pin_promotion_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS advertiser_id,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS DECIMAL(28, 6)) AS clickthrough_1,
CAST(NULL AS DECIMAL(28, 6)) AS clickthrough_2,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS DECIMAL(28, 6)) AS impression_1,
CAST(NULL AS DECIMAL(28, 6)) AS impression_2,
CAST(NULL AS TEXT) AS pin_promotion_id,
CAST(NULL AS DECIMAL(28, 6)) AS spend_in_micro_dollar,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
pin_promotion_id,
ad_group_id,
campaign_id,
advertiser_id,
COALESCE(impression_1, 0) + COALESCE(impression_2, 0) AS impressions,
COALESCE(clickthrough_1, 0) + COALESCE(clickthrough_2, 0) AS clicks,
spend_in_micro_dollar / 1000000.0 AS spend
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. |
date_day | timestamp without time zone | The date of the report. |
pin_promotion_id | bigint | The ID of the related Pin promotion. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
advertiser_id | bigint | The ID of the related Advertiser. |
impressions | integer | The count of impressions. |
clicks | integer | The count of clicks. |
spend | numeric | The cost of the ads. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type, initialized as NULL. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Pinterest ads data related to pin promotion reports.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
date | timestamp without time zone | None |
pin_promotion_id | bigint | None |
ad_group_id | bigint | None |
campaign_id | bigint | None |
advertiser_id | bigint | None |
_fivetran_synced | text | None |
impression_1 | integer | None |
impression_2 | integer | None |
clickthrough_1 | integer | None |
clickthrough_2 | integer | None |
spend_in_micro_dollar | integer | None |
This SQL query combines data from two staging tables: account reports and accounts. It joins these tables on account_id and source_relation, then aggregates the data by summing clicks, impressions, and spend. The result includes various account details and daily aggregated metrics.
IntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account_report
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account
), joined AS (
SELECT
report.source_relation,
report.date_day,
report.account_id,
accounts.currency,
accounts.attribution_type,
accounts.status,
accounts.time_zone_id,
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
)
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 report. |
account_id | bigint | The ID representing the account. |
currency | text | The currency this account uses (ISO-4217) |
attribution_type | text | Attribution type: "CLICK_THROUGH_CONVERSION", "VIEW_THROUGH_CONVERSION", or "ALL_CONVERSION". CLICK_THROUGH_CONVERSION: A user clicked on your ad and then completed the conversion action on your site. VIEW_THROUGH_CONVERSION: A user saw your ad and did not click it, but did complete the conversion action on your site. ALL_CONVERSION: Combination of both. |
status | integer | The current state of the advertiser. "PENDING_BILLING", "VALID", "TRUSTED", "ADMIN", "FAILED_BILLING", "SUSPICIOUS", "SUSPENDED", or "BANNED" |
time_zone_id | text | The time zone id preference for this account |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | bigint | Spend converted out of microcurrency (so Spend/1,000,000) |
This SQL query integrates data from multiple Reddit Ads tables (ad group report, ad groups, campaigns, and accounts) to create a comprehensive ad performance report. It joins these tables based on common identifiers and source relations, then aggregates the data to calculate total clicks, impressions, and spend for each unique combination of date, account, ad group, and campaign. The result provides a detailed view of ad performance across different levels of the advertising hierarchy.
IntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group_report
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account
), joined AS (
SELECT
report.source_relation,
report.date_day,
report.account_id,
ad_groups.ad_group_name,
report.ad_group_id,
campaigns.campaign_name,
ad_groups.campaign_id,
accounts.currency,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend
FROM report
LEFT JOIN ad_groups
ON report.ad_group_id = ad_groups.ad_group_id
AND report.source_relation = ad_groups.source_relation
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8
)
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 report. |
account_id | bigint | The ID representing the account. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
currency | text | The currency this account uses (ISO-4217) |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | bigint | Spend converted out of microcurrency (so Spend/1,000,000) |
This SQL query integrates data from multiple Reddit ad-related tables (ad reports, ads, ad groups, campaigns, and accounts) to create a comprehensive view of ad performance. It joins these tables based on common identifiers and source relations, then aggregates the data to calculate total clicks, impressions, and spend for each unique combination of ad, campaign, ad group, and account details.
IntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_report
), ads AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account
), joined AS (
SELECT
report.source_relation,
report.date_day,
report.ad_id,
ads.ad_name,
report.account_id,
campaigns.campaign_name,
ads.campaign_id,
ad_groups.ad_group_name,
ads.ad_group_id,
accounts.currency,
ads.post_id,
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 accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.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
)
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 report. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
ad_name | text | The name of the ad, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
currency | text | The currency this account uses (ISO-4217) |
post_id | text | The ID of the post. |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | bigint | Spend converted out of microcurrency (so Spend/1,000,000) |
This SQL query integrates data from three tables: campaign report, campaigns, and accounts. It joins these tables based on account_id, campaign_id, and source_relation. The query then aggregates the data, summing up clicks, impressions, and spend for each unique combination of source_relation, date_day, account_id, campaign_name, campaign_id, and currency. The result provides a comprehensive view of campaign performance metrics across different accounts and campaigns.
IntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account
), joined AS (
SELECT
report.source_relation,
report.date_day,
report.account_id,
campaigns.campaign_name,
report.campaign_id,
accounts.currency,
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 campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6
)
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 report. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
currency | text | The currency this account uses (ISO-4217) |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | bigint | Spend converted out of microcurrency (so Spend/1,000,000) |
This SQL query integrates data from multiple Reddit ads-related tables (ad reports, ads, ad groups, campaigns, and accounts) to create a comprehensive URL report. It cleans and extracts various URL components and UTM parameters from the click_url field, aggregates metrics like clicks, impressions, and spend, and filters out ads without valid URLs. The query performs complex string manipulations to parse URL information and combines it with other ad-related data to provide a detailed view of ad performance by URL.
IntegrationCleaningFeaturizationAggregationFilteringWITH report AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_report
), ads AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account
), joined AS (
SELECT
report.source_relation,
report.date_day,
ads.ad_name,
report.ad_id,
report.account_id,
campaigns.campaign_name,
ads.campaign_id,
ad_groups.ad_group_name,
ads.ad_group_id,
accounts.currency,
ads.post_id,
ads.post_url,
ads.click_url,
SPLIT_PART(ads.click_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(ads.click_url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(ads.click_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(ads.click_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(ads.click_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(ads.click_url, 'utm_term=', 2), '&', 1), '') AS utm_term,
NULLIF(SPLIT_PART(SPLIT_PART(ads.click_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
COALESCE(
NULLIF(SPLIT_PART(SPLIT_PART(ads.click_url, 'utm_campaign=', 2), '&', 1), ''),
campaigns.campaign_name
) AS utm_campaign,
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 accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.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,
13,
14,
15,
16,
17,
18,
19,
20
), filtered AS (
SELECT
*
FROM joined
WHERE
NOT click_url IS NULL /* filter for only ads with valid URLs */
)
SELECT
*
FROM filtered
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 report. |
ad_name | text | The name of the ad, if present in the source data. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
account_id | bigint | The ID representing the account. |
campaign_name | text | The name of the campaign, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
ad_group_name | text | The name of the ad group, if present in the source data. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
currency | text | The currency this account uses (ISO-4217) |
post_id | text | The ID of the post. |
post_url | integer | The URL belonging to the post. |
click_url | text | The destination url, or the website address, that a visitor goes to when they click on the ad |
clicks | bigint | The count of clicks. |
impressions | bigint | The count of impressions. |
spend | bigint | Spend converted out of microcurrency (so Spend/1,000,000) |
base_url | text | The base URL of the ad, extracted from the `click_url`. |
url_host | text | The URL host of the ad, extracted from the `click_url`. |
url_path | text | The URL path of the ad, extracted from the `click_url`. |
utm_source | text | The site that sent traffic to your page. Microsoft Advertising sets this to Bing; extracted from the `click_url`. |
utm_medium | text | Which channel was used. Microsoft Advertising sets this to cp; extracted from the `click_url`. |
utm_campaign | text | Which campaign the keyword came from; extracted from the `click_url`. |
utm_content | text | Which ad group the keyword came from; extracted from the `click_url`. |
utm_term | text | Which keyword brought people to your website; extracted from the `click_url`. |
This SQL query performs a basic transformation on data from a staging table for Reddit ads accounts. It casts several columns to specific data types, renames the 'id' column to 'account_id', and adds a 'source_relation' column. The query doesn't filter, deduplicate, or aggregate data, but rather focuses on cleaning and standardizing the data structure.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS attribution_type,
CAST(NULL AS TEXT) AS click_attribution_window,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS status,
CAST(NULL AS TEXT) AS time_zone_id,
CAST(NULL AS TEXT) AS view_attribution_window,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
attribution_type,
click_attribution_window,
CAST(created_at AS TIMESTAMP) AS created_at,
currency,
id AS account_id,
status,
time_zone_id,
view_attribution_window
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 representing the account. |
attribution_type | text | Attribution type: "CLICK_THROUGH_CONVERSION", "VIEW_THROUGH_CONVERSION", or "ALL_CONVERSION". CLICK_THROUGH_CONVERSION: A user clicked on your ad and then completed the conversion action on your site. VIEW_THROUGH_CONVERSION: A user saw your ad and did not click it, but did complete the conversion action on your site. ALL_CONVERSION: Combination of both. |
click_attribution_window | text | Determines how long after clicking on your ad you count that user’s actions as a conversion. "DAY", "WEEK", or "MONTH" |
created_at | timestamp without time zone | Time that the respective record (ad, ad group, campaign, post, etc) was created. ISO-8601 timestamp. |
currency | text | The currency this account uses (ISO-4217) |
status | integer | The current state of the advertiser. "PENDING_BILLING", "VALID", "TRUSTED", "ADMIN", "FAILED_BILLING", "SUSPICIOUS", "SUSPENDED", or "BANNED" |
time_zone_id | text | The time zone id preference for this account |
view_attribution_window | text | Determines how long after viewing on your ad you count that user’s actions as a conversion. "DAY", "WEEK", or "MONTH" |
This SQL query performs data transformation on the 'stg_reddit_ads__account_report_tmp' table. It starts by casting NULL values to specific data types for various fields. Then, it selects and renames certain columns, and performs a simple calculation on the 'spend' column (dividing it by 1,000,000). The query also adds a 'source_relation' column with an empty string value.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__account_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS region,
CAST(NULL AS INT) AS spend,
CAST(NULL AS DATE) AS date_day,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
clicks,
date AS date_day,
impressions,
region,
(
spend / 1000000
) AS spend
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 representing the account. |
clicks | integer | The count of clicks. |
date_day | date | YYYY-MM-DD formatted date |
impressions | integer | The count of impressions. |
region | integer | The region (US state or UK country) targeted for the reports |
spend | integer | Spend converted out of microcurrency (so Spend/1,000,000) |
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 |
---|---|---|
account_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
app_install_metrics_add_payment_info | integer | None |
app_install_metrics_add_to_cart | integer | None |
app_install_metrics_app_launch | integer | None |
app_install_metrics_completed_tutorial | integer | None |
app_install_metrics_install | integer | None |
app_install_metrics_level_achieved | integer | None |
app_install_metrics_purchase | integer | None |
app_install_metrics_search | integer | None |
app_install_metrics_sign_up | integer | None |
app_install_metrics_spend | integer | None |
app_install_metrics_spend_credits | integer | None |
app_install_metrics_view_content | integer | None |
clicks | integer | None |
comment_downvotes | integer | None |
comment_upvotes | integer | None |
comments_page_views | integer | None |
conversion_roas | integer | None |
cpc | double precision | None |
ctr | double precision | None |
ecpm | double precision | None |
gallery_item_caption | integer | None |
gallery_item_id | integer | None |
impressions | integer | None |
legacy_click_conversions_attribution_window_day | integer | None |
legacy_click_conversions_attribution_window_month | integer | None |
legacy_click_conversions_attribution_window_week | integer | None |
legacy_view_conversions_attribution_window_day | integer | None |
legacy_view_conversions_attribution_window_month | integer | None |
legacy_view_conversions_attribution_window_week | integer | None |
priority | integer | None |
region | integer | None |
spend | integer | None |
video_fully_viewable_impressions | integer | None |
video_plays_expanded | integer | None |
video_plays_with_sound | integer | None |
video_started | integer | None |
video_viewable_impressions | integer | None |
video_watched_100_percent | integer | None |
video_watched_10_seconds | integer | None |
video_watched_25_percent | integer | None |
video_watched_3_seconds | integer | None |
video_watched_50_percent | integer | None |
video_watched_5_seconds | integer | None |
video_watched_75_percent | integer | None |
video_watched_95_percent | integer | None |
viewable_impressions | integer | None |
viewer_comments | integer | None |
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 a staging table in a dbt (data build tool) project, specifically for Reddit ads account data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
_fivetran_synced | timestamp without time zone | None |
attribution_type | text | None |
click_attribution_window | text | None |
created_at | timestamp without time zone | None |
currency | text | None |
status | integer | None |
time_zone_id | text | None |
view_attribution_window | text | None |
This SQL query stages data from a temporary table for Reddit ads. It performs type casting on various fields, renames some columns, and selects specific columns for the final output. The query doesn't filter, deduplicate, or aggregate data, but rather prepares it for further processing or analysis.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS click_url,
CAST(NULL AS TEXT) AS configured_status,
CAST(NULL AS TEXT) AS effective_status,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS BOOLEAN) AS is_processing,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS post_id,
CAST(NULL AS TEXT) AS post_url,
CAST(NULL AS TEXT) AS rejection_reason,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
ad_group_id,
campaign_id,
click_url,
configured_status,
effective_status,
id AS ad_id,
is_processing,
name AS ad_name,
post_id,
post_url,
rejection_reason
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 representing the ad, if present in the source data. |
account_id | text | The ID representing the account. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
click_url | text | The destination url, or the website address, that a visitor goes to when they click on the ad |
configured_status | text | The status configured by the account owner. "ACTIVE", "PAUSED", "ARCHIVED", "DELETED" |
effective_status | text | The calculated status determining the real status of this entity. |
is_processing | boolean | Whether or not effective status is processing |
ad_name | text | The name of the ad, if present in the source data. |
post_id | text | The ID of the post. |
post_url | integer | The URL belonging to the post. |
rejection_reason | integer | Reason why entity was rejected. |
This SQL query performs data cleaning and transformation on the 'stg_reddit_ads__ad_group_tmp' table. It casts columns to specific data types, renames some columns, and restructures the data. The query doesn't filter or aggregate data, but focuses on standardizing the format and structure of the ad group information from Reddit Ads.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS bid_strategy,
CAST(NULL AS INT) AS bid_value,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS configured_status,
CAST(NULL AS TEXT) AS effective_status,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS BOOLEAN) AS expand_targeting,
CAST(NULL AS TEXT) AS goal_type,
CAST(NULL AS INT) AS goal_value,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS BOOLEAN) AS is_processing,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS optimization_strategy_type,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
bid_strategy,
bid_value,
campaign_id,
configured_status,
effective_status,
CAST(end_time AS TIMESTAMP) AS end_time_at,
expand_targeting,
goal_type,
goal_value,
id AS ad_group_id,
is_processing,
name AS ad_group_name,
optimization_strategy_type,
CAST(start_time AS TIMESTAMP) AS start_time_at
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_group_id | bigint | The ID representing the ad group, if present in the source data. |
account_id | integer | The ID representing the account. |
bid_strategy | text | The bid strategy for this entity. "MAXIMIZE_VOLUME", "MANUAL_BIDDING", or "BIDLESS" |
bid_value | integer | The amount to pay in microcurrency per bidding event. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
configured_status | text | The status configured by the account owner. "ACTIVE", "PAUSED", "ARCHIVED", "DELETED" |
effective_status | text | The calculated status determining the real status of this entity. |
end_time_at | timestamp without time zone | When the entity will stop delivering. |
expand_targeting | boolean | Boolean that when selected, allows Reddit to expand your targeting to maximize your results. |
goal_type | text | The type of goal for the entity. "IMPRESSIONS", "PERCENTAGE", "CLICKS", "CONVERSIONS", "LIFETIME_SPEND", "DAILY_SPEND", or "VIDEO_VIEWABLE_IMPRESSIONS" |
goal_value | integer | The value used to determine the goal has been met. This is measured in microcurrency for monetary goals types. |
is_processing | boolean | Whether or not effective status is processing |
ad_group_name | text | The name of the ad group, if present in the source data. |
optimization_strategy_type | integer | The strategy to use when optimizing the delivery of an ad. "DOWNSTREAM_CONVERSIONS" or "APP_INSTALLS" |
start_time_at | timestamp without time zone | When the entity will begin to deliver. |
This SQL query stages data from a temporary table, casts columns to specific data types, renames some columns, and performs a minor calculation on the 'spend' column (dividing it by 1,000,000). It prepares the data for further processing or analysis by standardizing the structure and format of the data.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_group_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS ad_group_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS region,
CAST(NULL AS INT) AS spend,
CAST(NULL AS DATE) AS date_day,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
ad_group_id,
clicks,
date AS date_day,
impressions,
region,
(
spend / 1000000
) AS spend
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 representing the account. |
ad_group_id | bigint | The ID representing the ad group, if present in the source data. |
clicks | integer | The count of clicks. |
date_day | date | YYYY-MM-DD formatted date |
impressions | integer | The count of impressions. |
region | integer | The region (US state or UK country) targeted for the reports |
spend | integer | Spend converted out of microcurrency (so Spend/1,000,000) |
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 template or schema for the 'model.reddit_ads_source.stg_reddit_ads__ad_group_report_tmp' model without actually returning any data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | bigint | None |
ad_group_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
app_install_metrics_add_payment_info | integer | None |
app_install_metrics_add_to_cart | integer | None |
app_install_metrics_app_launch | integer | None |
app_install_metrics_completed_tutorial | integer | None |
app_install_metrics_install | integer | None |
app_install_metrics_level_achieved | integer | None |
app_install_metrics_purchase | integer | None |
app_install_metrics_search | integer | None |
app_install_metrics_sign_up | integer | None |
app_install_metrics_spend | integer | None |
app_install_metrics_spend_credits | integer | None |
app_install_metrics_view_content | integer | None |
clicks | integer | None |
comment_downvotes | integer | None |
comment_upvotes | integer | None |
comments_page_views | integer | None |
conversion_roas | integer | None |
cpc | double precision | None |
ctr | double precision | None |
ecpm | double precision | None |
gallery_item_caption | integer | None |
gallery_item_id | integer | None |
impressions | integer | None |
legacy_click_conversions_attribution_window_day | integer | None |
legacy_click_conversions_attribution_window_month | integer | None |
legacy_click_conversions_attribution_window_week | integer | None |
legacy_view_conversions_attribution_window_day | integer | None |
legacy_view_conversions_attribution_window_month | integer | None |
legacy_view_conversions_attribution_window_week | integer | None |
priority | integer | None |
region | integer | None |
spend | integer | None |
video_fully_viewable_impressions | integer | None |
video_plays_expanded | integer | None |
video_plays_with_sound | integer | None |
video_started | integer | None |
video_viewable_impressions | integer | None |
video_watched_100_percent | integer | None |
video_watched_10_seconds | integer | None |
video_watched_25_percent | integer | None |
video_watched_3_seconds | integer | None |
video_watched_50_percent | integer | None |
video_watched_5_seconds | integer | None |
video_watched_75_percent | integer | None |
video_watched_95_percent | integer | None |
viewable_impressions | integer | None |
viewer_comments | integer | None |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Reddit ads data related to ad groups.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | integer | None |
id | bigint | None |
_fivetran_synced | timestamp without time zone | None |
bid_strategy | text | None |
bid_value | integer | None |
campaign_id | bigint | None |
configured_status | text | None |
effective_status | text | None |
end_time | timestamp without time zone | None |
expand_targeting | boolean | None |
goal_type | text | None |
goal_value | integer | None |
is_processing | boolean | None |
name | text | None |
optimization_strategy_type | integer | None |
start_time | timestamp without time zone | None |
This SQL query performs data type casting and minor transformations on the 'stg_reddit_ads__ad_report' model. It casts various columns to specific data types, renames the 'date' column to 'date_day', and converts the 'spend' column from microseconds to seconds by dividing it by 1,000,000. The query also includes a 'source_relation' column, though it's set to an empty string in this case.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__ad_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS ad_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS region,
CAST(NULL AS INT) AS spend,
CAST(NULL AS DATE) AS date_day,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
ad_id,
clicks,
date AS date_day,
impressions,
region,
(
spend / 1000000
) AS spend
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 representing the account. |
ad_id | bigint | The ID representing the ad, if present in the source data. |
clicks | integer | The count of clicks. |
date_day | date | YYYY-MM-DD formatted date |
impressions | integer | The count of impressions. |
region | integer | The region (US state or UK country) targeted for the reports |
spend | integer | Spend converted out of microcurrency (so Spend/1,000,000) |
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 a staging table in dbt (data build tool) for Reddit ads data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | bigint | None |
ad_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
app_install_metrics_add_payment_info | integer | None |
app_install_metrics_add_to_cart | integer | None |
app_install_metrics_app_launch | integer | None |
app_install_metrics_completed_tutorial | integer | None |
app_install_metrics_install | integer | None |
app_install_metrics_level_achieved | integer | None |
app_install_metrics_purchase | integer | None |
app_install_metrics_search | integer | None |
app_install_metrics_sign_up | integer | None |
app_install_metrics_spend | integer | None |
app_install_metrics_spend_credits | integer | None |
app_install_metrics_view_content | integer | None |
clicks | integer | None |
comment_downvotes | integer | None |
comment_upvotes | integer | None |
comments_page_views | integer | None |
conversion_roas | integer | None |
cpc | double precision | None |
ctr | double precision | None |
ecpm | double precision | None |
gallery_item_caption | integer | None |
gallery_item_id | integer | None |
impressions | integer | None |
legacy_click_conversions_attribution_window_day | integer | None |
legacy_click_conversions_attribution_window_month | integer | None |
legacy_click_conversions_attribution_window_week | integer | None |
legacy_view_conversions_attribution_window_day | integer | None |
legacy_view_conversions_attribution_window_month | integer | None |
legacy_view_conversions_attribution_window_week | integer | None |
priority | integer | None |
region | integer | None |
spend | integer | None |
video_fully_viewable_impressions | integer | None |
video_plays_expanded | integer | None |
video_plays_with_sound | integer | None |
video_started | integer | None |
video_viewable_impressions | integer | None |
video_watched_100_percent | integer | None |
video_watched_10_seconds | integer | None |
video_watched_25_percent | integer | None |
video_watched_3_seconds | integer | None |
video_watched_50_percent | integer | None |
video_watched_5_seconds | integer | None |
video_watched_75_percent | integer | None |
video_watched_95_percent | integer | None |
viewable_impressions | integer | None |
viewer_comments | integer | None |
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 generating a schema without any actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
_fivetran_synced | text | None |
account_id | text | None |
ad_group_id | bigint | None |
campaign_id | bigint | None |
click_url | text | None |
configured_status | text | None |
effective_status | text | None |
is_processing | boolean | None |
name | text | None |
post_id | text | None |
post_url | integer | None |
preview_expiry | integer | None |
preview_url | integer | None |
rejection_reason | integer | None |
search_term | integer | None |
This SQL query creates a staging table for Reddit ads campaign data. It starts with a base table, then defines a fields CTE that casts all columns to specific data types (mostly setting them to NULL). Finally, it selects and renames some columns from the fields CTE to create the final output. The query appears to be setting up a structure for data that will be populated later, as all fields are currently set to NULL or empty string.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS configured_status,
CAST(NULL AS TEXT) AS effective_status,
CAST(NULL AS TEXT) AS funding_instrument_id,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS BOOLEAN) AS is_processing,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS objective,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
configured_status,
effective_status,
funding_instrument_id,
id AS campaign_id,
is_processing,
name AS campaign_name,
objective
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 representing the campaign, if present in the source data. |
account_id | bigint | The ID representing the account. |
configured_status | text | The status configured by the account owner. "ACTIVE", "PAUSED", "ARCHIVED", "DELETED" |
effective_status | text | The calculated status determining the real status of this entity. |
funding_instrument_id | integer | Campaign level funding instrument id |
is_processing | boolean | Whether or not effective status is processing |
campaign_name | text | The name of the campaign, if present in the source data. |
objective | text | The objective type of a campaign. |
This SQL query performs data type casting and minor transformations on the Reddit ads campaign report data. It starts by selecting all columns from a temporary staging table, then casts each column to a specific data type. The final select statement renames the 'date' column to 'date_day' and converts the 'spend' column from microseconds to seconds by dividing it by 1,000,000. The query also adds a 'source_relation' column, though it's set to an empty string.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_reddit_ads_source.stg_reddit_ads__campaign_report_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS DATE) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS region,
CAST(NULL AS INT) AS spend,
CAST(NULL AS DATE) AS date_day,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
campaign_id,
clicks,
date AS date_day,
impressions,
region,
(
spend / 1000000
) AS spend
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 representing the account. |
campaign_id | bigint | The ID representing the campaign, if present in the source data. |
clicks | integer | The count of clicks. |
date_day | date | YYYY-MM-DD formatted date |
impressions | integer | The count of impressions. |
region | integer | The region (US state or UK country) targeted for the reports |
spend | integer | Spend converted out of microcurrency (so Spend/1,000,000) |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Reddit ads campaign reporting.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | bigint | None |
campaign_id | bigint | None |
date | date | None |
_fivetran_synced | timestamp without time zone | None |
app_install_metrics_add_payment_info | integer | None |
app_install_metrics_add_to_cart | integer | None |
app_install_metrics_app_launch | integer | None |
app_install_metrics_completed_tutorial | integer | None |
app_install_metrics_install | integer | None |
app_install_metrics_level_achieved | integer | None |
app_install_metrics_purchase | integer | None |
app_install_metrics_search | integer | None |
app_install_metrics_sign_up | integer | None |
app_install_metrics_spend | integer | None |
app_install_metrics_spend_credits | integer | None |
app_install_metrics_view_content | integer | None |
clicks | integer | None |
comment_downvotes | integer | None |
comment_upvotes | integer | None |
comments_page_views | integer | None |
conversion_roas | integer | None |
cpc | double precision | None |
ctr | double precision | None |
ecpm | double precision | None |
gallery_item_caption | integer | None |
gallery_item_id | integer | None |
impressions | integer | None |
legacy_click_conversions_attribution_window_day | integer | None |
legacy_click_conversions_attribution_window_month | integer | None |
legacy_click_conversions_attribution_window_week | integer | None |
legacy_view_conversions_attribution_window_day | integer | None |
legacy_view_conversions_attribution_window_month | integer | None |
legacy_view_conversions_attribution_window_week | integer | None |
priority | integer | None |
region | integer | None |
spend | integer | None |
video_fully_viewable_impressions | integer | None |
video_plays_expanded | integer | None |
video_plays_with_sound | integer | None |
video_started | integer | None |
video_viewable_impressions | integer | None |
video_watched_100_percent | integer | None |
video_watched_10_seconds | integer | None |
video_watched_25_percent | integer | None |
video_watched_3_seconds | integer | None |
video_watched_50_percent | integer | None |
video_watched_5_seconds | integer | None |
video_watched_75_percent | integer | None |
video_watched_95_percent | integer | None |
viewable_impressions | integer | None |
viewer_comments | integer | None |
This SQL query creates a temporary table with a single column named '_dbt_source_relation' of type TEXT, initialized with NULL values. The LIMIT 0 clause ensures that no rows are actually returned, effectively creating an empty table structure.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | bigint | None |
_fivetran_synced | timestamp without time zone | None |
account_id | bigint | None |
configured_status | text | None |
effective_status | text | None |
funding_instrument_id | integer | None |
is_processing | boolean | None |
name | text | None |
objective | text | None |
This SQL query aggregates Snapchat ad performance data at the account level. It joins several tables (ad_hourly, ads, ad_squads, campaigns, and account) to combine ad performance metrics with account information. The query filters for the most recent records in the history tables, aggregates metrics like swipes, impressions, and spend by date, account ID, account name, and currency. The result is a daily summary of ad performance for each Snapchat ad account.
FilteringIntegrationAggregationWITH ad_hourly AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_hourly_report
), account AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_squads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), aggregated AS (
SELECT
ad_hourly.source_relation,
CAST(ad_hourly.date_hour AS DATE) AS date_day,
account.ad_account_id,
account.ad_account_name,
account.currency,
SUM(ad_hourly.swipes) AS swipes,
SUM(ad_hourly.impressions) AS impressions,
ROUND(SUM(ad_hourly.spend), 2) AS spend
FROM ad_hourly
LEFT JOIN ads
ON ad_hourly.ad_id = ads.ad_id AND ad_hourly.source_relation = ads.source_relation
LEFT JOIN ad_squads
ON ads.ad_squad_id = ad_squads.ad_squad_id
AND ads.source_relation = ad_squads.source_relation
LEFT JOIN campaigns
ON ad_squads.campaign_id = campaigns.campaign_id
AND ad_squads.source_relation = campaigns.source_relation
LEFT JOIN account
ON campaigns.ad_account_id = account.ad_account_id
AND campaigns.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5
)
SELECT
*
FROM aggregated
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 report. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
currency | text | The current used by the account in Snapchat. |
spend | numeric | The spend on the ad in the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
swipes | bigint | The number of swipes the ad had on the given day. |
This SQL query combines data from multiple Snapchat Ads-related tables to create a comprehensive ad performance report. It processes creative history, URL tags, ad hourly reports, ad account information, and ad history. The query cleans and standardizes URL-related data, extracts UTM parameters, joins various tables to integrate information, and finally aggregates daily ad performance metrics such as swipes, impressions, and spend.
FilteringCleaningFeaturizationIntegrationAggregationWITH __dbt__cte__snapchat_ads__creative_history_prep AS (
WITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_history
WHERE
is_most_recent_record = TRUE
), url_tags AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_url_tag_history
WHERE
is_most_recent_record = TRUE
), url_tags_pivoted AS (
SELECT
source_relation,
creative_id,
MIN(CASE WHEN param_key = 'utm_source' THEN param_value END) AS utm_source,
MIN(CASE WHEN param_key = 'utm_medium' THEN param_value END) AS utm_medium,
MIN(CASE WHEN param_key = 'utm_campaign' THEN param_value END) AS utm_campaign,
MIN(CASE WHEN param_key = 'utm_content' THEN param_value END) AS utm_content,
MIN(CASE WHEN param_key = 'utm_term' THEN param_value END) AS utm_term
FROM url_tags
GROUP BY
1,
2
), fields AS (
SELECT
base.source_relation,
base.creative_id,
base.ad_account_id,
base.creative_name,
base.url,
SPLIT_PART(base.url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(base.url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
COALESCE(
url_tags_pivoted.utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_source=', 2), '&', 1), '')
) AS utm_source,
COALESCE(
url_tags_pivoted.utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_medium=', 2), '&', 1), '')
) AS utm_medium,
COALESCE(
url_tags_pivoted.utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_campaign=', 2), '&', 1), '')
) AS utm_campaign,
COALESCE(
url_tags_pivoted.utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_content=', 2), '&', 1), '')
) AS utm_content,
COALESCE(
url_tags_pivoted.utm_term,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_term=', 2), '&', 1), '')
) AS utm_term
FROM base
LEFT JOIN url_tags_pivoted
ON base.creative_id = url_tags_pivoted.creative_id
AND base.source_relation = url_tags_pivoted.source_relation
)
SELECT
*
FROM fields
), ad_hourly AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_hourly_report
), creatives AS (
SELECT
*
FROM __dbt__cte__snapchat_ads__creative_history_prep
), account AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_history
WHERE
is_most_recent_record = TRUE
), aggregated AS (
SELECT
ad_hourly.source_relation,
CAST(ad_hourly.date_hour AS DATE) AS date_day,
account.ad_account_id,
account.ad_account_name,
ad_hourly.ad_id,
ads.ad_name,
account.currency,
SUM(ad_hourly.swipes) AS swipes,
SUM(ad_hourly.impressions) AS impressions,
ROUND(SUM(ad_hourly.spend), 2) AS spend
FROM ad_hourly
LEFT JOIN ads
ON ad_hourly.ad_id = ads.ad_id AND ad_hourly.source_relation = ads.source_relation
LEFT JOIN creatives
ON ads.creative_id = creatives.creative_id
AND ads.source_relation = creatives.source_relation
LEFT JOIN account
ON creatives.ad_account_id = account.ad_account_id
AND creatives.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM aggregated
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 report. |
ad_id | text | The ID of the ad in Snapchat. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
ad_name | text | The name of the ad in Snapchat. |
currency | text | The current used by the account in Snapchat. |
spend | numeric | The spend on the ad in the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
swipes | bigint | The number of swipes the ad had on the given day. |
This SQL query combines data from multiple Snapchat advertising-related tables to create a comprehensive ad squad report. It joins ad squad hourly data with account, ad squad, and campaign information, filtering for the most recent records where applicable. The query then aggregates metrics such as swipes, impressions, and spend by date, account, campaign, and ad squad, providing a daily summary of ad performance across different organizational levels.
FilteringIntegrationAggregationWITH ad_squad_hourly AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_hourly_report
), account AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history
WHERE
is_most_recent_record = TRUE
), ad_squads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), aggregated AS (
SELECT
ad_squad_hourly.source_relation,
CAST(ad_squad_hourly.date_hour AS DATE) AS date_day,
account.ad_account_id,
account.ad_account_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_squad_hourly.ad_squad_id,
ad_squads.ad_squad_name,
account.currency,
SUM(ad_squad_hourly.swipes) AS swipes,
SUM(ad_squad_hourly.impressions) AS impressions,
ROUND(SUM(ad_squad_hourly.spend), 2) AS spend
FROM ad_squad_hourly
LEFT JOIN ad_squads
ON ad_squad_hourly.ad_squad_id = ad_squads.ad_squad_id
AND ad_squad_hourly.source_relation = ad_squads.source_relation
LEFT JOIN campaigns
ON ad_squads.campaign_id = campaigns.campaign_id
AND ad_squads.source_relation = campaigns.source_relation
LEFT JOIN account
ON campaigns.ad_account_id = account.ad_account_id
AND campaigns.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9
)
SELECT
*
FROM aggregated
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 report. |
ad_squad_id | text | The ID of the ad squad in Snapchat. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
campaign_id | text | The ID of the campaign in Snapchat. |
campaign_name | text | The name of the campaign in Snapchat. |
ad_squad_name | text | The name of the ad squad in Snapchat. |
currency | text | The current used by the account in Snapchat. |
spend | numeric | The spend on the ad in the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
swipes | bigint | The number of swipes the ad had on the given day. |
This SQL query integrates data from three source tables (campaign hourly report, ad account history, and campaign history) to create a daily campaign performance report. It joins the tables, filters for the most recent records in the history tables, aggregates metrics like swipes, impressions, and spend on a daily basis, and includes relevant dimensional information such as account and campaign details.
FilteringIntegrationAggregationWITH campaign_hourly AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_hourly_report
), account AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), aggregated AS (
SELECT
campaign_hourly.source_relation,
CAST(campaign_hourly.date_hour AS DATE) AS date_day,
account.ad_account_id,
account.ad_account_name,
campaign_hourly.campaign_id,
campaigns.campaign_name,
account.currency,
SUM(campaign_hourly.swipes) AS swipes,
SUM(campaign_hourly.impressions) AS impressions,
ROUND(SUM(campaign_hourly.spend), 2) AS spend
FROM campaign_hourly
LEFT JOIN campaigns
ON campaign_hourly.campaign_id = campaigns.campaign_id
AND campaign_hourly.source_relation = campaigns.source_relation
LEFT JOIN account
ON campaigns.ad_account_id = account.ad_account_id
AND campaigns.source_relation = account.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM aggregated
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 report. |
campaign_id | text | The ID of the campaign in Snapchat. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
campaign_name | text | The name of the campaign in Snapchat. |
currency | text | The current used by the account in Snapchat. |
spend | numeric | The spend on the ad in the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
swipes | bigint | The number of swipes the ad had on the given day. |
This SQL query processes Snapchat ad creative data. It starts by filtering for the most recent records from creative history and URL tag history. It then pivots the URL tag data to create columns for UTM parameters. The main part of the query extracts and cleans URL-related information from the creative data, including base URL, host, and path. It also combines UTM parameters from both the URL tags and the URL itself. Finally, it joins this processed data with the pivoted URL tag data to create a comprehensive view of each creative's URL and tracking information.
FilteringCleaningFeaturizationIntegrationAggregationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_history
WHERE
is_most_recent_record = TRUE
), url_tags AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_url_tag_history
WHERE
is_most_recent_record = TRUE
), url_tags_pivoted AS (
SELECT
source_relation,
creative_id,
MIN(CASE WHEN param_key = 'utm_source' THEN param_value END) AS utm_source,
MIN(CASE WHEN param_key = 'utm_medium' THEN param_value END) AS utm_medium,
MIN(CASE WHEN param_key = 'utm_campaign' THEN param_value END) AS utm_campaign,
MIN(CASE WHEN param_key = 'utm_content' THEN param_value END) AS utm_content,
MIN(CASE WHEN param_key = 'utm_term' THEN param_value END) AS utm_term
FROM url_tags
GROUP BY
1,
2
), fields AS (
SELECT
base.source_relation,
base.creative_id,
base.ad_account_id,
base.creative_name,
base.url,
SPLIT_PART(base.url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(base.url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
COALESCE(
url_tags_pivoted.utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_source=', 2), '&', 1), '')
) AS utm_source,
COALESCE(
url_tags_pivoted.utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_medium=', 2), '&', 1), '')
) AS utm_medium,
COALESCE(
url_tags_pivoted.utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_campaign=', 2), '&', 1), '')
) AS utm_campaign,
COALESCE(
url_tags_pivoted.utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_content=', 2), '&', 1), '')
) AS utm_content,
COALESCE(
url_tags_pivoted.utm_term,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_term=', 2), '&', 1), '')
) AS utm_term
FROM base
LEFT JOIN url_tags_pivoted
ON base.creative_id = url_tags_pivoted.creative_id
AND base.source_relation = url_tags_pivoted.source_relation
)
SELECT
*
FROM fields
Name | Type | Comment |
---|
This SQL query creates a comprehensive report for Snapchat ads with URL tracking. It combines data from various sources including ad creatives, ad hourly reports, ad accounts, ad squads, and campaigns. The query extracts URL components and UTM parameters, joins this information with ad performance data, and aggregates metrics like swipes, impressions, and spend. The result is a detailed view of ad performance tied to specific URL and UTM parameter combinations, allowing for analysis of ad effectiveness across different campaigns, content, and targeting strategies.
FilteringCleaningFeaturizationIntegrationAggregationWITH __dbt__cte__snapchat_ads__creative_history_prep AS (
WITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_history
WHERE
is_most_recent_record = TRUE
), url_tags AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_url_tag_history
WHERE
is_most_recent_record = TRUE
), url_tags_pivoted AS (
SELECT
source_relation,
creative_id,
MIN(CASE WHEN param_key = 'utm_source' THEN param_value END) AS utm_source,
MIN(CASE WHEN param_key = 'utm_medium' THEN param_value END) AS utm_medium,
MIN(CASE WHEN param_key = 'utm_campaign' THEN param_value END) AS utm_campaign,
MIN(CASE WHEN param_key = 'utm_content' THEN param_value END) AS utm_content,
MIN(CASE WHEN param_key = 'utm_term' THEN param_value END) AS utm_term
FROM url_tags
GROUP BY
1,
2
), fields AS (
SELECT
base.source_relation,
base.creative_id,
base.ad_account_id,
base.creative_name,
base.url,
SPLIT_PART(base.url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(base.url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '/'), 0),
STR_POSITION(REPLACE(REPLACE(base.url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
COALESCE(
url_tags_pivoted.utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_source=', 2), '&', 1), '')
) AS utm_source,
COALESCE(
url_tags_pivoted.utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_medium=', 2), '&', 1), '')
) AS utm_medium,
COALESCE(
url_tags_pivoted.utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_campaign=', 2), '&', 1), '')
) AS utm_campaign,
COALESCE(
url_tags_pivoted.utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_content=', 2), '&', 1), '')
) AS utm_content,
COALESCE(
url_tags_pivoted.utm_term,
NULLIF(SPLIT_PART(SPLIT_PART(base.url, 'utm_term=', 2), '&', 1), '')
) AS utm_term
FROM base
LEFT JOIN url_tags_pivoted
ON base.creative_id = url_tags_pivoted.creative_id
AND base.source_relation = url_tags_pivoted.source_relation
)
SELECT
*
FROM fields
), ad_hourly AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_hourly_report
), creatives AS (
SELECT
*
FROM __dbt__cte__snapchat_ads__creative_history_prep
), account AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history
WHERE
is_most_recent_record = TRUE
), ads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_history
WHERE
is_most_recent_record = TRUE
), ad_squads AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_history
WHERE
is_most_recent_record = TRUE
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_history
WHERE
is_most_recent_record = TRUE
), aggregated AS (
SELECT
ad_hourly.source_relation,
CAST(ad_hourly.date_hour AS DATE) AS date_day,
account.ad_account_id,
account.ad_account_name,
ad_hourly.ad_id,
ads.ad_name,
ad_squads.ad_squad_id,
ad_squads.ad_squad_name,
campaigns.campaign_id,
campaigns.campaign_name,
account.currency,
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(ad_hourly.swipes) AS swipes,
SUM(ad_hourly.impressions) AS impressions,
ROUND(SUM(ad_hourly.spend), 2) AS spend
FROM ad_hourly
LEFT JOIN ads
ON ad_hourly.ad_id = ads.ad_id AND ad_hourly.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_squads
ON ads.ad_squad_id = ad_squads.ad_squad_id
AND ads.source_relation = ad_squads.source_relation
LEFT JOIN campaigns
ON ad_squads.campaign_id = campaigns.campaign_id
AND ad_squads.source_relation = campaigns.source_relation
LEFT JOIN account
ON creatives.ad_account_id = account.ad_account_id
AND creatives.source_relation = account.source_relation
/* We only want utm ads to populate this report. Therefore, we filter where url ads are populated. */
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
)
SELECT
*
FROM aggregated
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 report. |
ad_id | text | The ID of the ad in Snapchat. |
ad_squad_id | text | The ID of the ad squad in Snapchat. |
campaign_id | text | The ID of the campaign in Snapchat. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
ad_name | text | The name of the ad in Snapchat. |
ad_squad_name | text | The name of the ad squad in Snapchat. |
campaign_name | text | The name of the campaign in Snapchat. |
currency | text | The current used by the account in Snapchat. |
base_url | text | The base URL of the ad, extracted from the web_view_url. |
url_host | text | The URL host of the ad, extracted from the web_view_url. |
url_path | text | The URL path of the ad, extracted from the web_view_url. |
utm_source | text | The utm_source parameter of the ad, extracted from the web_view_url. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the web_view_url. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the web_view_url. |
utm_content | text | The utm_content parameter of the ad, extracted from the web_view_url. |
utm_term | text | The utm_term parameter of the ad, extracted from the web_view_url. |
spend | numeric | The spend on the ad in the given day. |
impressions | bigint | The number of impressions the ad had on the given day. |
swipes | bigint | The number of swipes the ad had on the given day. |
This SQL query stages data from a Snapchat Ads source table, casting and renaming columns to create a standardized structure for ad account history. It includes type casting, column renaming, and adds a flag to identify the most recent record for each ad account. The query doesn't perform any filtering or aggregation but focuses on data cleaning and structuring.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_account_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS advertiser,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS timezone,
CAST(NULL AS TEXT) AS type,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_account_id,
name AS ad_account_name,
CAST(created_at AS TIMESTAMP) AS created_at,
advertiser,
currency,
timezone,
CAST(_fivetran_synced AS TIMESTAMP) AS _fivetran_synced,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_synced | timestamp without time zone | When the record was last synced by Fivetran. |
ad_account_id | text | The ID of the account in Snapchat. |
ad_account_name | text | The name of the account in Snapchat. |
advertiser | text | Name of advertiser |
currency | text | Currency used by account |
created_at | timestamp without time zone | Created at timestamp |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
timezone | text | Account timezone |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 data processing or as part of a larger data modeling process in dbt (data build tool).
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | text | None |
name | text | None |
_fivetran_synced | text | None |
This SQL query performs several operations on the snapchat_ads_source.stg_snapchat_ads__ad_history data. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types. The query then selects and renames certain columns, casts timestamps, and adds a column to identify the most recent record for each ad. The final result includes all columns from this transformation.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_squad_id,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS creative_id,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_id,
name AS ad_name,
CAST(created_at AS TIMESTAMP) AS created_at,
ad_squad_id,
creative_id,
CAST(_fivetran_synced AS TIMESTAMP) AS _fivetran_synced,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_synced | timestamp without time zone | When the record was last synced by Fivetran. |
ad_id | text | The ID of the ad in Snapchat. |
ad_squad_id | text | The ID of the related ad squad in Snapchat. |
creative_id | text | The ID of the related creative in Snapchat. |
ad_name | text | The name of the ad in Snapchat. |
created_at | timestamp without time zone | Created at timestamp |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, initialized to NULL. The LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used to define the structure of a temporary table or view without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | text | None |
ad_squad_id | text | None |
creative_id | text | None |
name | text | None |
_fivetran_synced | text | None |
This SQL query performs data transformation and cleaning on the Snapchat ads hourly report data. It casts various fields to appropriate data types, renames some columns, and performs unit conversions (e.g., milliseconds to seconds) for time-related fields. The query also adds a source_relation column to track the data source.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_hourly_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS ad_id,
CAST(NULL AS DECIMAL(28, 6)) AS attachment_quartile_1,
CAST(NULL AS DECIMAL(28, 6)) AS attachment_quartile_2,
CAST(NULL AS DECIMAL(28, 6)) AS attachment_quartile_3,
CAST(NULL AS DECIMAL(28, 6)) AS attachment_total_view_time_millis,
CAST(NULL AS DECIMAL(28, 6)) AS attachment_view_completion,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS DECIMAL(28, 6)) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS quartile_1,
CAST(NULL AS DECIMAL(28, 6)) AS quartile_2,
CAST(NULL AS DECIMAL(28, 6)) AS quartile_3,
CAST(NULL AS DECIMAL(28, 6)) AS saves,
CAST(NULL AS DECIMAL(28, 6)) AS screen_time_millis,
CAST(NULL AS DECIMAL(28, 6)) AS shares,
CAST(NULL AS DECIMAL(28, 6)) AS spend,
CAST(NULL AS DECIMAL(28, 6)) AS swipes,
CAST(NULL AS DECIMAL(28, 6)) AS video_views,
CAST(NULL AS DECIMAL(28, 6)) AS view_completion,
CAST(NULL AS DECIMAL(28, 6)) AS view_time_millis,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
ad_id,
CAST(date AS TIMESTAMP) AS date_hour,
attachment_quartile_1,
attachment_quartile_2,
attachment_quartile_3,
(
attachment_total_view_time_millis / 1000000.0
) AS attachment_total_view_time,
attachment_view_completion,
quartile_1,
quartile_2,
quartile_3,
saves,
shares,
(
screen_time_millis / 1000000.0
) AS screen_time,
video_views,
view_completion,
(
view_time_millis / 1000000.0
) AS view_time,
impressions,
(
spend / 1000000.0
) AS spend,
swipes
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 | text | The ID of the ad that the report relates to in Snapchat. |
date_hour | timestamp without time zone | The hour that the report relates to. |
attachment_quartile_1 | numeric(28,6) | Number of times your Long Form Video was viewed to 25% |
attachment_quartile_2 | numeric(28,6) | Number of times your Long Form Video was viewed to 50% |
attachment_quartile_3 | numeric(28,6) | Number of times your Long Form Video was viewed to 75% |
attachment_total_view_time | numeric | Total time Snapchatters spent on the attachment (HH:MM:SS) |
attachment_view_completion | numeric(28,6) | The number of times your Long Form Video was viewed to completion (97% ) |
quartile_1 | numeric(28,6) | The number of times your video ad was viewed to 25% |
quartile_2 | numeric(28,6) | The number of times your video ad was viewed to 50% |
quartile_3 | numeric(28,6) | The number of times your video ad was viewed to 75% |
saves | numeric(28,6) | Number of times a lens/filter was saved to Memories |
shares | numeric(28,6) | Number of times a lens/filter was shared in a Chat or Story |
screen_time | numeric | The number of milli seconds spent watching your ad across all paid impressions. Screen time starts recording as soon as the media is fully rendered on the device and the autoplay video starts |
video_views | numeric(28,6) | The number of times your video was watched at least 2 seconds or swiped up, whichever comes first |
view_completion | numeric(28,6) | The number of times your video ad was viewed to completion (97% ) |
view_time | numeric | The number of milli seconds spent watching your ad across all users reached. |
impressions | integer | The number of impressions for an ad in the hour of the record. |
spend | numeric | The amount of spend for an ad in the hour of the record converted from micros. |
swipes | integer | The number of swipes for an ad in the hour of the record. |
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 limits the output to 0 rows, effectively creating a schema-only representation of the table without any data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_id | text | None |
date | timestamp without time zone | None |
impressions | integer | None |
spend | integer | None |
swipes | integer | None |
This SQL query stages data from a temporary table for Snapchat ad squad history. It casts several fields to appropriate data types, renames some columns, and adds a flag to identify the most recent record for each ad squad. The query also includes a source relation field, though it's currently set to an empty string.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS ad_squad_id,
name AS ad_squad_name,
CAST(created_at AS TIMESTAMP) AS created_at,
campaign_id,
CAST(_fivetran_synced AS TIMESTAMP) AS _fivetran_synced,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_synced | timestamp without time zone | When the record was last synced by Fivetran. |
ad_squad_id | text | The ID of the ad squad in Snapchat. |
ad_squad_name | text | The name of the ad squad in Snapchat. |
created_at | timestamp without time zone | Created at timestamp. |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
campaign_id | text | The ID of the related campaign in Snapchat. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT. The query limits the result to 0 rows, effectively creating a template or skeleton for the table structure without any actual data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | text | None |
campaign_id | text | None |
name | text | None |
_fivetran_synced | text | None |
This SQL query performs data cleaning and featurization on the snapchat_ads source table. It casts various fields to specific data types, converts time-based fields from milliseconds to seconds, and renames some columns. The query also adds a source_relation column and restructures the data into a final format.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__ad_squad_hourly_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS ad_squad_id,
CAST(NULL AS INT) AS attachment_quartile_1,
CAST(NULL AS INT) AS attachment_quartile_2,
CAST(NULL AS INT) AS attachment_quartile_3,
CAST(NULL AS INT) AS attachment_total_view_time_millis,
CAST(NULL AS INT) AS attachment_view_completion,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS quartile_1,
CAST(NULL AS INT) AS quartile_2,
CAST(NULL AS INT) AS quartile_3,
CAST(NULL AS INT) AS saves,
CAST(NULL AS INT) AS screen_time_millis,
CAST(NULL AS INT) AS shares,
CAST(NULL AS INT) AS spend,
CAST(NULL AS INT) AS swipes,
CAST(NULL AS INT) AS video_views,
CAST(NULL AS INT) AS view_completion,
CAST(NULL AS INT) AS view_time_millis,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
ad_squad_id,
CAST(date AS TIMESTAMP) AS date_hour,
attachment_quartile_1,
attachment_quartile_2,
attachment_quartile_3,
(
attachment_total_view_time_millis / 1000000.0
) AS attachment_total_view_time,
attachment_view_completion,
quartile_1,
quartile_2,
quartile_3,
saves,
shares,
(
screen_time_millis / 1000000.0
) AS screen_time,
video_views,
view_completion,
(
view_time_millis / 1000000.0
) AS view_time,
impressions,
(
spend / 1000000.0
) AS spend,
swipes
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_squad_id | text | ID of the ad squad. |
date_hour | timestamp without time zone | The hour that the report relates to. |
attachment_quartile_1 | integer | Number of times your Long Form Video was viewed to 25% |
attachment_quartile_2 | integer | Number of times your Long Form Video was viewed to 50% |
attachment_quartile_3 | integer | Number of times your Long Form Video was viewed to 75% |
attachment_total_view_time | numeric | Total time Snapchatters spent on the attachment (HH:MM:SS) |
attachment_view_completion | integer | The number of times your Long Form Video was viewed to completion (97% ) |
quartile_1 | integer | The number of times your video ad was viewed to 25% |
quartile_2 | integer | The number of times your video ad was viewed to 50% |
quartile_3 | integer | The number of times your video ad was viewed to 75% |
saves | integer | Number of times a lens/filter was saved to Memories |
shares | integer | Number of times a lens/filter was shared in a Chat or Story |
screen_time | numeric | The number of milli seconds spent watching your ad across all paid impressions. Screen time starts recording as soon as the media is fully rendered on the device and the autoplay video starts |
video_views | integer | The number of times your video was watched at least 2 seconds or swiped up, whichever comes first |
view_completion | integer | The number of times your video ad was viewed to completion (97% ) |
view_time | numeric | The number of milli seconds spent watching your ad across all users reached. |
impressions | integer | The number of impressions for an ad in the hour of the record. |
spend | numeric | The amount of spend for an ad in the hour of the record. |
swipes | integer | The number of swipes for an ad in the hour of the record. Swipes are when your ad was swiped up on or the CTA was tapped to view the attachment below. |
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 LIMIT 0 clause ensures no rows are returned. This is likely used as a template or placeholder for further development or testing purposes.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_squad_id | text | None |
date | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
android_installs | integer | None |
attachment_avg_view_time_millis | integer | None |
attachment_quartile_1 | integer | None |
attachment_quartile_2 | integer | None |
attachment_quartile_3 | integer | None |
attachment_total_view_time_millis | integer | None |
attachment_view_completion | integer | None |
avg_screen_time_millis | integer | None |
avg_view_time_millis | integer | None |
conversion_add_billing | integer | None |
conversion_add_cart | integer | None |
conversion_app_opens | integer | None |
conversion_level_completes | integer | None |
conversion_page_views | integer | None |
conversion_purchases | integer | None |
conversion_purchases_value | integer | None |
conversion_save | integer | None |
conversion_searches | integer | None |
conversion_sign_ups | integer | None |
conversion_start_checkout | integer | None |
conversion_view_content | integer | None |
impressions | integer | None |
ios_installs | integer | None |
quartile_1 | integer | None |
quartile_2 | integer | None |
quartile_3 | integer | None |
saves | integer | None |
screen_time_millis | integer | None |
shares | integer | None |
spend | integer | None |
story_completes | integer | None |
story_opens | integer | None |
swipe_up_percent | integer | None |
swipes | integer | None |
total_installs | integer | None |
video_views | integer | None |
view_completion | integer | None |
view_time_millis | integer | None |
This SQL query stages data from a temporary Snapchat ads campaign history table. It casts specific fields to appropriate 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' field, though it's set to an empty string in this case.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_account_id,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS campaign_id,
ad_account_id,
CAST(created_at AS TIMESTAMP) AS created_at,
name AS campaign_name,
CAST(_fivetran_synced AS TIMESTAMP) AS _fivetran_synced,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_synced | timestamp without time zone | When the record was last synced by Fivetran. |
campaign_id | text | The id of the campaign in Snapchat. |
campaign_name | text | The name of the campaign in Snapchat. |
ad_account_id | text | The ID of the related ad account in Snapchat. |
created_at | timestamp without time zone | Created at timestamp. |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 a staging table in a dbt (data build tool) project, specifically for Snapchat ads campaign history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | text | None |
ad_account_id | text | None |
name | text | None |
_fivetran_synced | text | None |
This SQL query performs data transformation and type casting on a Snapchat ads campaign hourly report. It starts with a base table, then defines a set of fields with specific data types. The final select statement applies further transformations, including converting milliseconds to seconds for time-based fields and adjusting the spend value. The query also renames some fields and ensures consistent data types for the output.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__campaign_hourly_report_tmp
), fields AS (
SELECT
CAST(NULL AS INT) AS attachment_quartile_1,
CAST(NULL AS INT) AS attachment_quartile_2,
CAST(NULL AS INT) AS attachment_quartile_3,
CAST(NULL AS INT) AS attachment_total_view_time_millis,
CAST(NULL AS INT) AS attachment_view_completion,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS INT) AS quartile_1,
CAST(NULL AS INT) AS quartile_2,
CAST(NULL AS INT) AS quartile_3,
CAST(NULL AS INT) AS saves,
CAST(NULL AS INT) AS screen_time_millis,
CAST(NULL AS INT) AS shares,
CAST(NULL AS INT) AS spend,
CAST(NULL AS INT) AS swipes,
CAST(NULL AS INT) AS video_views,
CAST(NULL AS INT) AS view_completion,
CAST(NULL AS INT) AS view_time_millis,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
campaign_id,
CAST(date AS TIMESTAMP) AS date_hour,
attachment_quartile_1,
attachment_quartile_2,
attachment_quartile_3,
(
attachment_total_view_time_millis / 1000000.0
) AS attachment_total_view_time,
attachment_view_completion,
quartile_1,
quartile_2,
quartile_3,
saves,
shares,
(
screen_time_millis / 1000000.0
) AS screen_time,
video_views,
view_completion,
(
view_time_millis / 1000000.0
) AS view_time,
impressions,
(
spend / 1000000.0
) AS spend,
swipes
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 | text | ID of campaign |
date_hour | timestamp without time zone | The hour that the report relates to. |
attachment_quartile_1 | integer | Number of times your Long Form Video was viewed to 25% |
attachment_quartile_2 | integer | Number of times your Long Form Video was viewed to 50% |
attachment_quartile_3 | integer | Number of times your Long Form Video was viewed to 75% |
attachment_total_view_time | numeric | Total time Snapchatters spent on the attachment (HH:MM:SS) |
attachment_view_completion | integer | The number of times your Long Form Video was viewed to completion (97% ) |
quartile_1 | integer | The number of times your video ad was viewed to 25% |
quartile_2 | integer | The number of times your video ad was viewed to 50% |
quartile_3 | integer | The number of times your video ad was viewed to 75% |
saves | integer | Number of times a lens/filter was saved to Memories |
shares | integer | Number of times a lens/filter was shared in a Chat or Story |
screen_time | numeric | The number of milli seconds spent watching your ad across all paid impressions. Screen time starts recording as soon as the media is fully rendered on the device and the autoplay video starts |
video_views | integer | The number of times your video was watched at least 2 seconds or swiped up, whichever comes first |
view_completion | integer | The number of times your video ad was viewed to completion (97% ) |
view_time | numeric | The number of milli seconds spent watching your ad across all users reached. |
impressions | integer | The number of impressions for an ad in the hour of the record. |
spend | numeric | The amount of spend for an ad in the hour of the record. |
swipes | integer | The number of swipes for an ad in the hour of the record. Swipes are when your ad was swiped up on or the CTA was tapped to view the attachment below. |
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 LIMIT 0 ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
campaign_id | text | None |
date | timestamp without time zone | None |
_fivetran_synced | timestamp without time zone | None |
android_installs | integer | None |
attachment_avg_view_time_millis | integer | None |
attachment_quartile_1 | integer | None |
attachment_quartile_2 | integer | None |
attachment_quartile_3 | integer | None |
attachment_total_view_time_millis | integer | None |
attachment_view_completion | integer | None |
avg_screen_time_millis | integer | None |
avg_view_time_millis | integer | None |
conversion_add_billing | integer | None |
conversion_add_cart | integer | None |
conversion_app_opens | integer | None |
conversion_level_completes | integer | None |
conversion_page_views | integer | None |
conversion_purchases | integer | None |
conversion_purchases_value | integer | None |
conversion_save | integer | None |
conversion_searches | integer | None |
conversion_sign_ups | integer | None |
conversion_start_checkout | integer | None |
conversion_view_content | integer | None |
impressions | integer | None |
ios_installs | integer | None |
quartile_1 | integer | None |
quartile_2 | integer | None |
quartile_3 | integer | None |
saves | integer | None |
screen_time_millis | integer | None |
shares | integer | None |
spend | integer | None |
story_completes | integer | None |
story_opens | integer | None |
swipe_up_percent | integer | None |
swipes | integer | None |
total_installs | integer | None |
video_views | integer | None |
view_completion | integer | None |
view_time_millis | integer | None |
This SQL query stages data from a Snapchat Ads creative history source. It performs type casting on various fields, renames some columns, and adds a flag to identify the most recent record for each creative ID. The query also includes a source relation field and filters for the most recent record per creative ID.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_history_tmp
), fields AS (
SELECT
CAST(NULL AS TIMESTAMP) AS _fivetran_synced,
CAST(NULL AS TEXT) AS ad_account_id,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS TEXT) AS web_view_url,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS creative_id,
CAST(created_at AS TIMESTAMP) AS created_at,
ad_account_id,
name AS creative_name,
web_view_url AS url,
CAST(_fivetran_synced AS TIMESTAMP) AS _fivetran_synced,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY _fivetran_synced DESC) = 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_synced | timestamp without time zone | When the record was last synced by Fivetran. |
creative_id | text | The ID of the creative in Snapchat. |
creative_name | text | The name of the creative in Snapchat. |
ad_account_id | text | The ID of the related ad account in Snapchat. |
url | text | The URL of the creative if the creative is of type 'web view'. |
created_at | timestamp without time zone | Created at timestamp. |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | text | None |
ad_account_id | text | None |
name | text | None |
web_view_url | text | None |
_fivetran_synced | text | None |
This SQL query stages data from a temporary table, casts specific fields to desired data types, renames some columns, and adds a flag to identify the most recent record for each unique combination of source_relation, creative_id, and key. It prepares the data for further processing or analysis in the Snapchat ads context.
CleaningDeduplicationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_snapchat_ads_source.stg_snapchat_ads__creative_url_tag_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS creative_id,
CAST(NULL AS TEXT) AS key,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS TEXT) AS value,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
creative_id,
key AS param_key,
value AS param_value,
CAST(updated_at AS TIMESTAMP) AS updated_at,
ROW_NUMBER() OVER (PARTITION BY source_relation, creative_id, key ORDER BY updated_at DESC) = 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. |
creative_id | text | The ID of the related creative in Snapchat. |
param_key | text | The URL parameter key, i.e. UTM_SOURCE. |
param_value | text | The URL parameter value, i.e. Snapchat. |
updated_at | timestamp without time zone | Timestamp of when the record was updated. |
is_most_recent_record | boolean | Boolean representing whether a record is the most recent version of that record. |
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 development, or to create a temporary table structure without populating it with data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
creative_id | text | None |
key | text | None |
value | text | None |
updated_at | text | None |
This SQL query aggregates TikTok ad performance data at the ad group level. It joins hourly ad group report data with ad group, advertiser, and campaign information. The query calculates various performance metrics such as impressions, clicks, spend, and engagement metrics. It also computes derived metrics like CPC, CPM, and CTR. The results are grouped by date, advertiser, campaign, and ad group details.
IntegrationAggregationFeaturizationWITH hourly AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_report_hourly
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_history
WHERE
is_most_recent_record
), advertiser AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_history
WHERE
is_most_recent_record
), aggregated AS (
SELECT
hourly.source_relation,
CAST(hourly.stat_time_hour AS DATE) AS date_day,
ad_groups.advertiser_id,
advertiser.advertiser_name,
campaigns.campaign_id,
campaigns.campaign_name,
hourly.ad_group_id,
ad_groups.ad_group_name,
advertiser.currency,
ad_groups.category,
ad_groups.gender,
ad_groups.audience_type,
ad_groups.budget,
SUM(hourly.impressions) AS impressions,
SUM(hourly.clicks) AS clicks,
SUM(hourly.spend) AS spend,
SUM(hourly.reach) AS reach,
SUM(hourly.conversion) AS conversion,
SUM(hourly.likes) AS likes,
SUM(hourly.comments) AS comments,
SUM(hourly.shares) AS shares,
SUM(hourly.profile_visits) AS profile_visits,
SUM(hourly.follows) AS follows,
SUM(hourly.video_watched_2_s) AS video_watched_2_s,
SUM(hourly.video_watched_6_s) AS video_watched_6_s,
SUM(hourly.video_views_p_25) AS video_views_p_25,
SUM(hourly.video_views_p_50) AS video_views_p_50,
SUM(hourly.video_views_p_75) AS video_views_p_75,
SUM(hourly.spend) / NULLIF(SUM(hourly.clicks), 0) AS daily_cpc,
(
SUM(hourly.spend) / NULLIF(SUM(hourly.impressions), 0)
) * 1000 AS daily_cpm,
(
SUM(hourly.clicks) / NULLIF(SUM(hourly.impressions), 0)
) * 100 AS daily_ctr
FROM hourly
LEFT JOIN ad_groups
ON hourly.ad_group_id = ad_groups.ad_group_id
AND hourly.source_relation = ad_groups.source_relation
LEFT JOIN advertiser
ON ad_groups.advertiser_id = advertiser.advertiser_id
AND ad_groups.source_relation = advertiser.source_relation
LEFT JOIN campaigns
ON ad_groups.campaign_id = campaigns.campaign_id
AND ad_groups.source_relation = campaigns.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
)
SELECT
*
FROM aggregated
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 | Day of record |
ad_group_id | integer | Ad group ID |
ad_group_name | text | Ad group name |
campaign_id | integer | Campaign ID |
campaign_name | text | Campaign name |
advertiser_id | integer | Advertiser ID |
advertiser_name | text | Advertiser name. |
currency | text | Advertiser's currency. |
audience_type | integer | Audience Type |
budget | integer | Ad budget. Returns 0.0 when Campaign Budget Optimization (budget_optimize_switch) is on. |
category | integer | Ad group category. |
gender | text | Gender that you want to target. |
impressions | bigint | The number of impressions that occurred on the day of the record. |
clicks | bigint | The number of clicks that occurred on the day of the record. |
spend | double precision | The amount of spend that occurred on the day of the record. |
reach | bigint | The number of unique users who saw your ads at least once. This metric is estimated. |
conversion | bigint | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
likes | bigint | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
comments | bigint | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
shares | bigint | The number of shares that occurred on the day of the record. |
profile_visits | bigint | The number of profile visits that occurred on the day of the record. |
follows | bigint | The number of follows that occurred on the day of the record. |
video_watched_2_s | bigint | The number of times your video played for at least 2 seconds, or completely played. Replays will not be counted. |
video_watched_6_s | bigint | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
video_views_p_25 | bigint | The number of times your video was played at 25% of its length. Replays will not be counted. |
video_views_p_50 | bigint | The number of times your video was played at 50% of its length. Replays will not be counted. |
video_views_p_75 | bigint | The number of times your video was played at 75% of its length. Replays will not be counted. |
daily_cpc | double precision | The average amount of money you've spent on a click. |
daily_cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
daily_ctr | bigint | The percentage of times people saw your ad and performed a click. |
This SQL query aggregates and integrates data from various TikTok ad-related tables. It combines hourly ad report data with information about ads, ad groups, advertisers, and campaigns. The query calculates various metrics such as impressions, clicks, spend, and engagement metrics (likes, comments, shares, etc.) on a daily basis. It also computes derived metrics like CPC (Cost Per Click), CPM (Cost Per Mille), and CTR (Click-Through Rate). The result is a comprehensive daily report of ad performance across different dimensions of the advertising structure.
IntegrationAggregationFeaturizationFilteringWITH hourly AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_report_hourly
), ads AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_history
WHERE
is_most_recent_record
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_history
WHERE
is_most_recent_record
), advertiser AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_history
WHERE
is_most_recent_record
), aggregated AS (
SELECT
hourly.source_relation,
CAST(hourly.stat_time_hour AS DATE) AS date_day,
ad_groups.advertiser_id,
advertiser.advertiser_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_groups.ad_group_id,
ad_groups.ad_group_name,
hourly.ad_id,
ads.ad_name,
advertiser.currency,
ad_groups.category,
ad_groups.gender,
ad_groups.audience_type,
ad_groups.budget,
SUM(hourly.impressions) AS impressions,
SUM(hourly.clicks) AS clicks,
SUM(hourly.spend) AS spend,
SUM(hourly.reach) AS reach,
SUM(hourly.conversion) AS conversion,
SUM(hourly.likes) AS likes,
SUM(hourly.comments) AS comments,
SUM(hourly.shares) AS shares,
SUM(hourly.profile_visits) AS profile_visits,
SUM(hourly.follows) AS follows,
SUM(hourly.video_watched_2_s) AS video_watched_2_s,
SUM(hourly.video_watched_6_s) AS video_watched_6_s,
SUM(hourly.video_views_p_25) AS video_views_p_25,
SUM(hourly.video_views_p_50) AS video_views_p_50,
SUM(hourly.video_views_p_75) AS video_views_p_75,
SUM(hourly.spend) / NULLIF(SUM(hourly.clicks), 0) AS daily_cpc,
(
SUM(hourly.spend) / NULLIF(SUM(hourly.impressions), 0)
) * 1000 AS daily_cpm,
(
SUM(hourly.clicks) / NULLIF(SUM(hourly.impressions), 0)
) * 100 AS daily_ctr
FROM hourly
LEFT JOIN ads
ON hourly.ad_id = ads.ad_id AND hourly.source_relation = ads.source_relation
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.source_relation
LEFT JOIN advertiser
ON ads.advertiser_id = advertiser.advertiser_id
AND ads.source_relation = advertiser.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,
13,
14,
15
)
SELECT
*
FROM aggregated
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 | Day of record. |
ad_id | integer | Ad ID. |
ad_name | text | Ad name. |
campaign_id | integer | Campaign ID. |
campaign_name | text | Campaign name. |
advertiser_name | text | Advertiser name. |
advertiser_id | integer | Advertiser ID. |
ad_group_id | integer | Ad group ID. |
ad_group_name | text | Ad group name. |
audience_type | integer | Audience Type |
budget | integer | Ad budget. Returns 0.0 when Campaign Budget Optimization (budget_optimize_switch) is on. |
category | integer | Ad group category. |
gender | text | Gender that you want to target. |
currency | text | Advertiser's currency. |
impressions | bigint | The number of impressions that occurred on the day of the record. |
clicks | bigint | The number of clicks that occurred on the day of the record. |
spend | double precision | The amount of spend that occurred on the day of the record. |
reach | bigint | The number of unique users who saw your ads at least once. This metric is estimated. |
conversion | bigint | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
likes | bigint | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
comments | bigint | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
shares | bigint | The number of shares that occurred on the day of the record. |
profile_visits | bigint | The number of profile visits that occurred on the day of the record. |
follows | bigint | The number of follows that occurred on the day of the record. |
video_watched_2_s | bigint | The number of times your video played for at least 2 seconds, or completely played. Replays will not be counted. |
video_watched_6_s | bigint | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
video_views_p_25 | bigint | The number of times your video was played at 25% of its length. Replays will not be counted. |
video_views_p_50 | bigint | The number of times your video was played at 50% of its length. Replays will not be counted. |
video_views_p_75 | bigint | The number of times your video was played at 75% of its length. Replays will not be counted. |
daily_cpc | double precision | The average amount of money you've spent on a click. |
daily_cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
daily_ctr | bigint | The percentage of times people saw your ad and performed a click. |
This SQL query aggregates TikTok ad performance data at the advertiser level. It joins hourly ad report data with advertiser and ad history information, then calculates various performance metrics such as clicks, impressions, spend, and engagement metrics. The query also computes derived metrics like CPC, CPM, and CTR. The results are grouped by source relation, date, advertiser ID, advertiser name, and currency.
IntegrationAggregationFeaturizationFilteringWITH hourly AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_report_hourly
), advertiser AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser
), ads AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_history
WHERE
is_most_recent_record
), joined AS (
SELECT
hourly.source_relation,
CAST(hourly.stat_time_hour AS DATE) AS date_day,
ads.advertiser_id,
advertiser.advertiser_name,
advertiser.currency,
SUM(hourly.clicks) AS clicks,
SUM(hourly.impressions) AS impressions,
SUM(hourly.spend) AS spend,
SUM(hourly.reach) AS reach,
SUM(hourly.conversion) AS conversion,
SUM(hourly.likes) AS likes,
SUM(hourly.comments) AS comments,
SUM(hourly.shares) AS shares,
SUM(hourly.profile_visits) AS profile_visits,
SUM(hourly.follows) AS follows,
SUM(hourly.video_watched_2_s) AS video_watched_2_s,
SUM(hourly.video_watched_6_s) AS video_watched_6_s,
SUM(hourly.video_views_p_25) AS video_views_p_25,
SUM(hourly.video_views_p_50) AS video_views_p_50,
SUM(hourly.video_views_p_75) AS video_views_p_75,
SUM(hourly.spend) / NULLIF(SUM(hourly.clicks), 0) AS daily_cpc,
(
SUM(hourly.spend) / NULLIF(SUM(hourly.impressions), 0)
) * 1000 AS daily_cpm,
(
SUM(hourly.clicks) / NULLIF(SUM(hourly.impressions), 0)
) * 100 AS daily_ctr
FROM hourly
LEFT JOIN ads
ON hourly.ad_id = ads.ad_id AND hourly.source_relation = ads.source_relation
LEFT JOIN advertiser
ON ads.advertiser_id = advertiser.advertiser_id
AND ads.source_relation = advertiser.source_relation
GROUP BY
1,
2,
3,
4,
5
)
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 | Day of record. |
advertiser_name | text | Advertiser name. |
advertiser_id | integer | Advertiser ID. |
currency | text | Advertiser's currency. |
impressions | bigint | The number of impressions that occurred on the day of the record. |
clicks | bigint | The number of clicks that occurred on the day of the record. |
spend | double precision | The amount of spend that occurred on the day of the record. |
reach | bigint | The number of unique users who saw your ads at least once. This metric is estimated. |
conversion | bigint | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
likes | bigint | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
comments | bigint | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
shares | bigint | The number of shares that occurred on the day of the record. |
profile_visits | bigint | The number of profile visits that occurred on the day of the record. |
follows | bigint | The number of follows that occurred on the day of the record. |
video_watched_2_s | bigint | The number of times your video played for at least 2 seconds, or completely played. Replays will not be counted. |
video_watched_6_s | bigint | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
video_views_p_25 | bigint | The number of times your video was played at 25% of its length. Replays will not be counted. |
video_views_p_50 | bigint | The number of times your video was played at 50% of its length. Replays will not be counted. |
video_views_p_75 | bigint | The number of times your video was played at 75% of its length. Replays will not be counted. |
daily_cpc | double precision | The average amount of money you've spent on a click. |
daily_cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
daily_ctr | bigint | The percentage of times people saw your ad and performed a click. |
This SQL query combines data from three tables (campaign report, campaign history, and advertiser) to create a daily aggregated report of TikTok ad campaign performance. It joins the tables, aggregates hourly data to daily level, calculates various performance metrics (impressions, clicks, spend, etc.), and computes derived metrics like CPC, CPM, and CTR.
IntegrationAggregationFeaturizationWITH hourly AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_report_hourly
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_history
WHERE
is_most_recent_record
), advertiser AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser
), aggregated AS (
SELECT
hourly.source_relation,
CAST(hourly.stat_time_hour AS DATE) AS date_day,
advertiser.advertiser_id,
advertiser.advertiser_name,
hourly.campaign_id,
campaigns.campaign_name,
advertiser.currency,
SUM(hourly.impressions) AS impressions,
SUM(hourly.clicks) AS clicks,
SUM(hourly.spend) AS spend,
SUM(hourly.reach) AS reach,
SUM(hourly.conversion) AS conversion,
SUM(hourly.likes) AS likes,
SUM(hourly.comments) AS comments,
SUM(hourly.shares) AS shares,
SUM(hourly.profile_visits) AS profile_visits,
SUM(hourly.follows) AS follows,
SUM(hourly.video_watched_2_s) AS video_watched_2_s,
SUM(hourly.video_watched_6_s) AS video_watched_6_s,
SUM(hourly.video_views_p_25) AS video_views_p_25,
SUM(hourly.video_views_p_50) AS video_views_p_50,
SUM(hourly.video_views_p_75) AS video_views_p_75,
SUM(hourly.spend) / NULLIF(SUM(hourly.clicks), 0) AS daily_cpc,
(
SUM(hourly.spend) / NULLIF(SUM(hourly.impressions), 0)
) * 1000 AS daily_cpm,
(
SUM(hourly.clicks) / NULLIF(SUM(hourly.impressions), 0)
) * 100 AS daily_ctr
FROM hourly
LEFT JOIN campaigns
ON hourly.campaign_id = campaigns.campaign_id
AND hourly.source_relation = campaigns.source_relation
LEFT JOIN advertiser
ON campaigns.advertiser_id = advertiser.advertiser_id
AND campaigns.source_relation = advertiser.source_relation
GROUP BY
1,
2,
3,
4,
5,
6,
7
)
SELECT
*
FROM aggregated
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 | Day of record |
campaign_id | integer | Campaign ID |
campaign_name | text | Campaign name |
currency | text | Advertiser's currency. |
advertiser_id | integer | Advertiser ID |
advertiser_name | text | Advertiser name |
impressions | bigint | The number of impressions that occurred on the day of the record. |
clicks | bigint | The number of clicks that occurred on the day of the record. |
spend | double precision | The amount of spend that occurred on the day of the record. |
reach | bigint | The number of unique users who saw your ads at least once. This metric is estimated. |
conversion | bigint | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
likes | bigint | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
comments | bigint | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
shares | bigint | The number of shares that occurred on the day of the record. |
profile_visits | bigint | The number of profile visits that occurred on the day of the record. |
follows | bigint | The number of follows that occurred on the day of the record. |
video_watched_2_s | bigint | The number of times your video played for at least 2 seconds, or completely played. Replays will not be counted. |
video_watched_6_s | bigint | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
video_views_p_25 | bigint | The number of times your video was played at 25% of its length. Replays will not be counted. |
video_views_p_50 | bigint | The number of times your video was played at 50% of its length. Replays will not be counted. |
video_views_p_75 | bigint | The number of times your video was played at 75% of its length. Replays will not be counted. |
daily_cpc | double precision | The average amount of money you've spent on a click. |
daily_cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
daily_ctr | bigint | The percentage of times people saw your ad and performed a click. |
This SQL query combines data from multiple TikTok ad-related tables to create a comprehensive daily report of ad performance metrics. It joins ad reports with ad, ad group, advertiser, and campaign data, then aggregates various performance metrics (like impressions, clicks, spend) by date and ad details. The query also calculates derived metrics such as CPC, CPM, and CTR. It filters for ads with populated URL fields and includes various ad targeting and UTM parameters.
FilteringIntegrationAggregationFeaturizationWITH hourly AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_report_hourly
), ads AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_history
WHERE
is_most_recent_record
), ad_groups AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_history
WHERE
is_most_recent_record
), advertiser AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_history
WHERE
is_most_recent_record
), aggregated AS (
SELECT
hourly.source_relation,
CAST(hourly.stat_time_hour AS DATE) AS date_day,
ad_groups.advertiser_id,
advertiser.advertiser_name,
campaigns.campaign_id,
campaigns.campaign_name,
ad_groups.ad_group_id,
ad_groups.ad_group_name,
hourly.ad_id,
ads.ad_name,
ads.base_url,
ads.url_host,
ads.url_path,
ads.utm_source,
ads.utm_medium,
ads.utm_campaign,
ads.utm_content,
ads.utm_term,
advertiser.currency,
ad_groups.category,
ad_groups.gender,
ad_groups.audience_type,
ad_groups.budget,
SUM(hourly.impressions) AS impressions,
SUM(hourly.clicks) AS clicks,
SUM(hourly.spend) AS spend,
SUM(hourly.reach) AS reach,
SUM(hourly.conversion) AS conversion,
SUM(hourly.likes) AS likes,
SUM(hourly.comments) AS comments,
SUM(hourly.shares) AS shares,
SUM(hourly.profile_visits) AS profile_visits,
SUM(hourly.follows) AS follows,
SUM(hourly.video_watched_2_s) AS video_watched_2_s,
SUM(hourly.video_watched_6_s) AS video_watched_6_s,
SUM(hourly.video_views_p_25) AS video_views_p_25,
SUM(hourly.video_views_p_50) AS video_views_p_50,
SUM(hourly.video_views_p_75) AS video_views_p_75,
SUM(hourly.spend) / NULLIF(SUM(hourly.clicks), 0) AS daily_cpc,
(
SUM(hourly.spend) / NULLIF(SUM(hourly.impressions), 0)
) * 1000 AS daily_cpm,
(
SUM(hourly.clicks) / NULLIF(SUM(hourly.impressions), 0)
) * 100 AS daily_ctr
FROM hourly
LEFT JOIN ads
ON hourly.ad_id = ads.ad_id AND hourly.source_relation = ads.source_relation
LEFT JOIN ad_groups
ON ads.ad_group_id = ad_groups.ad_group_id
AND ads.source_relation = ad_groups.source_relation
LEFT JOIN advertiser
ON ads.advertiser_id = advertiser.advertiser_id
AND ads.source_relation = advertiser.source_relation
LEFT JOIN campaigns
ON ads.campaign_id = campaigns.campaign_id
AND ads.source_relation = campaigns.source_relation
/* We are filtering for only ads where url fields are populated. */
WHERE
NOT ads.landing_page_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,
21,
22,
23
)
SELECT
*
FROM aggregated
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 | Day of record. |
ad_id | integer | Ad ID. |
ad_name | text | Ad name. |
campaign_id | integer | Campaign ID. |
campaign_name | text | Campaign name. |
advertiser_name | text | Advertiser name. |
advertiser_id | integer | Advertiser ID. |
ad_group_id | integer | Ad group ID. |
ad_group_name | text | Ad group name. |
base_url | text | The base URL of the ad, extracted from the `landing page url`. |
url_host | text | The URL host of the ad, extracted from the `landing page url`. |
url_path | text | The URL path of the ad, extracted from the `landing page url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `landing page url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `landing page url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `landing page url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `landing page url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `landing page url`. |
audience_type | integer | Audience Type |
budget | integer | Ad budget. Returns 0.0 when Campaign Budget Optimization (budget_optimize_switch) is on. |
category | integer | Ad group category. |
gender | text | Gender that you want to target. |
currency | text | Advertiser's currency. |
impressions | bigint | The number of impressions that occurred on the day of the record. |
clicks | bigint | The number of clicks that occurred on the day of the record. |
spend | double precision | The amount of spend that occurred on the day of the record. |
reach | bigint | The number of unique users who saw your ads at least once. This metric is estimated. |
conversion | bigint | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
likes | bigint | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
comments | bigint | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
shares | bigint | The number of shares that occurred on the day of the record. |
profile_visits | bigint | The number of profile visits that occurred on the day of the record. |
follows | bigint | The number of follows that occurred on the day of the record. |
video_watched_2_s | bigint | The number of times your video played for at least 2 seconds, or completely played. Replays will not be counted. |
video_watched_6_s | bigint | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
video_views_p_25 | bigint | The number of times your video was played at 25% of its length. Replays will not be counted. |
video_views_p_50 | bigint | The number of times your video was played at 50% of its length. Replays will not be counted. |
video_views_p_75 | bigint | The number of times your video was played at 75% of its length. Replays will not be counted. |
daily_cpc | double precision | The average amount of money you've spent on a click. |
daily_cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
daily_ctr | bigint | The percentage of times people saw your ad and performed a click. |
This SQL query stages data from a temporary TikTok ads table, casts various fields to specific data types, renames some columns, coalesces age-related fields, and adds a flag to identify the most recent record for each ad group. It also includes a source relation field for tracking purposes.
CleaningDeduplicationFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_history_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS action_days,
CAST(NULL AS DECIMAL(28, 6)) AS adgroup_id,
CAST(NULL AS TEXT) AS adgroup_name,
CAST(NULL AS DECIMAL(28, 6)) AS advertiser_id,
CAST(NULL AS TEXT) AS audience_type,
CAST(NULL AS FLOAT) AS budget,
CAST(NULL AS DECIMAL(28, 6)) AS campaign_id,
CAST(NULL AS DECIMAL(28, 6)) AS category,
CAST(NULL AS TEXT) AS display_name,
CAST(NULL AS DECIMAL(28, 6)) AS frequency,
CAST(NULL AS DECIMAL(28, 6)) AS frequency_schedule,
CAST(NULL AS TEXT) AS gender,
CAST(NULL AS TEXT) AS landing_page_url,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST(NULL AS TEXT) AS interest_category_v_2,
CAST(NULL AS TEXT) AS action_categories,
CAST(NULL AS TEXT) AS age,
CAST(NULL AS TEXT) AS age_groups,
CAST(NULL AS TEXT) AS languages,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
adgroup_id AS ad_group_id,
CAST(updated_at AS TIMESTAMP) AS updated_at,
advertiser_id,
campaign_id,
action_days,
action_categories,
adgroup_name AS ad_group_name,
COALESCE(age_groups, age) AS age_groups,
audience_type,
budget,
category,
display_name,
interest_category_v_2 AS interest_category,
frequency,
frequency_schedule,
gender,
languages,
landing_page_url,
ROW_NUMBER() OVER (PARTITION BY source_relation, adgroup_id ORDER BY updated_at DESC) = 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_group_id | integer | Ad group ID |
updated_at | timestamp without time zone | Time the record was updated. |
advertiser_id | integer | Advertiser ID |
action_categories | text | IDs of the action categories (behaviors) that you want to target. |
campaign_id | integer | The Ad group's campaign ID. |
action_days | integer | Action days |
ad_group_name | text | Ad group name. Character limit is 512 and cannot contain emoji. |
age_groups | text | Age groups you want to target. |
audience_type | integer | Audience Type |
budget | integer | Ad budget. Returns 0.0 when Campaign Budget Optimization (budget_optimize_switch) is on. |
category | integer | Ad group category. |
display_name | integer | Display name of ad group. |
interest_category | text | Interest classification. If the interest is specified, users that do not meet interest target will be excluded during delivery. |
frequency | integer | frequency, together with frequency_schedule, controls how often people see your ad (only available for REACH ads). For example, frequency = 2 frequency_schedule = 3 means "show ads no more than twice every 3 day". |
frequency_schedule | integer | frequency, together with frequency, controls how often people see your ad (only available for REACH ads). |
gender | text | Gender that you want to target. |
landing_page_url | integer | Landing page URL. |
languages | text | Codes of the languages that you want to target. |
is_most_recent_record | boolean | Whether record is the most recent one for this particular grain. |
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 likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for TikTok ads data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
adgroup_id | integer | None |
updated_at | text | None |
advertiser_id | integer | None |
campaign_id | integer | None |
create_time | text | None |
adgroup_name | text | None |
placement_type | text | None |
profile_image | integer | None |
landing_page_url | integer | None |
display_name | integer | None |
app_type | integer | None |
app_download_url | integer | None |
app_name | integer | None |
optimization_event | text | None |
secondary_optimization_event | integer | None |
creative_material_mode | text | None |
audience_type | integer | None |
gender | text | None |
min_android_version | integer | None |
min_ios_version | integer | None |
budget_mode | text | None |
schedule_type | text | None |
dayparting | integer | None |
optimization_goal | text | None |
cpv_video_duration | integer | None |
pacing | text | None |
billing_event | text | None |
bid_type | text | None |
deep_bid_type | integer | None |
impression_tracking_url | integer | None |
click_tracking_url | integer | None |
secondary_status | text | None |
operation_status | text | None |
statistic_type | integer | None |
video_download | text | None |
open_url | integer | None |
open_url_type | integer | None |
fallback_type | integer | None |
budget | integer | None |
bid_price | integer | None |
conversion_bid_price | integer | None |
deep_cpa_bid | integer | None |
schedule_start_time | text | None |
schedule_end_time | text | None |
app_id | integer | None |
pixel_id | integer | None |
inventory_filter_enabled | boolean | None |
is_hfss | boolean | None |
is_new_structure | boolean | None |
category | integer | None |
is_comment_disable | integer | None |
skip_learning_phase | integer | None |
frequency | integer | None |
frequency_schedule | integer | None |
action_days | integer | None |
audience | text | None |
excluded_audience | text | None |
location | text | None |
interest_category_v_2 | text | None |
pangle_block_app_list_id | text | None |
action_categories | text | None |
placements | text | None |
keywords | integer | None |
age_groups | text | None |
languages | text | None |
operating_systems | text | None |
network_types | text | None |
carriers | text | None |
video_actions | text | None |
package | integer | None |
_fivetran_synced | text | None |
This SQL query performs data type casting and column renaming on a TikTok ads ad group hourly report. It starts with a base table, defines the desired data types and column names in a CTE named 'fields', and then selects and rearranges these fields in the final output. The query primarily focuses on standardizing data types and column names without performing any complex transformations or aggregations.
CleaningWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_group_report_hourly_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS adgroup_id,
CAST(NULL AS FLOAT) AS average_video_play,
CAST(NULL AS FLOAT) AS average_video_play_per_user,
CAST(NULL AS DECIMAL(28, 6)) AS clicks,
CAST(NULL AS DECIMAL(28, 6)) AS comments,
CAST(NULL AS DECIMAL(28, 6)) AS conversion,
CAST(NULL AS FLOAT) AS conversion_rate,
CAST(NULL AS FLOAT) AS cost_per_conversion,
CAST(NULL AS FLOAT) AS cpc,
CAST(NULL AS FLOAT) AS cpm,
CAST(NULL AS FLOAT) AS ctr,
CAST(NULL AS DECIMAL(28, 6)) AS follows,
CAST(NULL AS DECIMAL(28, 6)) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS likes,
CAST(NULL AS DECIMAL(28, 6)) AS profile_visits,
CAST(NULL AS DECIMAL(28, 6)) AS reach,
CAST(NULL AS DECIMAL(28, 6)) AS shares,
CAST(NULL AS DECIMAL(28, 6)) AS spend,
CAST(NULL AS TIMESTAMP) AS stat_time_hour,
CAST(NULL AS DECIMAL(28, 6)) AS video_play_actions,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_25,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_50,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_75,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_2_s,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_6_s,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
adgroup_id AS ad_group_id,
CAST(stat_time_hour AS TIMESTAMP) AS stat_time_hour,
cpc,
cpm,
ctr,
impressions,
clicks,
spend,
reach,
conversion,
cost_per_conversion,
conversion_rate,
likes,
comments,
shares,
profile_visits,
follows,
video_play_actions,
video_watched_2_s,
video_watched_6_s,
video_views_p_25,
video_views_p_50,
video_views_p_75,
average_video_play,
average_video_play_per_user
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_group_id | integer | Ad group id |
stat_time_hour | timestamp without time zone | Hour of activity |
cost_per_conversion | double precision | The average amount of money you've spent on a conversion. (The total count is calculated based on the time each ad impression occurred.) |
cpc | double precision | The average amount of money you've spent on a click. |
video_play_actions | integer | The number of times your video starts to play. Replays will not be counted. |
conversion_rate | integer | The percentage of results you received out of all the clicks of your ads. (The total count is calculated based on the time each ad impression occurred.) |
video_views_p_75 | integer | The number of times your video was played at 75% of its length. Replays will not be counted. |
video_views_p_50 | integer | The number of times your video was played at 50% of its length. Replays will not be counted. |
impressions | integer | The number of times your ads were on screen. |
comments | integer | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
conversion | integer | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
shares | integer | The number of shares your video creative received within 1 day of a user seeing a paid ad. |
clicks | integer | The number of clicks on your ads. |
cost_per_1000_reached | None | The average cost to reach 1,000 unique users. This metric is estimated. |
video_views_p_25 | integer | The number of times your video was played at 25% of its length. Replays will not be counted. |
reach | integer | The number of unique users who saw your ads at least once. This metric is estimated. |
profile_visits_rate | None | The rate of profile visits per impression the paid ad drove during the campaign. This metric is only for Boosted TikToks. |
average_video_play | double precision | The average time your video was played per single video view, including any time spent replaying the video. |
average_video_play_per_user | double precision | The average time per user your video was played per single video view, including any time spent replaying the video. |
profile_visits | integer | The number of profile visits the ad drove during the campaign. This metric is only for Boosted TikToks. |
cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
ctr | double precision | The percentage of times people saw your ad and performed a click. |
video_watched_2_s | integer | The number of times your video played for at least 2 seconds. Replays will not be counted. |
follows | integer | The number of new followers that were gained within 1 day of a user seeing a paid ad. This metric is only for Boosted TikToks. |
video_watched_6_s | integer | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
spend | double precision | The estimated total amount of money you've spent on your campaign, ad group or ad during its schedule. |
likes | integer | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
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 a staging table in a dbt (data build tool) project, specifically for TikTok ads data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
adgroup_id | integer | None |
stat_time_hour | timestamp without time zone | None |
cost_per_conversion | double precision | None |
real_time_conversion | integer | None |
cpc | double precision | None |
video_play_actions | integer | None |
conversion_rate | integer | None |
video_views_p_75 | integer | None |
result | integer | None |
video_views_p_50 | integer | None |
impressions | integer | None |
comments | integer | None |
real_time_cost_per_result | double precision | None |
conversion | integer | None |
real_time_result | integer | None |
video_views_p_100 | integer | None |
shares | integer | None |
real_time_conversion_rate | double precision | None |
cost_per_secondary_goal_result | text | None |
secondary_goal_result_rate | text | None |
clicks | integer | None |
cost_per_1000_reached | double precision | None |
video_views_p_25 | integer | None |
reach | integer | None |
real_time_cost_per_conversion | double precision | None |
profile_visits_rate | integer | None |
average_video_play | double precision | None |
profile_visits | integer | None |
cpm | double precision | None |
ctr | double precision | None |
video_watched_2_s | integer | None |
follows | integer | None |
result_rate | double precision | None |
video_watched_6_s | integer | None |
secondary_goal_result | text | None |
cost_per_result | double precision | None |
average_video_play_per_user | double precision | None |
real_time_result_rate | double precision | None |
spend | double precision | None |
likes | integer | None |
_fivetran_synced | text | None |
This SQL query processes data from a TikTok ads history source table. It performs several transformations on the data, including casting data types, extracting URL components (base URL, host, path), parsing UTM parameters, and identifying the most recent record for each ad. The query also cleans and standardizes the landing page URL by removing common prefixes and splitting it into components.
CleaningFeaturizationDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_history_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS ad_id,
CAST(NULL AS TEXT) AS ad_name,
CAST(NULL AS DECIMAL(28, 6)) AS adgroup_id,
CAST(NULL AS DECIMAL(28, 6)) AS advertiser_id,
CAST(NULL AS TEXT) AS call_to_action,
CAST(NULL AS DECIMAL(28, 6)) AS campaign_id,
CAST(NULL AS TEXT) AS click_tracking_url,
CAST(NULL AS TEXT) AS impression_tracking_url,
CAST(NULL AS TEXT) AS landing_page_url,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
ad_id,
CAST(updated_at AS TIMESTAMP) AS updated_at,
adgroup_id AS ad_group_id,
advertiser_id,
campaign_id,
ad_name,
call_to_action,
click_tracking_url,
impression_tracking_url,
SPLIT_PART(landing_page_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(
REPLACE(REPLACE(landing_page_url, 'android-app://', ''), 'http://', ''),
'https://',
''
),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(landing_page_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(landing_page_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(landing_page_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(landing_page_url, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(landing_page_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(landing_page_url, 'utm_term=', 2), '&', 1), '') AS utm_term,
landing_page_url,
ROW_NUMBER() OVER (PARTITION BY source_relation, ad_id ORDER BY updated_at DESC) = 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 | integer | Ad ID |
updated_at | timestamp without time zone | Time the record was updated. |
ad_group_id | integer | Ad group ID |
advertiser_id | integer | Advertiser ID |
base_url | text | The base URL of the ad, extracted from the `landing page url`. |
campaign_id | integer | Campaign ID |
ad_name | text | Ad Name. |
call_to_action | text | Call to action values. |
click_tracking_url | integer | Click monitoring URL. |
impression_tracking_url | integer | Display monitoring URL. |
landing_page_url | text | Landing page URL. |
url_host | text | The URL host of the ad, extracted from the `landing_page_url`. |
url_path | text | The URL path of the ad, extracted from the `landing_page_url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `landing_page_url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `landing_page_url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `landing_page_url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `landing_page_url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `landing_page_url`. |
is_most_recent_record | boolean | Whether record is the most recent one for this particular grain. |
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) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_id | integer | None |
updated_at | text | None |
advertiser_id | integer | None |
adgroup_id | integer | None |
campaign_id | integer | None |
create_time | text | None |
ad_name | text | None |
call_to_action | text | None |
secondary_status | text | None |
operation_status | text | None |
ad_text | text | None |
video_id | text | None |
app_name | integer | None |
deeplink | integer | None |
landing_page_url | text | None |
display_name | text | None |
profile_image_url | integer | None |
impression_tracking_url | integer | None |
click_tracking_url | integer | None |
playable_url | integer | None |
is_aco | boolean | None |
creative_authorized | boolean | None |
is_new_structure | boolean | None |
image_ids | text | None |
_fivetran_synced | text | None |
This SQL query performs a data type casting and column selection operation on a TikTok ads hourly report table. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types (mostly numeric and timestamp). Finally, it selects and renames these fields, ensuring consistent data types and structure for the ad report data.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__ad_report_hourly_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS ad_id,
CAST(NULL AS FLOAT) AS average_video_play,
CAST(NULL AS FLOAT) AS average_video_play_per_user,
CAST(NULL AS DECIMAL(28, 6)) AS clicks,
CAST(NULL AS DECIMAL(28, 6)) AS comments,
CAST(NULL AS DECIMAL(28, 6)) AS conversion,
CAST(NULL AS FLOAT) AS conversion_rate,
CAST(NULL AS FLOAT) AS cost_per_conversion,
CAST(NULL AS FLOAT) AS cpc,
CAST(NULL AS FLOAT) AS cpm,
CAST(NULL AS FLOAT) AS ctr,
CAST(NULL AS DECIMAL(28, 6)) AS follows,
CAST(NULL AS DECIMAL(28, 6)) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS likes,
CAST(NULL AS DECIMAL(28, 6)) AS profile_visits,
CAST(NULL AS DECIMAL(28, 6)) AS reach,
CAST(NULL AS DECIMAL(28, 6)) AS shares,
CAST(NULL AS DECIMAL(28, 6)) AS spend,
CAST(NULL AS TIMESTAMP) AS stat_time_hour,
CAST(NULL AS DECIMAL(28, 6)) AS video_play_actions,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_25,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_50,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_75,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_2_s,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_6_s,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
ad_id,
CAST(stat_time_hour AS TIMESTAMP) AS stat_time_hour,
cpc,
cpm,
ctr,
impressions,
clicks,
spend,
reach,
conversion,
cost_per_conversion,
conversion_rate,
likes,
comments,
shares,
profile_visits,
follows,
video_play_actions,
video_watched_2_s,
video_watched_6_s,
video_views_p_25,
video_views_p_50,
video_views_p_75,
average_video_play,
average_video_play_per_user
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 | integer | Ad id |
stat_time_hour | timestamp without time zone | Hour of activity |
cost_per_conversion | double precision | The average amount of money you've spent on a conversion. (The total count is calculated based on the time each ad impression occurred.) |
cpc | double precision | The average amount of money you've spent on a click. |
video_play_actions | integer | The number of times your video starts to play. Replays will not be counted. |
conversion_rate | integer | The percentage of results you received out of all the clicks of your ads. (The total count is calculated based on the time each ad impression occurred.) |
video_views_p_75 | integer | The number of times your video was played at 75% of its length. Replays will not be counted. |
video_views_p_50 | integer | The number of times your video was played at 50% of its length. Replays will not be counted. |
impressions | integer | The number of times your ads were on screen. |
comments | integer | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
conversion | integer | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
shares | integer | The number of shares your video creative received within 1 day of a user seeing a paid ad. |
clicks | integer | The number of clicks on your ads. |
cost_per_1000_reached | None | The average cost to reach 1,000 unique users. This metric is estimated. |
video_views_p_25 | integer | The number of times your video was played at 25% of its length. Replays will not be counted. |
reach | integer | The number of unique users who saw your ads at least once. This metric is estimated. |
profile_visits_rate | None | The rate of profile visits per impression the paid ad drove during the campaign. This metric is only for Boosted TikToks. |
average_video_play | double precision | The average time your video was played per single video view, including any time spent replaying the video. |
average_video_play_per_user | integer | The average time per user your video was played per single video view, including any time spent replaying the video. |
profile_visits | integer | The number of profile visits the ad drove during the campaign. This metric is only for Boosted TikToks. |
cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
ctr | double precision | The percentage of times people saw your ad and performed a click. |
video_watched_2_s | integer | The number of times your video played for at least 2 seconds. Replays will not be counted. |
follows | integer | The number of new followers that were gained within 1 day of a user seeing a paid ad. This metric is only for Boosted TikToks. |
video_watched_6_s | integer | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
spend | double precision | The estimated total amount of money you've spent on your campaign, ad group or ad during its schedule. |
likes | integer | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
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 |
---|---|---|
ad_id | integer | None |
stat_time_hour | timestamp without time zone | None |
cost_per_conversion | double precision | None |
real_time_conversion | integer | None |
cpc | double precision | None |
video_play_actions | integer | None |
conversion_rate | integer | None |
video_views_p_75 | integer | None |
result | integer | None |
video_views_p_50 | integer | None |
impressions | integer | None |
comments | integer | None |
real_time_cost_per_result | double precision | None |
conversion | integer | None |
real_time_result | integer | None |
video_views_p_100 | integer | None |
shares | integer | None |
real_time_conversion_rate | integer | None |
cost_per_secondary_goal_result | text | None |
secondary_goal_result_rate | text | None |
clicks | integer | None |
cost_per_1000_reached | integer | None |
video_views_p_25 | integer | None |
reach | integer | None |
real_time_cost_per_conversion | double precision | None |
profile_visits_rate | integer | None |
average_video_play | double precision | None |
profile_visits | integer | None |
cpm | double precision | None |
ctr | double precision | None |
video_watched_2_s | integer | None |
follows | integer | None |
result_rate | integer | None |
video_watched_6_s | integer | None |
secondary_goal_result | text | None |
cost_per_result | double precision | None |
average_video_play_per_user | integer | None |
real_time_result_rate | integer | None |
spend | double precision | None |
likes | integer | None |
_fivetran_synced | text | None |
This SQL query stages data for a TikTok ads advertiser model. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types (mostly as NULL or empty string placeholders). Finally, it selects and renames certain fields, and combines some fields using COALESCE to handle potential duplicates or alternatives (e.g., cellphone_number and phone_number).
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__advertiser_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS address,
CAST(NULL AS FLOAT) AS balance,
CAST(NULL AS TEXT) AS cellphone_number,
CAST(NULL AS TEXT) AS company,
CAST(NULL AS TEXT) AS contacter,
CAST(NULL AS TEXT) AS country,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS TEXT) AS description,
CAST(NULL AS TEXT) AS email,
CAST(NULL AS DECIMAL(28, 6)) AS id,
CAST(NULL AS TEXT) AS industry,
CAST(NULL AS TEXT) AS language,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS phone_number,
CAST(NULL AS TEXT) AS telephone,
CAST(NULL AS TEXT) AS telephone_number,
CAST(NULL AS TEXT) AS timezone,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
id AS advertiser_id,
address,
balance,
company,
contacter,
country,
currency,
description,
email,
industry,
language,
name AS advertiser_name,
COALESCE(cellphone_number, phone_number) AS cellphone_number,
COALESCE(telephone_number, telephone) AS telephone_number,
timezone
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. |
advertiser_id | integer | Advertiser ID |
address | text | Advertiser address information |
balance | double precision | Account available balance |
company | text | Advertiser's company name |
contacter | integer | Contact Person |
country | text | The advertiser's country |
create_time | None | Advertiser's create time |
currency | text | Type of currency used by advertisers |
description | text | Brand description, i.e. promotional content |
text | Advertiser contact email, desensitised data | |
industry | integer | Advertiser industry category |
language | text | Language used by advertisers |
license_no | None | License number |
license_url | None | License preview address, the link is valid for an hour by default. |
advertiser_name | text | Advertiser name |
cellphone_number | text | Contact mobile number, desensitised data |
telephone_number | text | Fixed phone number, desensitised data |
timezone | text | Ad account time zone including GMT offset |
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 development, or to establish a schema without actually populating data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
name | text | None |
address | text | None |
company | text | None |
contacter | integer | None |
country | text | None |
currency | text | None |
description | text | None |
text | None | |
industry | integer | None |
license_no | integer | None |
license_url | integer | None |
promotion_area | integer | None |
rejected_reason | integer | None |
role | text | None |
status | text | None |
telephone_number | text | None |
timezone | text | None |
balance | double precision | None |
create_time | text | None |
language | text | None |
cellphone_number | text | None |
_fivetran_synced | text | None |
This SQL query stages data from a temporary table, casts fields to specific data types, and identifies the most recent record for each campaign. It structures the data into a standardized format with specific column names and types, preparing it for further processing or analysis.
CleaningDeduplicationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS advertiser_id,
CAST(NULL AS DECIMAL(28, 6)) AS campaign_id,
CAST(NULL AS TEXT) AS campaign_name,
CAST(NULL AS TEXT) AS campaign_type,
CAST(NULL AS TEXT) AS split_test_variable,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
campaign_id,
CAST(updated_at AS TIMESTAMP) AS updated_at,
advertiser_id,
campaign_name,
campaign_type,
split_test_variable,
ROW_NUMBER() OVER (PARTITION BY source_relation, campaign_id ORDER BY updated_at DESC) = 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 | integer | Campaign ID |
updated_at | timestamp without time zone | Time the record was updated. |
advertiser_id | integer | Advertiser ID |
campaign_name | text | Campaign name |
campaign_type | text | Campaign Type, indicates the campaign is a regular campaign or iOS 14 campaign. |
split_test_variable | text | Split Test variables. Optional values; TARGETING, BIDDING_OPTIMIZATION , CREATIVE. |
is_most_recent_record | boolean | Whether record is the most recent one for this particular grain. |
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 development or testing purposes in a data modeling context.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
ad_id | integer | None |
updated_at | text | None |
advertiser_id | integer | None |
adgroup_id | integer | None |
campaign_id | integer | None |
create_time | text | None |
ad_name | text | None |
call_to_action | text | None |
secondary_status | text | None |
operation_status | text | None |
ad_text | text | None |
video_id | text | None |
app_name | integer | None |
deeplink | integer | None |
landing_page_url | text | None |
display_name | text | None |
profile_image_url | integer | None |
impression_tracking_url | integer | None |
click_tracking_url | integer | None |
playable_url | integer | None |
is_aco | boolean | None |
creative_authorized | boolean | None |
is_new_structure | boolean | None |
image_ids | text | None |
_fivetran_synced | text | None |
This SQL query stages data for a TikTok ads campaign report. It starts by selecting all columns from a temporary table, then defines a set of fields with specific data types and nulls or empty values. Finally, it selects and renames these fields, casting the stat_time_hour to a TIMESTAMP. The query appears to be setting up a structure for data that will be populated later, rather than performing operations on existing data.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_stg_tiktok_ads.stg_tiktok_ads__campaign_report_hourly_tmp
), fields AS (
SELECT
CAST(NULL AS DECIMAL(28, 6)) AS campaign_id,
CAST(NULL AS FLOAT) AS average_video_play,
CAST(NULL AS FLOAT) AS average_video_play_per_user,
CAST(NULL AS DECIMAL(28, 6)) AS clicks,
CAST(NULL AS DECIMAL(28, 6)) AS comments,
CAST(NULL AS DECIMAL(28, 6)) AS conversion,
CAST(NULL AS FLOAT) AS conversion_rate,
CAST(NULL AS FLOAT) AS cost_per_conversion,
CAST(NULL AS FLOAT) AS cpc,
CAST(NULL AS FLOAT) AS cpm,
CAST(NULL AS FLOAT) AS ctr,
CAST(NULL AS DECIMAL(28, 6)) AS follows,
CAST(NULL AS DECIMAL(28, 6)) AS impressions,
CAST(NULL AS DECIMAL(28, 6)) AS likes,
CAST(NULL AS DECIMAL(28, 6)) AS profile_visits,
CAST(NULL AS DECIMAL(28, 6)) AS reach,
CAST(NULL AS DECIMAL(28, 6)) AS shares,
CAST(NULL AS DECIMAL(28, 6)) AS spend,
CAST(NULL AS TIMESTAMP) AS stat_time_hour,
CAST(NULL AS DECIMAL(28, 6)) AS video_play_actions,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_25,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_50,
CAST(NULL AS DECIMAL(28, 6)) AS video_views_p_75,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_2_s,
CAST(NULL AS DECIMAL(28, 6)) AS video_watched_6_s,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
campaign_id,
CAST(stat_time_hour AS TIMESTAMP) AS stat_time_hour,
cpc,
cpm,
ctr,
impressions,
clicks,
spend,
reach,
conversion,
cost_per_conversion,
conversion_rate,
likes,
comments,
shares,
profile_visits,
follows,
video_play_actions,
video_watched_2_s,
video_watched_6_s,
video_views_p_25,
video_views_p_50,
video_views_p_75,
average_video_play,
average_video_play_per_user
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 | integer | Campaign id |
stat_time_hour | timestamp without time zone | Hour of activity |
cost_per_conversion | double precision | The average amount of money you've spent on a conversion. (The total count is calculated based on the time each ad impression occurred.) |
cpc | double precision | The average amount of money you've spent on a click. |
video_play_actions | integer | The number of times your video starts to play. Replays will not be counted. |
conversion_rate | integer | The percentage of results you received out of all the clicks of your ads. (The total count is calculated based on the time each ad impression occurred.) |
video_views_p_75 | integer | The number of times your video was played at 75% of its length. Replays will not be counted. |
video_views_p_50 | integer | The number of times your video was played at 50% of its length. Replays will not be counted. |
impressions | integer | The number of times your ads were on screen. |
comments | integer | The number of comments your video creative received within 1 day of a user seeing a paid ad. |
conversion | integer | The number of times your ad achieved an outcome, based on the secondary goal you selected. As one campaign may have a number of different secondary goals, this statistic is not supported for campaigns. Please go to ad groups or ads to view. (The total count is calculated based on the time each ad impression occurred.) |
shares | integer | The number of shares your video creative received within 1 day of a user seeing a paid ad. |
clicks | integer | The number of clicks on your ads. |
cost_per_1000_reached | None | The average cost to reach 1,000 unique users. This metric is estimated. |
video_views_p_25 | integer | The number of times your video was played at 25% of its length. Replays will not be counted. |
reach | integer | The number of unique users who saw your ads at least once. This metric is estimated. |
profile_visits_rate | None | The rate of profile visits per impression the paid ad drove during the campaign. This metric is only for Boosted TikToks. |
average_video_play | double precision | The average time your video was played per single video view, including any time spent replaying the video. |
average_video_play_per_user | double precision | The average time per user your video was played per single video view, including any time spent replaying the video. |
profile_visits | integer | The number of profile visits the ad drove during the campaign. This metric is only for Boosted TikToks. |
cpm | double precision | The average amount of money you've spent per 1,000 impressions. |
ctr | double precision | The percentage of times people saw your ad and performed a click. |
video_watched_2_s | integer | The number of times your video played for at least 2 seconds. Replays will not be counted. |
follows | integer | The number of new followers that were gained within 1 day of a user seeing a paid ad. This metric is only for Boosted TikToks. |
video_watched_6_s | integer | The number of times your video played for at least 6 seconds, or completely played. Replays will not be counted. |
spend | double precision | The estimated total amount of money you've spent on your campaign, ad group or ad during its schedule. |
likes | integer | The number of likes your video creative received within 1 day of a user seeing a paid ad. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for further development or testing purposes in the dbt (data build tool) framework, specifically for the TikTok ads campaign report hourly staging model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
campaign_id | integer | None |
stat_time_hour | timestamp without time zone | None |
cost_per_conversion | double precision | None |
real_time_conversion | integer | None |
cpc | double precision | None |
video_play_actions | integer | None |
conversion_rate | integer | None |
video_views_p_75 | integer | None |
result | integer | None |
video_views_p_50 | integer | None |
impressions | integer | None |
comments | integer | None |
real_time_cost_per_result | double precision | None |
conversion | integer | None |
real_time_result | integer | None |
video_views_p_100 | integer | None |
shares | integer | None |
real_time_conversion_rate | double precision | None |
cost_per_secondary_goal_result | text | None |
secondary_goal_result_rate | text | None |
clicks | integer | None |
cost_per_1000_reached | double precision | None |
video_views_p_25 | integer | None |
reach | integer | None |
real_time_cost_per_conversion | double precision | None |
profile_visits_rate | integer | None |
average_video_play | double precision | None |
profile_visits | integer | None |
cpm | double precision | None |
ctr | double precision | None |
video_watched_2_s | integer | None |
follows | integer | None |
result_rate | double precision | None |
video_watched_6_s | integer | None |
secondary_goal_result | text | None |
cost_per_result | double precision | None |
average_video_play_per_user | double precision | None |
real_time_result_rate | double precision | None |
spend | double precision | None |
likes | integer | None |
_fivetran_synced | text | None |
This SQL query combines data from Twitter ads account history and promoted tweet reports. It first filters the account history for the latest versions, then aggregates the promoted tweet report data by date, account, and placement. Finally, it joins this aggregated data with the account information, providing a comprehensive report of ad performance metrics (clicks, impressions, spend, etc.) along with account details.
FilteringIntegrationAggregationWITH accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), promoted_tweet_report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_report
), rollup_report AS (
SELECT
source_relation,
date_day,
account_id,
placement,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
SUM(spend) AS spend,
SUM(spend_micro) AS spend_micro,
SUM(url_clicks) AS url_clicks
FROM promoted_tweet_report
GROUP BY
1,
2,
3,
4
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
report.account_id,
accounts.name AS account_name,
accounts.is_deleted,
accounts.timezone,
accounts.industry_type,
accounts.approval_status,
accounts.business_name,
accounts.business_id,
accounts.created_timestamp,
accounts.updated_timestamp,
accounts.timezone_switched_timestamp,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM rollup_report AS 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,
10,
11,
12,
13,
14
)
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. |
spend_micro | numeric | The spend (in micros) for the account on that day. |
spend | numeric | The spend for the account on that day. |
clicks | numeric | The clicks for th account on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | numeric | The impressions for the account on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
url_clicks | numeric | The url clicks for the account on that day. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
approval_status | text | The approval status of the account. |
business_id | integer | The ID of the related business. |
business_name | integer | The name of the related business. |
created_timestamp | text | Time that the respective record (ad, ad group, campaign, post, etc) was created. ISO-8601 timestamp. |
is_deleted | boolean | Whether the record has been deleted or not. |
account_id | text | The ID of the account. |
account_name | character varying | Name of the account. |
industry_type | integer | The industry of the accounts. |
timezone | text | The timezone the account is set to. |
timezone_switched_timestamp | text | The timestamp the account's timezone was last changed. |
updated_timestamp | text | Timestamp of when the record was last updated in Google Ads. |
This SQL query combines data from three sources: campaign reports, campaign history, and account history. It joins these tables to create a comprehensive report that includes campaign performance metrics (clicks, impressions, spend) along with campaign and account details. The query filters for the latest versions of campaigns and accounts, and aggregates the report data by various dimensions such as date, placement, account, and campaign.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history
WHERE
is_latest_version
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
report.account_id,
accounts.name AS account_name,
report.campaign_id,
campaigns.campaign_name,
campaigns.is_deleted,
campaigns.entity_status AS campaign_status,
campaigns.currency,
campaigns.is_servable,
campaigns.is_standard_delivery,
campaigns.frequency_cap,
campaigns.start_timestamp,
campaigns.end_timestamp,
campaigns.created_timestamp,
campaigns.updated_timestamp,
campaigns.funding_instrument_id,
campaigns.daily_budget_amount,
campaigns.total_budget_amount,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM report
LEFT JOIN campaigns
ON report.campaign_id = campaigns.campaign_id
AND report.source_relation = campaigns.source_relation
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,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20
)
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 | text | The ID of the campaign. |
campaign_name | text | The name of the campaign. |
clicks | bigint | The number of clicks on that date. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | bigint | The number of impressions on that date. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
spend | numeric | The amount of spend on that date. |
spend_micro | bigint | The amount of spend, in micros, on that date. |
url_clicks | bigint | The number of URL clicks on that date. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
account_id | text | The ID of the related account. |
account_name | character varying | The name of the related account. |
created_timestamp | text | Time that the respective record (ad, ad group, campaign, post, etc) was created. ISO-8601 timestamp. |
currency | text | The currency all metrics for the account are set to. |
is_deleted | boolean | Whether the record has been deleted or not. |
end_timestamp | text | The time the campaign will end |
campaign_status | text | The status of the campaign. |
frequency_cap | integer | The maximum number of times an ad could be delivered to a user. |
is_servable | boolean | Whether the campaign is in a state to be actively served to users. |
is_standard_delivery | boolean | Whether standard delivery is enabled (vs accelerated delivery). |
start_timestamp | text | The time the campaign will start. |
updated_timestamp | text | Timestamp of when the record was last updated in Google Ads. |
funding_instrument_id | text | Reference to the funding instrument. |
daily_budget_amount | numeric | The daily budget amount to be allocated to the campaign. The currency associated with the specified funding instrument will be used. |
total_budget_amount | numeric | The total budget amount to be allocated to the campaign. |
This SQL query integrates data from multiple Twitter Ads-related tables to create a comprehensive keyword report. It joins the keyword report with line item, campaign, and account information, and then aggregates metrics such as clicks, impressions, spend, and URL clicks. The query filters for the latest versions of line items, campaigns, and accounts, and groups the results by various dimensions including date, account, campaign, line item, and keyword.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_keywords_report
), line_items AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_history
WHERE
is_latest_version
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history
WHERE
is_latest_version
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
report.account_id,
accounts.name AS account_name,
line_items.campaign_id,
campaigns.campaign_name,
report.line_item_id,
line_items.name AS line_item_name,
report.keyword_id,
report.keyword,
line_items.currency,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM report
LEFT JOIN line_items
ON report.line_item_id = line_items.line_item_id
AND report.source_relation = line_items.source_relation
LEFT JOIN campaigns
ON line_items.campaign_id = campaigns.campaign_id
AND line_items.source_relation = campaigns.source_relation
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,
10,
11,
12
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | None | The source of the record if the unioning functionality is being used. If not this field will be empty. |
account_id | None | The ID of the related account. |
spend_micro | None | The spend for the line item + keyword on that day, in micros and in whichever currency was selected during account creation. |
spend | None | The spend for the line item + keyword on that day in whichever currency was selected during account creation. |
clicks | None | The clicks for the line item + keyword on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | None | The date of the performance. |
impressions | None | The impressions for the line item + keyword on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
line_item_id | None | The ID of the related line item (ad group). |
url_clicks | None | The url clicks for the line item + keyword on that day. |
keyword | None | The keyword whose performance is being tracked. |
keyword_id | None | Unique key of the table built from the combination of 'account_id', 'line_item_id', 'segment', and 'placement' fields. |
placement | None | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
account_name | None | Name of the associated account. |
campaign_id | None | The ID of the related campaign. |
campaign_name | None | The name of the related campaign. |
line_item_name | None | The ID of the related line item. |
currency | None | The currency all metrics for the account are set to. |
This SQL query integrates data from multiple Twitter Ads-related tables (line item report, line items, campaigns, and accounts) to create a comprehensive report. It joins these tables based on common identifiers and source relations, selecting the latest versions of historical data where applicable. The query then aggregates metrics such as clicks, impressions, spend, and URL clicks while including various attributes from the joined tables. The result is a detailed view of Twitter Ads performance at the line item level, including associated campaign and account information.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_report
), line_items AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_history
WHERE
is_latest_version
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history
WHERE
is_latest_version
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
report.account_id,
accounts.name AS account_name,
line_items.campaign_id,
campaigns.campaign_name,
report.line_item_id,
line_items.name AS line_item_name,
line_items.is_deleted,
line_items.entity_status AS line_item_status,
campaigns.entity_status AS campaign_status,
line_items.currency,
line_items.advertiser_domain,
line_items.advertiser_user_id,
line_items.bid_type,
line_items.bid_unit,
line_items.charge_by,
line_items.objective,
line_items.optimization,
line_items.product_type,
line_items.primary_web_event_tag,
line_items.creative_source,
line_items.start_timestamp,
line_items.end_timestamp,
line_items.created_timestamp,
line_items.updated_timestamp,
line_items.target_cpa,
line_items.total_budget_amount,
line_items.bid_amount,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM report
LEFT JOIN line_items
ON report.line_item_id = line_items.line_item_id
AND report.source_relation = line_items.source_relation
LEFT JOIN campaigns
ON line_items.campaign_id = campaigns.campaign_id
AND line_items.source_relation = campaigns.source_relation
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,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30
)
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 | text | The ID of the line item's campaign. |
campaign_name | text | The name of the line item's campaign. |
clicks | bigint | The number of clicks on that date. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | bigint | The number of impressions on that date. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
line_item_id | text | The ID of the line item. |
line_item_name | text | The name of the line item. |
spend | numeric | The amount of spend on that date. |
spend_micro | bigint | The amount of spend, in micros, on that date. |
url_clicks | bigint | The number of URL clicks on that date. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
account_id | text | The ID of the related account. |
account_name | character varying | The name of the related account. |
advertiser_domain | text | The website domain for this advertiser, without the protocol specification. |
advertiser_user_id | integer | The Twitter user identifier for the handle promoting the ad. |
bid_type | text | The bidding mechanism. |
bid_unit | text | The bid unit for this line item. |
charge_by | text | The unit to charge this line item by. |
created_timestamp | text | Time that the respective record (ad, ad group, campaign, post, etc) was created. ISO-8601 timestamp. |
creative_source | text | The source of the creatives for the line item. |
currency | text | The currency in which metrics will be reported. |
is_deleted | boolean | Whether the record has been deleted or not. |
end_timestamp | integer | The timestamp at which the line item will stop being served. |
line_item_status | text | The status of the line item. |
campaign_status | text | The status of the line item's related campaign. |
objective | text | The campaign objective for this line item. |
optimization | text | The optimization setting to use with this line item. |
primary_web_event_tag | integer | The identifier of the primary web event tag. Allows more accurate tracking of engagements for the campaign pertaining to this line item. |
product_type | text | The type of promoted product that this line item will contain. |
start_timestamp | integer | The timestamp at which the line item will start being served. |
updated_timestamp | text | Timestamp of when the record was last updated in Google Ads. |
bid_amount | numeric | The bid amount to be associated with this line item. |
total_budget_amount | numeric | The total budget amount to be allocated to the campaign. |
target_cpa | numeric | The target cost per acquisition for the line item. |
This SQL query integrates data from multiple Twitter Ads-related tables to create a comprehensive report on promoted tweets. It joins information from accounts, campaigns, line items, promoted tweets, and tweets, filtering for the latest versions where applicable. The query then aggregates metrics such as clicks, impressions, spend, and URL clicks, grouping by various dimensions including date, placement, account, campaign, and tweet details.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history
WHERE
is_latest_version
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), line_items AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_history
WHERE
is_latest_version
), promoted_tweets AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_history
WHERE
is_latest_version
), tweets AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__tweet
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
accounts.account_id,
accounts.name AS account_name,
campaigns.campaign_id,
campaigns.campaign_name,
line_items.line_item_id,
line_items.name AS line_item_name,
promoted_tweets.promoted_tweet_id,
promoted_tweets.tweet_id,
tweets.name AS tweet_name,
tweets.full_text,
promoted_tweets.is_deleted,
promoted_tweets.entity_status AS promoted_tweet_status,
campaigns.entity_status AS campaign_status,
line_items.entity_status AS line_item_status,
tweets.language,
campaigns.currency,
promoted_tweets.approval_status,
promoted_tweets.created_timestamp,
promoted_tweets.updated_timestamp,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM report
LEFT JOIN promoted_tweets
ON report.promoted_tweet_id = promoted_tweets.promoted_tweet_id
AND report.source_relation = promoted_tweets.source_relation
LEFT JOIN tweets
ON promoted_tweets.tweet_id = tweets.tweet_id
AND promoted_tweets.source_relation = tweets.source_relation
LEFT JOIN line_items
ON promoted_tweets.line_item_id = line_items.line_item_id
AND promoted_tweets.source_relation = line_items.source_relation
LEFT JOIN campaigns
ON line_items.campaign_id = campaigns.campaign_id
AND line_items.source_relation = campaigns.source_relation
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,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22
)
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. |
date_day | timestamp without time zone | The date of the performance. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
account_id | text | The ID of the related account. |
account_name | character varying | The name of the related account. |
campaign_id | text | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
line_item_id | text | The ID of the related line item (ad group). |
line_item_name | text | The ID of the related line item. |
promoted_tweet_id | text | The ID of the promoted tweet that the URL appeared in. |
tweet_id | bigint | The ID of the tweet that the URL appeared in. |
tweet_name | integer | The name, if provided, of the tweet that the URL appeared in. |
full_text | text | The full text of the tweet that the URL appeared in. |
currency | text | The currency all metrics for the account are set to. |
clicks | bigint | The clicks for the promoted tweet + URL on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
impressions | bigint | The impressions for the promoted tweet + URL on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
spend | numeric | The spend for the promoted tweet + URL on that day. |
spend_micro | bigint | The spend, in micros, for the tweet + URL on that day. |
url_clicks | bigint | The URL clicks for the promoted tweet + URL on that day. |
approval_status | text | The approval status of the promoted tweet. |
created_timestamp | text | Time that the respective record (ad, ad group, campaign, post, etc) was created. ISO-8601 timestamp. |
is_deleted | boolean | Whether the record has been deleted or not. |
promoted_tweet_status | text | The status of the promoted tweet. |
campaign_status | text | The status of the tweet's campaign. |
line_item_status | text | The status of the tweet's line item. |
updated_timestamp | text | Timestamp of when the record was last updated in Google Ads. |
language | text | Two-letter language code of the tweet. |
This SQL query integrates data from multiple Twitter Ads-related tables to create a comprehensive URL report. It joins information from promoted tweets, campaigns, accounts, line items, and tweets, focusing on URL-related metrics. The query filters for the latest versions of historical data and non-null expanded URLs. It then aggregates metrics like clicks, impressions, spend, and URL clicks, grouping by various dimensions such as account, campaign, tweet, and URL attributes.
FilteringIntegrationAggregationWITH report AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_report
), campaigns AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history
WHERE
is_latest_version
), accounts AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history
WHERE
is_latest_version
), line_items AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_history
WHERE
is_latest_version
), promoted_tweets AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_history
WHERE
is_latest_version
), tweets AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__tweet
), tweet_url AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__tweet_url
WHERE
index = 0
), final AS (
SELECT
report.source_relation,
report.date_day,
report.placement,
accounts.account_id,
accounts.name AS account_name,
campaigns.campaign_id,
campaigns.campaign_name,
line_items.line_item_id,
line_items.name AS line_item_name,
promoted_tweets.promoted_tweet_id,
promoted_tweets.tweet_id,
tweets.name AS tweet_name,
tweets.full_text AS tweet_full_text,
tweet_url.base_url,
tweet_url.url_host,
tweet_url.url_path,
tweet_url.utm_source,
tweet_url.utm_medium,
tweet_url.utm_campaign,
tweet_url.utm_content,
tweet_url.utm_term,
tweet_url.expanded_url,
tweet_url.display_url,
campaigns.currency,
SUM(report.clicks) AS clicks,
SUM(report.impressions) AS impressions,
SUM(report.spend) AS spend,
SUM(report.spend_micro) AS spend_micro,
SUM(report.url_clicks) AS url_clicks
FROM report
LEFT JOIN promoted_tweets
ON report.promoted_tweet_id = promoted_tweets.promoted_tweet_id
AND report.source_relation = promoted_tweets.source_relation
LEFT JOIN tweet_url
ON promoted_tweets.tweet_id = tweet_url.tweet_id
AND promoted_tweets.source_relation = tweet_url.source_relation
LEFT JOIN tweets
ON promoted_tweets.tweet_id = tweets.tweet_id
AND promoted_tweets.source_relation = tweets.source_relation
LEFT JOIN line_items
ON promoted_tweets.line_item_id = line_items.line_item_id
AND promoted_tweets.source_relation = line_items.source_relation
LEFT JOIN campaigns
ON line_items.campaign_id = campaigns.campaign_id
AND line_items.source_relation = campaigns.source_relation
LEFT JOIN accounts
ON report.account_id = accounts.account_id
AND report.source_relation = accounts.source_relation
WHERE
NOT tweet_url.expanded_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,
21,
22,
23,
24
)
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. |
date_day | timestamp without time zone | The date of the performance. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
account_id | text | The ID of the related account. |
account_name | character varying | The name of the related account. |
campaign_id | text | The ID of the related campaign. |
campaign_name | text | The name of the related campaign. |
line_item_id | text | The ID of the related line item (ad group). |
line_item_name | text | The ID of the related line item. |
promoted_tweet_id | text | The ID of the promoted tweet that the URL appeared in. |
tweet_id | bigint | The ID of the tweet that the URL appeared in. |
tweet_name | integer | The name, if provided, of the tweet that the URL appeared in. |
tweet_full_text | text | The full text of the tweet that the URL appeared in. |
base_url | text | The base URL of the ad, extracted from the `expanded_url`. |
url_host | text | The URL host of the ad, extracted from the `expanded_url`. |
url_path | text | The URL path of the ad, extracted from the `expanded_url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `expanded_url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `expanded_url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `expanded_url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `expanded_url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `expanded_url`. |
display_url | text | The URL as it will be displayed. |
expanded_url | text | The fully expanded URL. |
currency | text | The currency all metrics for the account are set to. |
clicks | bigint | The clicks for the promoted tweet + URL on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
impressions | bigint | The impressions for the promoted tweet + URL on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
spend | numeric | The spend for the promoted tweet + URL on that day. |
spend_micro | bigint | The spend, in micros, for the tweet + URL on that day. |
url_clicks | bigint | The URL clicks for the promoted tweet + URL on that day. |
This SQL query performs a series of transformations on data from a Twitter Ads account history table. It casts various fields to specific data types, renames some columns, and adds a flag to identify the latest version of each account record. The query also includes a 'source_relation' field, though it's set to an empty string in this case.
CleaningDeduplicationOtherWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__account_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS approval_status,
CAST(NULL AS TEXT) AS business_id,
CAST(NULL AS TEXT) AS business_name,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS BOOLEAN) AS deleted,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS industry_type,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS salt,
CAST(NULL AS TEXT) AS timezone,
CAST(NULL AS TIMESTAMP) AS timezone_switch_at,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM source
), final AS (
SELECT
source_relation,
approval_status,
business_id,
business_name,
created_at AS created_timestamp,
deleted AS is_deleted,
id AS account_id,
industry_type,
name,
salt,
timezone,
timezone_switch_at AS timezone_switched_timestamp,
updated_at AS updated_timestamp,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_latest_version
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. |
approval_status | text | The approval status of the account. |
business_id | integer | The ID of the related business. |
business_name | integer | The name of the related business. |
created_timestamp | text | The timestamp the account was created. |
is_deleted | boolean | Whether the record has been deleted or not. |
account_id | text | The ID of the account. |
industry_type | integer | The industry of the accounts. |
name | character varying | The name of the account. |
timezone | text | The timezone the account is set to. |
timezone_switched_timestamp | text | The timestamp the account's timezone was last changed. |
updated_timestamp | text | The timestamp the account was last updated. |
is_latest_version | boolean | Whether the record is the most latest version for the account |
salt | integer | The random encryption key used to hash data. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT. The query doesn't select any actual data; it's likely used as a template or placeholder in a dbt (data build tool) model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_synced | text | None |
approval_status | text | None |
business_id | integer | None |
business_name | integer | None |
created_at | text | None |
deleted | boolean | None |
id | text | None |
industry_type | integer | None |
name | character varying | None |
salt | integer | None |
timezone | text | None |
timezone_switch_at | text | None |
updated_at | text | None |
This SQL query processes data from a Twitter Ads campaign history staging table. It casts and renames fields, calculates budget amounts in standard currency units, adds a flag for the latest version of each campaign, and selects all resulting columns. The query performs data type conversions, column renaming, and adds derived columns for analysis purposes.
CleaningFeaturizationDeduplicationWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS INT) AS daily_budget_amount_local_micro,
CAST(NULL AS BOOLEAN) AS deleted,
CAST(NULL AS INT) AS duration_in_days,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS TEXT) AS entity_status,
CAST(NULL AS INT) AS frequency_cap,
CAST(NULL AS TEXT) AS funding_instrument_id,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS BOOLEAN) AS servable,
CAST(NULL AS BOOLEAN) AS standard_delivery,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS INT) AS total_budget_amount_local_micro,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM source
), final AS (
SELECT
source_relation,
account_id,
created_at AS created_timestamp,
currency,
daily_budget_amount_local_micro,
deleted AS is_deleted,
duration_in_days,
end_time AS end_timestamp,
entity_status,
frequency_cap,
funding_instrument_id,
id AS campaign_id,
name AS campaign_name,
servable AS is_servable,
standard_delivery AS is_standard_delivery,
start_time AS start_timestamp,
total_budget_amount_local_micro,
updated_at AS updated_timestamp,
ROUND(daily_budget_amount_local_micro / 1000000.0, 2) AS daily_budget_amount,
ROUND(total_budget_amount_local_micro / 1000000.0, 2) AS total_budget_amount,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_latest_version
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 | text | The ID of the related account. |
created_timestamp | text | The timestamp the account was created. |
currency | text | The currency all metrics for the account are set to. |
daily_budget_amount_local_micro | integer | The daily budget amount to be allocated to the campaign, in micros. The currency associated with the specified funding instrument will be used. |
is_deleted | boolean | Whether the record has been deleted or not. |
duration_in_days | integer | The time period within which the frequency_cap is achieved. |
end_timestamp | text | The time the campaign will end |
entity_status | text | The status of the campaign. |
frequency_cap | integer | The maximum number of times an ad could be delivered to a user. |
campaign_id | text | The ID of the campaign. |
campaign_name | text | The name of the campaign. |
is_servable | boolean | Whether the campaign is in a state to be actively served to users. |
is_standard_delivery | boolean | Whether standard delivery is enabled (vs accelerated delivery). |
start_timestamp | text | The time the campaign will start. |
total_budget_amount_local_micro | integer | The total budget amount to be allocated to the campaign, in micros. |
updated_timestamp | text | The timestamp the account was last updated. |
is_latest_version | boolean | Whether the record is the most latest version for the campaign |
funding_instrument_id | text | Reference to the funding instrument. |
daily_budget_amount | numeric | The daily budget amount to be allocated to the campaign. The currency associated with the specified funding instrument will be used. |
total_budget_amount | numeric | The total budget amount to be allocated to the campaign. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of TEXT data type. It's likely used as a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Twitter Ads campaign history data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_synced | text | None |
account_id | text | None |
created_at | text | None |
currency | text | None |
daily_budget_amount_local_micro | integer | None |
deleted | boolean | None |
duration_in_days | integer | None |
end_time | text | None |
entity_status | text | None |
frequency_cap | integer | None |
funding_instrument_id | text | None |
id | text | None |
name | text | None |
servable | boolean | None |
standard_delivery | boolean | None |
start_time | text | None |
total_budget_amount_local_micro | integer | None |
updated_at | text | None |
This SQL query stages data from a temporary table for Twitter ads campaign reports. It casts columns to specific data types, renames some columns, and performs minor transformations such as truncating the date to day level and converting the spend from micro units to standard currency units. The query doesn't filter or aggregate data, but prepares it for further analysis.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__campaign_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS INT) AS billed_charge_local_micro,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS placement,
CAST(NULL AS INT) AS url_clicks,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
account_id,
campaign_id,
placement,
clicks,
impressions,
billed_charge_local_micro AS spend_micro,
ROUND(billed_charge_local_micro / 1000000.0, 2) AS spend,
url_clicks
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 | text | The ID of the related account. |
spend_micro | integer | The spend for the campaign on that day, in micros and in whichever currency was selected during account creation. |
spend | numeric | The spend for the campaign on that day in whichever currency was selected during account creation. |
clicks | integer | The clicks for the campaign on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | integer | The impressions for the campaign on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
campaign_id | text | The ID of the campaign. |
url_clicks | integer | The url clicks for the campaign on that day. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
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 a staging table in a dbt (data build tool) project, specifically for Twitter ads campaign reporting.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | text | None |
campaign_id | text | None |
date | timestamp without time zone | None |
placement | text | None |
_fivetran_synced | text | None |
app_clicks | integer | None |
billed_charge_local_micro | integer | None |
billed_engagements | integer | None |
card_engagements | integer | None |
carousel_swipes | integer | None |
clicks | integer | None |
conversion_custom_metric | integer | None |
conversion_custom_order_quantity | integer | None |
conversion_custom_order_quantity_engagement | integer | None |
conversion_custom_order_quantity_view | integer | None |
conversion_custom_post_engagement | integer | None |
conversion_custom_post_view | integer | None |
conversion_custom_sale_amount | integer | None |
conversion_custom_sale_amount_engagement | integer | None |
conversion_custom_sale_amount_view | integer | None |
conversion_downloads_metric | integer | None |
conversion_downloads_order_quantity | integer | None |
conversion_downloads_order_quantity_engagement | integer | None |
conversion_downloads_order_quantity_view | integer | None |
conversion_downloads_post_engagement | integer | None |
conversion_downloads_post_view | integer | None |
conversion_downloads_sale_amount | integer | None |
conversion_downloads_sale_amount_engagement | integer | None |
conversion_downloads_sale_amount_view | integer | None |
conversion_purchases_assisted | integer | None |
conversion_purchases_metric | integer | None |
conversion_purchases_order_quantity | integer | None |
conversion_purchases_order_quantity_engagement | integer | None |
conversion_purchases_order_quantity_view | integer | None |
conversion_purchases_post_engagement | integer | None |
conversion_purchases_post_view | integer | None |
conversion_purchases_sale_amount | integer | None |
conversion_purchases_sale_amount_engagement | integer | None |
conversion_purchases_sale_amount_view | integer | None |
conversion_sign_ups_assisted | integer | None |
conversion_sign_ups_metric | integer | None |
conversion_sign_ups_order_quantity | integer | None |
conversion_sign_ups_order_quantity_engagement | integer | None |
conversion_sign_ups_order_quantity_view | integer | None |
conversion_sign_ups_post_engagement | integer | None |
conversion_sign_ups_post_view | integer | None |
conversion_sign_ups_sale_amount | integer | None |
conversion_sign_ups_sale_amount_engagement | integer | None |
conversion_sign_ups_sale_amount_view | integer | None |
conversion_site_visits_metric | integer | None |
conversion_site_visits_order_quantity | integer | None |
conversion_site_visits_order_quantity_engagement | integer | None |
conversion_site_visits_order_quantity_view | integer | None |
conversion_site_visits_post_engagement | integer | None |
conversion_site_visits_post_view | integer | None |
conversion_site_visits_sale_amount | integer | None |
conversion_site_visits_sale_amount_engagement | integer | None |
conversion_site_visits_sale_amount_view | integer | None |
engagements | integer | None |
follows | integer | None |
impressions | integer | None |
likes | integer | None |
media_engagements | integer | None |
media_views | integer | None |
mobile_conversion_achievements_unlocked_assisted | integer | None |
mobile_conversion_achievements_unlocked_order_quantity | integer | None |
mobile_conversion_achievements_unlocked_post_engagement | integer | None |
mobile_conversion_achievements_unlocked_post_view | integer | None |
mobile_conversion_achievements_unlocked_sale_amount | integer | None |
mobile_conversion_add_to_carts_assisted | integer | None |
mobile_conversion_add_to_carts_order_quantity | integer | None |
mobile_conversion_add_to_carts_post_engagement | integer | None |
mobile_conversion_add_to_carts_post_view | integer | None |
mobile_conversion_add_to_carts_sale_amount | integer | None |
mobile_conversion_add_to_wishlists_assisted | integer | None |
mobile_conversion_add_to_wishlists_order_quantity | integer | None |
mobile_conversion_add_to_wishlists_post_engagement | integer | None |
mobile_conversion_add_to_wishlists_post_view | integer | None |
mobile_conversion_add_to_wishlists_sale_amount | integer | None |
mobile_conversion_checkouts_initiated_assisted | integer | None |
mobile_conversion_checkouts_initiated_order_quantity | integer | None |
mobile_conversion_checkouts_initiated_post_engagement | integer | None |
mobile_conversion_checkouts_initiated_post_view | integer | None |
mobile_conversion_checkouts_initiated_sale_amount | integer | None |
mobile_conversion_content_views_assisted | integer | None |
mobile_conversion_content_views_order_quantity | integer | None |
mobile_conversion_content_views_post_engagement | integer | None |
mobile_conversion_content_views_post_view | integer | None |
mobile_conversion_content_views_sale_amount | integer | None |
mobile_conversion_downloads_order_quantity | integer | None |
mobile_conversion_downloads_post_engagement | integer | None |
mobile_conversion_downloads_post_view | integer | None |
mobile_conversion_downloads_sale_amount | integer | None |
mobile_conversion_installs_assisted | integer | None |
mobile_conversion_installs_order_quantity | integer | None |
mobile_conversion_installs_post_engagement | integer | None |
mobile_conversion_installs_post_view | integer | None |
mobile_conversion_installs_sale_amount | integer | None |
mobile_conversion_invites_assisted | integer | None |
mobile_conversion_invites_order_quantity | integer | None |
mobile_conversion_invites_post_engagement | integer | None |
mobile_conversion_invites_post_view | integer | None |
mobile_conversion_invites_sale_amount | integer | None |
mobile_conversion_key_page_views_post_engagement | integer | None |
mobile_conversion_key_page_views_post_view | integer | None |
mobile_conversion_levels_achieved_assisted | integer | None |
mobile_conversion_levels_achieved_order_quantity | integer | None |
mobile_conversion_levels_achieved_post_engagement | integer | None |
mobile_conversion_levels_achieved_post_view | integer | None |
mobile_conversion_levels_achieved_sale_amount | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_metric | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_order_qu | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_sale_amo | integer | None |
mobile_conversion_lifetime_value_add_to_carts_metric | integer | None |
mobile_conversion_lifetime_value_add_to_carts_order_quantity | integer | None |
mobile_conversion_lifetime_value_add_to_carts_sale_amount | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_metric | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_order_quantit | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_sale_amount | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_metric | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_order_quan | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_sale_amoun | integer | None |
mobile_conversion_lifetime_value_content_views_metric | integer | None |
mobile_conversion_lifetime_value_content_views_order_quantity | integer | None |
mobile_conversion_lifetime_value_content_views_sale_amount | integer | None |
mobile_conversion_lifetime_value_invites_metric | integer | None |
mobile_conversion_lifetime_value_invites_order_quantity | integer | None |
mobile_conversion_lifetime_value_invites_sale_amount | integer | None |
mobile_conversion_lifetime_value_levels_achieved_metric | integer | None |
mobile_conversion_lifetime_value_levels_achieved_order_quantity | integer | None |
mobile_conversion_lifetime_value_levels_achieved_sale_amount | integer | None |
mobile_conversion_lifetime_value_logins_metric | integer | None |
mobile_conversion_lifetime_value_logins_order_quantity | integer | None |
mobile_conversion_lifetime_value_logins_sale_amount | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_metric | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_order_q | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_sale_am | integer | None |
mobile_conversion_lifetime_value_purchases_metric | integer | None |
mobile_conversion_lifetime_value_purchases_order_quantity | integer | None |
mobile_conversion_lifetime_value_purchases_sale_amount | integer | None |
mobile_conversion_lifetime_value_rates_metric | integer | None |
mobile_conversion_lifetime_value_rates_order_quantity | integer | None |
mobile_conversion_lifetime_value_rates_sale_amount | integer | None |
mobile_conversion_lifetime_value_reservations_metric | integer | None |
mobile_conversion_lifetime_value_reservations_order_quantity | integer | None |
mobile_conversion_lifetime_value_reservations_sale_amount | integer | None |
mobile_conversion_lifetime_value_searches_metric | integer | None |
mobile_conversion_lifetime_value_searches_order_quantity | integer | None |
mobile_conversion_lifetime_value_searches_sale_amount | integer | None |
mobile_conversion_lifetime_value_shares_metric | integer | None |
mobile_conversion_lifetime_value_shares_order_quantity | integer | None |
mobile_conversion_lifetime_value_shares_sale_amount | integer | None |
mobile_conversion_lifetime_value_sign_ups_metric | integer | None |
mobile_conversion_lifetime_value_sign_ups_order_quantity | integer | None |
mobile_conversion_lifetime_value_sign_ups_sale_amount | integer | None |
mobile_conversion_lifetime_value_spent_credits_metric | integer | None |
mobile_conversion_lifetime_value_spent_credits_order_quantity | integer | None |
mobile_conversion_lifetime_value_spent_credits_sale_amount | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_metric | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_order_quan | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_sale_amoun | integer | None |
mobile_conversion_lifetime_value_updates_metric | integer | None |
mobile_conversion_lifetime_value_updates_order_quantity | integer | None |
mobile_conversion_lifetime_value_updates_sale_amount | integer | None |
mobile_conversion_logins_assisted | integer | None |
mobile_conversion_logins_order_quantity | integer | None |
mobile_conversion_logins_post_engagement | integer | None |
mobile_conversion_logins_post_view | integer | None |
mobile_conversion_logins_sale_amount | integer | None |
mobile_conversion_payment_info_additions_assisted | integer | None |
mobile_conversion_payment_info_additions_order_quantity | integer | None |
mobile_conversion_payment_info_additions_post_engagement | integer | None |
mobile_conversion_payment_info_additions_post_view | integer | None |
mobile_conversion_payment_info_additions_sale_amount | integer | None |
mobile_conversion_purchases_assisted | integer | None |
mobile_conversion_purchases_order_quantity | integer | None |
mobile_conversion_purchases_post_engagement | integer | None |
mobile_conversion_purchases_post_view | integer | None |
mobile_conversion_purchases_sale_amount | integer | None |
mobile_conversion_rates_assisted | integer | None |
mobile_conversion_rates_order_quantity | integer | None |
mobile_conversion_rates_post_engagement | integer | None |
mobile_conversion_rates_post_view | integer | None |
mobile_conversion_rates_sale_amount | integer | None |
mobile_conversion_re_engages_assisted | integer | None |
mobile_conversion_re_engages_order_quantity | integer | None |
mobile_conversion_re_engages_post_engagement | integer | None |
mobile_conversion_re_engages_post_view | integer | None |
mobile_conversion_re_engages_sale_amount | integer | None |
mobile_conversion_reservations_assisted | integer | None |
mobile_conversion_reservations_order_quantity | integer | None |
mobile_conversion_reservations_post_engagement | integer | None |
mobile_conversion_reservations_post_view | integer | None |
mobile_conversion_reservations_sale_amount | integer | None |
mobile_conversion_searches_assisted | integer | None |
mobile_conversion_searches_order_quantity | integer | None |
mobile_conversion_searches_post_engagement | integer | None |
mobile_conversion_searches_post_view | integer | None |
mobile_conversion_searches_sale_amount | integer | None |
mobile_conversion_shares_assisted | integer | None |
mobile_conversion_shares_order_quantity | integer | None |
mobile_conversion_shares_post_engagement | integer | None |
mobile_conversion_shares_post_view | integer | None |
mobile_conversion_shares_sale_amount | integer | None |
mobile_conversion_sign_ups_assisted | integer | None |
mobile_conversion_sign_ups_order_quantity | integer | None |
mobile_conversion_sign_ups_post_engagement | integer | None |
mobile_conversion_sign_ups_post_view | integer | None |
mobile_conversion_sign_ups_sale_amount | integer | None |
mobile_conversion_site_visits_order_quantity | integer | None |
mobile_conversion_site_visits_post_engagement | integer | None |
mobile_conversion_site_visits_post_view | integer | None |
mobile_conversion_site_visits_sale_amount | integer | None |
mobile_conversion_spent_credits_assisted | integer | None |
mobile_conversion_spent_credits_order_quantity | integer | None |
mobile_conversion_spent_credits_post_engagement | integer | None |
mobile_conversion_spent_credits_post_view | integer | None |
mobile_conversion_spent_credits_sale_amount | integer | None |
mobile_conversion_tutorials_completed_assisted | integer | None |
mobile_conversion_tutorials_completed_order_quantity | integer | None |
mobile_conversion_tutorials_completed_post_engagement | integer | None |
mobile_conversion_tutorials_completed_post_view | integer | None |
mobile_conversion_tutorials_completed_sale_amount | integer | None |
mobile_conversion_updates_assisted | integer | None |
mobile_conversion_updates_order_quantity | integer | None |
mobile_conversion_updates_post_engagement | integer | None |
mobile_conversion_updates_post_view | integer | None |
mobile_conversion_updates_sale_amount | integer | None |
poll_card_vote | integer | None |
qualified_impressions | integer | None |
replies | integer | None |
retweets | integer | None |
tweets_send | integer | None |
unfollows | integer | None |
url_clicks | integer | None |
video_3_s_100_pct_views | integer | None |
video_6_s_views | integer | None |
video_content_starts | integer | None |
video_cta_clicks | integer | None |
video_mrc_views | integer | None |
video_total_views | integer | None |
video_views_100 | integer | None |
video_views_25 | integer | None |
video_views_50 | integer | None |
video_views_75 | integer | None |
mobile_conversion_installs_skan_post_engagement | integer | None |
mobile_conversion_installs_skan_post_view | integer | None |
mobile_conversion_purchases_skan_post_engagement | integer | None |
mobile_conversion_purchases_skan_post_view | integer | None |
video_15_s_views | integer | None |
auto_created_conversion_landing_page_view | integer | None |
auto_created_conversion_session | integer | None |
This SQL query stages data from a Twitter Ads source table, casting various fields to specific data types, renaming some columns, and performing light transformations. It also adds a flag to identify the latest version of each line item and calculates monetary amounts from micro units. The query prepares the data for further analysis or integration into a larger data model.
CleaningDeduplicationFeaturizationWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS advertiser_domain,
CAST(NULL AS INT) AS advertiser_user_id,
CAST(NULL AS BOOLEAN) AS automatically_select_bid,
CAST(NULL AS INT) AS bid_amount_local_micro,
CAST(NULL AS TEXT) AS bid_type,
CAST(NULL AS TEXT) AS bid_unit,
CAST(NULL AS TEXT) AS campaign_id,
CAST(NULL AS TEXT) AS charge_by,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS TEXT) AS creative_source,
CAST(NULL AS TEXT) AS currency,
CAST(NULL AS BOOLEAN) AS deleted,
CAST(NULL AS TIMESTAMP) AS end_time,
CAST(NULL AS TEXT) AS entity_status,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS name,
CAST(NULL AS TEXT) AS objective,
CAST(NULL AS TEXT) AS optimization,
CAST(NULL AS TEXT) AS primary_web_event_tag,
CAST(NULL AS TEXT) AS product_type,
CAST(NULL AS TIMESTAMP) AS start_time,
CAST(NULL AS INT) AS target_cpa_local_micro,
CAST(NULL AS INT) AS total_budget_amount_local_micro,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM source
), final AS (
SELECT
source_relation,
advertiser_domain,
advertiser_user_id,
automatically_select_bid,
bid_amount_local_micro,
bid_type,
bid_unit,
campaign_id,
charge_by,
created_at AS created_timestamp,
creative_source,
currency,
deleted AS is_deleted,
end_time AS end_timestamp,
entity_status,
id AS line_item_id,
name,
objective,
optimization,
primary_web_event_tag,
product_type,
start_time AS start_timestamp,
target_cpa_local_micro,
total_budget_amount_local_micro,
updated_at AS updated_timestamp,
ROUND(bid_amount_local_micro / 1000000.0, 2) AS bid_amount,
ROUND(total_budget_amount_local_micro / 1000000.0, 2) AS total_budget_amount,
ROUND(target_cpa_local_micro / 1000000.0, 2) AS target_cpa,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_latest_version
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. |
advertiser_domain | text | The website domain for this advertiser, without the protocol specification. |
advertiser_user_id | integer | The Twitter user identifier for the handle promoting the ad. |
automatically_select_bid | boolean | Whether automatically optimize bidding is enabled based on daily budget and campaign flight dates. |
bid_amount_local_micro | integer | The bid amount to be associated with this line item, in micros. |
bid_type | text | The bidding mechanism. |
bid_unit | text | The bid unit for this line item. |
campaign_id | text | The ID of the related campaign. |
charge_by | text | The unit to charge this line item by. |
created_timestamp | text | The timestamp the account was created. |
creative_source | text | The source of the creatives for the line item. |
currency | text | The currency in which metrics will be reported. |
is_deleted | boolean | Whether the record has been deleted or not. |
end_timestamp | integer | The timestamp at which the line item will stop being served. |
entity_status | text | The status of the line item. |
line_item_id | text | The ID of the line item. |
name | text | The name of the line item. |
objective | text | The campaign objective for this line item. |
optimization | text | The optimization setting to use with this line item. |
primary_web_event_tag | integer | The identifier of the primary web event tag. Allows more accurate tracking of engagements for the campaign pertaining to this line item. |
product_type | text | The type of promoted product that this line item will contain. |
start_timestamp | integer | The timestamp at which the line item will start being served. |
target_cpa_local_micro | integer | The target cost per acquisition for the line item, in micros. |
total_budget_amount_local_micro | integer | The total budget amount to be allocated to the line item, in micros. |
updated_timestamp | text | The timestamp the account was last updated. |
is_latest_version | boolean | Whether the record is the most latest version for the line item. |
bid_amount | numeric | The bid amount to be associated with this line item. |
total_budget_amount | numeric | The total budget amount to be allocated to the campaign. |
target_cpa | numeric | The target cost per acquisition for the line item. |
This SQL query creates an empty result set with a single column named '_dbt_source_relation' of type TEXT, initialized to NULL. The LIMIT 0 clause ensures no rows are returned. This is likely used as a template or placeholder for a staging table in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
advertiser_domain | text | None |
advertiser_user_id | integer | None |
automatically_select_bid | boolean | None |
bid_amount_local_micro | integer | None |
bid_type | text | None |
bid_unit | text | None |
campaign_id | text | None |
charge_by | text | None |
created_at | text | None |
creative_source | text | None |
currency | text | None |
deleted | boolean | None |
end_time | integer | None |
entity_status | text | None |
id | text | None |
name | text | None |
objective | text | None |
optimization | text | None |
primary_web_event_tag | integer | None |
product_type | text | None |
start_time | integer | None |
target_cpa_local_micro | integer | None |
total_budget_amount_local_micro | integer | None |
updated_at | text | None |
This SQL query transforms and structures data from a Twitter Ads line item keywords report. It casts fields to specific data types, creates a date_day column by truncating the date to day level, generates a unique keyword_id using MD5 hashing of multiple fields, and calculates spend from billed_charge_local_micro. The query also renames some columns and selects specific fields for the final output.
CleaningFeaturizationOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_keywords_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS INT) AS billed_charge_local_micro,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS line_item_id,
CAST(NULL AS TEXT) AS placement,
CAST(NULL AS TEXT) AS segment,
CAST(NULL AS INT) AS url_clicks,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
MD5(
CAST(COALESCE(CAST(account_id AS TEXT), '_dbt_utils_surrogate_key_null_') || '-' || COALESCE(CAST(line_item_id AS TEXT), '_dbt_utils_surrogate_key_null_') || '-' || COALESCE(CAST(segment AS TEXT), '_dbt_utils_surrogate_key_null_') || '-' || COALESCE(CAST(placement AS TEXT), '_dbt_utils_surrogate_key_null_') AS TEXT)
) AS keyword_id,
account_id,
line_item_id,
segment AS keyword,
placement,
clicks,
impressions,
billed_charge_local_micro AS spend_micro,
ROUND(billed_charge_local_micro / 1000000.0, 2) AS spend,
url_clicks
FROM fields
)
SELECT
*
FROM final
Name | Type | Comment |
---|---|---|
source_relation | None | The source of the record if the unioning functionality is being used. If not this field will be empty. |
keyword_id | None | Unique key of the table built from the combination of 'account_id', 'line_item_id','segment', and 'placement' fields. |
account_id | None | The ID of the related account. |
spend_micro | None | The spend for the line item + keyword on that day, in micros and in whichever currency was selected during account creation. |
spend | None | The spend for the line item + keyword on that day in whichever currency was selected during account creation. |
clicks | None | The clicks for the line item + keyword on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | None | The date of the performance. |
impressions | None | The impressions for the line item + keyword on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
line_item_id | None | The ID of the line item. |
url_clicks | None | The url clicks for the line item + keyword on that day. |
keyword | None | The keyword whose performance is being tracked. |
placement | None | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
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 LIMIT 0 clause ensures no rows are returned. This query appears to be a placeholder or template for a staging table in a dbt (data build tool) project, specifically for Twitter ads line item keywords report data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|
This SQL query transforms data from a temporary staging table for Twitter Ads line item reports. It casts columns to specific data types, renames some columns, and performs minor calculations. The query also adds a source_relation column and truncates the date to day-level granularity. No filtering, deduplication, or aggregation is performed.
CleaningFeaturizationWITH base AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__line_item_report_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS INT) AS billed_charge_local_micro,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS line_item_id,
CAST(NULL AS TEXT) AS placement,
CAST(NULL AS INT) AS url_clicks,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
account_id,
line_item_id,
placement,
clicks,
impressions,
billed_charge_local_micro AS spend_micro,
ROUND(billed_charge_local_micro / 1000000.0, 2) AS spend,
url_clicks
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 | text | The ID of the related account. |
spend_micro | integer | The spend for the line item on that day, in micros and in whichever currency was selected during account creation. |
spend | numeric | The spend for the line item on that day in whichever currency was selected during account creation. |
clicks | integer | The clicks for the line item on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | integer | The impressions for the line item on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
line_item_id | text | The ID of the line item. |
url_clicks | integer | The url clicks for the line item on that day. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
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 LIMIT 0 clause ensures no rows are returned. This appears to be a template or placeholder query, possibly used for schema definition or testing purposes in a dbt (data build tool) project.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
account_id | text | None |
date | timestamp without time zone | None |
line_item_id | text | None |
placement | text | None |
_fivetran_synced | text | None |
app_clicks | integer | None |
billed_charge_local_micro | integer | None |
billed_engagements | integer | None |
card_engagements | integer | None |
carousel_swipes | integer | None |
clicks | integer | None |
conversion_custom_metric | integer | None |
conversion_custom_order_quantity | integer | None |
conversion_custom_order_quantity_engagement | integer | None |
conversion_custom_order_quantity_view | integer | None |
conversion_custom_post_engagement | integer | None |
conversion_custom_post_view | integer | None |
conversion_custom_sale_amount | integer | None |
conversion_custom_sale_amount_engagement | integer | None |
conversion_custom_sale_amount_view | integer | None |
conversion_downloads_metric | integer | None |
conversion_downloads_order_quantity | integer | None |
conversion_downloads_order_quantity_engagement | integer | None |
conversion_downloads_order_quantity_view | integer | None |
conversion_downloads_post_engagement | integer | None |
conversion_downloads_post_view | integer | None |
conversion_downloads_sale_amount | integer | None |
conversion_downloads_sale_amount_engagement | integer | None |
conversion_downloads_sale_amount_view | integer | None |
conversion_purchases_assisted | integer | None |
conversion_purchases_metric | integer | None |
conversion_purchases_order_quantity | integer | None |
conversion_purchases_order_quantity_engagement | integer | None |
conversion_purchases_order_quantity_view | integer | None |
conversion_purchases_post_engagement | integer | None |
conversion_purchases_post_view | integer | None |
conversion_purchases_sale_amount | integer | None |
conversion_purchases_sale_amount_engagement | integer | None |
conversion_purchases_sale_amount_view | integer | None |
conversion_sign_ups_assisted | integer | None |
conversion_sign_ups_metric | integer | None |
conversion_sign_ups_order_quantity | integer | None |
conversion_sign_ups_order_quantity_engagement | integer | None |
conversion_sign_ups_order_quantity_view | integer | None |
conversion_sign_ups_post_engagement | integer | None |
conversion_sign_ups_post_view | integer | None |
conversion_sign_ups_sale_amount | integer | None |
conversion_sign_ups_sale_amount_engagement | integer | None |
conversion_sign_ups_sale_amount_view | integer | None |
conversion_site_visits_metric | integer | None |
conversion_site_visits_order_quantity | integer | None |
conversion_site_visits_order_quantity_engagement | integer | None |
conversion_site_visits_order_quantity_view | integer | None |
conversion_site_visits_post_engagement | integer | None |
conversion_site_visits_post_view | integer | None |
conversion_site_visits_sale_amount | integer | None |
conversion_site_visits_sale_amount_engagement | integer | None |
conversion_site_visits_sale_amount_view | integer | None |
engagements | integer | None |
follows | integer | None |
impressions | integer | None |
likes | integer | None |
media_engagements | integer | None |
media_views | integer | None |
mobile_conversion_achievements_unlocked_assisted | integer | None |
mobile_conversion_achievements_unlocked_order_quantity | integer | None |
mobile_conversion_achievements_unlocked_post_engagement | integer | None |
mobile_conversion_achievements_unlocked_post_view | integer | None |
mobile_conversion_achievements_unlocked_sale_amount | integer | None |
mobile_conversion_add_to_carts_assisted | integer | None |
mobile_conversion_add_to_carts_order_quantity | integer | None |
mobile_conversion_add_to_carts_post_engagement | integer | None |
mobile_conversion_add_to_carts_post_view | integer | None |
mobile_conversion_add_to_carts_sale_amount | integer | None |
mobile_conversion_add_to_wishlists_assisted | integer | None |
mobile_conversion_add_to_wishlists_order_quantity | integer | None |
mobile_conversion_add_to_wishlists_post_engagement | integer | None |
mobile_conversion_add_to_wishlists_post_view | integer | None |
mobile_conversion_add_to_wishlists_sale_amount | integer | None |
mobile_conversion_checkouts_initiated_assisted | integer | None |
mobile_conversion_checkouts_initiated_order_quantity | integer | None |
mobile_conversion_checkouts_initiated_post_engagement | integer | None |
mobile_conversion_checkouts_initiated_post_view | integer | None |
mobile_conversion_checkouts_initiated_sale_amount | integer | None |
mobile_conversion_content_views_assisted | integer | None |
mobile_conversion_content_views_order_quantity | integer | None |
mobile_conversion_content_views_post_engagement | integer | None |
mobile_conversion_content_views_post_view | integer | None |
mobile_conversion_content_views_sale_amount | integer | None |
mobile_conversion_downloads_order_quantity | integer | None |
mobile_conversion_downloads_post_engagement | integer | None |
mobile_conversion_downloads_post_view | integer | None |
mobile_conversion_downloads_sale_amount | integer | None |
mobile_conversion_installs_assisted | integer | None |
mobile_conversion_installs_order_quantity | integer | None |
mobile_conversion_installs_post_engagement | integer | None |
mobile_conversion_installs_post_view | integer | None |
mobile_conversion_installs_sale_amount | integer | None |
mobile_conversion_invites_assisted | integer | None |
mobile_conversion_invites_order_quantity | integer | None |
mobile_conversion_invites_post_engagement | integer | None |
mobile_conversion_invites_post_view | integer | None |
mobile_conversion_invites_sale_amount | integer | None |
mobile_conversion_key_page_views_post_engagement | integer | None |
mobile_conversion_key_page_views_post_view | integer | None |
mobile_conversion_levels_achieved_assisted | integer | None |
mobile_conversion_levels_achieved_order_quantity | integer | None |
mobile_conversion_levels_achieved_post_engagement | integer | None |
mobile_conversion_levels_achieved_post_view | integer | None |
mobile_conversion_levels_achieved_sale_amount | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_metric | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_order_qu | integer | None |
mobile_conversion_lifetime_value_achievements_unlocked_sale_amo | integer | None |
mobile_conversion_lifetime_value_add_to_carts_metric | integer | None |
mobile_conversion_lifetime_value_add_to_carts_order_quantity | integer | None |
mobile_conversion_lifetime_value_add_to_carts_sale_amount | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_metric | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_order_quantit | integer | None |
mobile_conversion_lifetime_value_add_to_wishlists_sale_amount | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_metric | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_order_quan | integer | None |
mobile_conversion_lifetime_value_checkouts_initiated_sale_amoun | integer | None |
mobile_conversion_lifetime_value_content_views_metric | integer | None |
mobile_conversion_lifetime_value_content_views_order_quantity | integer | None |
mobile_conversion_lifetime_value_content_views_sale_amount | integer | None |
mobile_conversion_lifetime_value_invites_metric | integer | None |
mobile_conversion_lifetime_value_invites_order_quantity | integer | None |
mobile_conversion_lifetime_value_invites_sale_amount | integer | None |
mobile_conversion_lifetime_value_levels_achieved_metric | integer | None |
mobile_conversion_lifetime_value_levels_achieved_order_quantity | integer | None |
mobile_conversion_lifetime_value_levels_achieved_sale_amount | integer | None |
mobile_conversion_lifetime_value_logins_metric | integer | None |
mobile_conversion_lifetime_value_logins_order_quantity | integer | None |
mobile_conversion_lifetime_value_logins_sale_amount | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_metric | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_order_q | integer | None |
mobile_conversion_lifetime_value_payment_info_additions_sale_am | integer | None |
mobile_conversion_lifetime_value_purchases_metric | integer | None |
mobile_conversion_lifetime_value_purchases_order_quantity | integer | None |
mobile_conversion_lifetime_value_purchases_sale_amount | integer | None |
mobile_conversion_lifetime_value_rates_metric | integer | None |
mobile_conversion_lifetime_value_rates_order_quantity | integer | None |
mobile_conversion_lifetime_value_rates_sale_amount | integer | None |
mobile_conversion_lifetime_value_reservations_metric | integer | None |
mobile_conversion_lifetime_value_reservations_order_quantity | integer | None |
mobile_conversion_lifetime_value_reservations_sale_amount | integer | None |
mobile_conversion_lifetime_value_searches_metric | integer | None |
mobile_conversion_lifetime_value_searches_order_quantity | integer | None |
mobile_conversion_lifetime_value_searches_sale_amount | integer | None |
mobile_conversion_lifetime_value_shares_metric | integer | None |
mobile_conversion_lifetime_value_shares_order_quantity | integer | None |
mobile_conversion_lifetime_value_shares_sale_amount | integer | None |
mobile_conversion_lifetime_value_sign_ups_metric | integer | None |
mobile_conversion_lifetime_value_sign_ups_order_quantity | integer | None |
mobile_conversion_lifetime_value_sign_ups_sale_amount | integer | None |
mobile_conversion_lifetime_value_spent_credits_metric | integer | None |
mobile_conversion_lifetime_value_spent_credits_order_quantity | integer | None |
mobile_conversion_lifetime_value_spent_credits_sale_amount | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_metric | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_order_quan | integer | None |
mobile_conversion_lifetime_value_tutorials_completed_sale_amoun | integer | None |
mobile_conversion_lifetime_value_updates_metric | integer | None |
mobile_conversion_lifetime_value_updates_order_quantity | integer | None |
mobile_conversion_lifetime_value_updates_sale_amount | integer | None |
mobile_conversion_logins_assisted | integer | None |
mobile_conversion_logins_order_quantity | integer | None |
mobile_conversion_logins_post_engagement | integer | None |
mobile_conversion_logins_post_view | integer | None |
mobile_conversion_logins_sale_amount | integer | None |
mobile_conversion_payment_info_additions_assisted | integer | None |
mobile_conversion_payment_info_additions_order_quantity | integer | None |
mobile_conversion_payment_info_additions_post_engagement | integer | None |
mobile_conversion_payment_info_additions_post_view | integer | None |
mobile_conversion_payment_info_additions_sale_amount | integer | None |
mobile_conversion_purchases_assisted | integer | None |
mobile_conversion_purchases_order_quantity | integer | None |
mobile_conversion_purchases_post_engagement | integer | None |
mobile_conversion_purchases_post_view | integer | None |
mobile_conversion_purchases_sale_amount | integer | None |
mobile_conversion_rates_assisted | integer | None |
mobile_conversion_rates_order_quantity | integer | None |
mobile_conversion_rates_post_engagement | integer | None |
mobile_conversion_rates_post_view | integer | None |
mobile_conversion_rates_sale_amount | integer | None |
mobile_conversion_re_engages_assisted | integer | None |
mobile_conversion_re_engages_order_quantity | integer | None |
mobile_conversion_re_engages_post_engagement | integer | None |
mobile_conversion_re_engages_post_view | integer | None |
mobile_conversion_re_engages_sale_amount | integer | None |
mobile_conversion_reservations_assisted | integer | None |
mobile_conversion_reservations_order_quantity | integer | None |
mobile_conversion_reservations_post_engagement | integer | None |
mobile_conversion_reservations_post_view | integer | None |
mobile_conversion_reservations_sale_amount | integer | None |
mobile_conversion_searches_assisted | integer | None |
mobile_conversion_searches_order_quantity | integer | None |
mobile_conversion_searches_post_engagement | integer | None |
mobile_conversion_searches_post_view | integer | None |
mobile_conversion_searches_sale_amount | integer | None |
mobile_conversion_shares_assisted | integer | None |
mobile_conversion_shares_order_quantity | integer | None |
mobile_conversion_shares_post_engagement | integer | None |
mobile_conversion_shares_post_view | integer | None |
mobile_conversion_shares_sale_amount | integer | None |
mobile_conversion_sign_ups_assisted | integer | None |
mobile_conversion_sign_ups_order_quantity | integer | None |
mobile_conversion_sign_ups_post_engagement | integer | None |
mobile_conversion_sign_ups_post_view | integer | None |
mobile_conversion_sign_ups_sale_amount | integer | None |
mobile_conversion_site_visits_order_quantity | integer | None |
mobile_conversion_site_visits_post_engagement | integer | None |
mobile_conversion_site_visits_post_view | integer | None |
mobile_conversion_site_visits_sale_amount | integer | None |
mobile_conversion_spent_credits_assisted | integer | None |
mobile_conversion_spent_credits_order_quantity | integer | None |
mobile_conversion_spent_credits_post_engagement | integer | None |
mobile_conversion_spent_credits_post_view | integer | None |
mobile_conversion_spent_credits_sale_amount | integer | None |
mobile_conversion_tutorials_completed_assisted | integer | None |
mobile_conversion_tutorials_completed_order_quantity | integer | None |
mobile_conversion_tutorials_completed_post_engagement | integer | None |
mobile_conversion_tutorials_completed_post_view | integer | None |
mobile_conversion_tutorials_completed_sale_amount | integer | None |
mobile_conversion_updates_assisted | integer | None |
mobile_conversion_updates_order_quantity | integer | None |
mobile_conversion_updates_post_engagement | integer | None |
mobile_conversion_updates_post_view | integer | None |
mobile_conversion_updates_sale_amount | integer | None |
poll_card_vote | integer | None |
qualified_impressions | integer | None |
replies | integer | None |
retweets | integer | None |
tweets_send | integer | None |
unfollows | integer | None |
url_clicks | integer | None |
video_3_s_100_pct_views | integer | None |
video_6_s_views | integer | None |
video_content_starts | integer | None |
video_cta_clicks | integer | None |
video_mrc_views | integer | None |
video_total_views | integer | None |
video_views_100 | integer | None |
video_views_25 | integer | None |
video_views_50 | integer | None |
video_views_75 | integer | None |
mobile_conversion_installs_skan_post_engagement | integer | None |
mobile_conversion_installs_skan_post_view | integer | None |
mobile_conversion_purchases_skan_post_engagement | integer | None |
mobile_conversion_purchases_skan_post_view | integer | None |
video_15_s_views | integer | None |
auto_created_conversion_landing_page_view | integer | None |
auto_created_conversion_session | integer | None |
This SQL query performs several transformations on data from a Twitter Ads source table. It casts several fields to specific data types, renames some columns, and adds a flag to identify the latest version of each promoted tweet. The query also includes a source relation field, though it's set to an empty string in this case.
CleaningDeduplicationFeaturizationWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_history_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS approval_status,
CAST(NULL AS TIMESTAMP) AS created_at,
CAST(NULL AS BOOLEAN) AS deleted,
CAST(NULL AS TEXT) AS entity_status,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS line_item_id,
CAST(NULL AS TEXT) AS tweet_id,
CAST(NULL AS TIMESTAMP) AS updated_at,
CAST('' AS TEXT) AS source_relation
FROM source
), final AS (
SELECT
source_relation,
approval_status,
created_at AS created_timestamp,
deleted AS is_deleted,
entity_status,
id AS promoted_tweet_id,
line_item_id,
tweet_id,
updated_at AS updated_timestamp,
ROW_NUMBER() OVER (PARTITION BY source_relation, id ORDER BY updated_at DESC) = 1 AS is_latest_version
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. |
approval_status | text | The approval status of the promoted tweet. |
created_timestamp | text | The timestamp the account was created. |
is_deleted | boolean | Whether the record has been deleted or not. |
entity_status | text | The status of the promoted tweet. |
promoted_tweet_id | text | The ID of the promoted tweet. |
line_item_id | text | The ID of the related line item. |
tweet_id | bigint | The ID of the related tweet. |
updated_timestamp | text | The timestamp the account was last updated. |
is_latest_version | boolean | Whether the record is the most latest version for the promoted tweet. |
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 template or placeholder for future data operations or to create a temporary table structure.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_synced | text | None |
approval_status | text | None |
created_at | text | None |
deleted | boolean | None |
entity_status | text | None |
id | text | None |
line_item_id | text | None |
tweet_id | bigint | None |
updated_at | text | None |
This SQL query processes data from a Twitter ads promoted tweet report. It first casts columns to specific data types, renames them, and then performs calculations on the spend data. The query truncates the date to the day level and converts the spend from micro-units to standard currency units. It doesn't filter or aggregate the data, but rather prepares it for further analysis.
CleaningFeaturizationWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__promoted_tweet_report_tmp
), renamed AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS INT) AS billed_charge_local_micro,
CAST(NULL AS INT) AS clicks,
CAST(NULL AS TIMESTAMP) AS date,
CAST(NULL AS INT) AS impressions,
CAST(NULL AS TEXT) AS placement,
CAST(NULL AS TEXT) AS promoted_tweet_id,
CAST(NULL AS INT) AS url_clicks,
CAST('' AS TEXT) AS source_relation
FROM source
), spend_calc AS (
SELECT
source_relation,
DATE_TRUNC('DAY', date) AS date_day,
account_id,
promoted_tweet_id,
placement,
clicks AS clicks,
impressions AS impressions,
billed_charge_local_micro AS spend_micro,
ROUND(billed_charge_local_micro / 1000000.0, 2) AS spend,
url_clicks AS url_clicks
FROM renamed
)
SELECT
*
FROM spend_calc
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 | text | The ID of the related account. |
spend_micro | integer | The spend (in micros) for the promoted tweet on that day. |
spend | numeric | The spend for the promoted tweet on that day. |
clicks | integer | The clicks for the promoted tweet on that day. Includes clicks on the URL (shortened or regular links), profile pic, screen name, username, detail, hashtags, and likes. |
date_day | timestamp without time zone | The date of the performance. |
impressions | integer | The impressions for the promoted tweet on that day. This is the number of users who see a Promoted Ad either in their home timeline or search results. |
promoted_tweet_id | text | The ID of the related promoted tweet. |
url_clicks | integer | The url clicks for the promoted tweet on that day. |
placement | text | Where on Twitter the ad is being displayed. Possible values include 'ALL_ON_TWITTER', 'PUBLISHER_NETWORK', 'TWITTER_PROFILE', 'TWITTER_SEARCH', 'TWITTER_TIMELINE', and 'TAP_*', which are more granular options for `PUBLISHER_NETWORK`. |
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 a staging table in a dbt (data build tool) project, specifically for Twitter ads promoted tweet reports.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_synced | text | None |
account_id | text | None |
billed_charge_local_micro | integer | None |
clicks | integer | None |
date | timestamp without time zone | None |
impressions | integer | None |
promoted_tweet_id | text | None |
url_clicks | integer | None |
placement | text | None |
This SQL query performs a basic data transformation on the 'stg_twitter_ads__tweet_tmp' table. It casts several columns to TEXT type with NULL or empty string values, renames some columns, and selects a subset of columns for the final output. The query doesn't filter, aggregate, or integrate data from multiple sources.
CleaningOtherWITH base AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__tweet_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS account_id,
CAST(NULL AS TEXT) AS full_text,
CAST(NULL AS TEXT) AS id,
CAST(NULL AS TEXT) AS lang,
CAST(NULL AS TEXT) AS name,
CAST('' AS TEXT) AS source_relation
FROM base
), final AS (
SELECT
source_relation,
account_id,
id AS tweet_id,
name,
full_text,
lang AS language
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 | text | The ID of the related account. |
tweet_id | integer | Unique identifier of the tweet. |
name | integer | If provided, the non-public title of the tweet. |
full_text | text | Full text of the tweet's content. |
language | text | Two-letter language code of the tweet. |
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 LIMIT 0 clause ensures no rows are returned. This type of query is often used as a placeholder or to define a schema structure without actually populating data.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
id | integer | None |
_fivetran_synced | text | None |
account_id | text | None |
card_uri | integer | None |
coordinates_coordinates | integer | None |
coordinates_type | integer | None |
created_at | text | None |
favorite_count | integer | None |
favorited | boolean | None |
followers | integer | None |
full_text | text | None |
geo_coordinates | integer | None |
geo_type | integer | None |
in_reply_to_screen_name | integer | None |
in_reply_to_status_id | integer | None |
in_reply_to_user_id | integer | None |
lang | text | None |
media_key | integer | None |
retweet_count | integer | None |
retweeted | boolean | None |
source | text | None |
truncated | boolean | None |
tweet_type | text | None |
user_id | integer | None |
name | integer | None |
This SQL query processes Twitter ad data, specifically focusing on tweet URLs. It starts by casting fields to specific data types, then performs extensive URL parsing and analysis. The query extracts various components from the expanded URL, including the base URL, host, path, and UTM parameters (source, medium, campaign, content, and term). This transformation and feature extraction from URL data can be valuable for analyzing the effectiveness and reach of Twitter ad campaigns.
CleaningFeaturizationWITH source AS (
SELECT
*
FROM TEST.PUBLIC_twitter_ads_source.stg_twitter_ads__tweet_url_tmp
), fields AS (
SELECT
CAST(NULL AS TEXT) AS display_url,
CAST(NULL AS TEXT) AS expanded_url,
CAST(NULL AS INT) AS index,
CAST(NULL AS TEXT) AS indices,
CAST(NULL AS TEXT) AS tweet_id,
CAST(NULL AS TEXT) AS url,
CAST('' AS TEXT) AS source_relation
FROM source
), final AS (
SELECT
source_relation,
display_url,
expanded_url,
index,
indices,
tweet_id,
url,
SPLIT_PART(expanded_url, '?', 1) AS base_url,
TRY_CAST(SPLIT_PART(
SPLIT_PART(
REPLACE(REPLACE(REPLACE(expanded_url, 'android-app://', ''), 'http://', ''), 'https://', ''),
'/',
1
),
'?',
1
) AS TEXT) AS url_host,
'/' || TRY_CAST(SPLIT_PART(
CASE
WHEN LENGTH(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', ''), '?') - 1
) = 0
THEN ''
ELSE RIGHT(
REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', ''),
LENGTH(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', '')) - COALESCE(
NULLIF(
STR_POSITION(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', ''), '/'),
0
),
STR_POSITION(REPLACE(REPLACE(expanded_url, 'http://', ''), 'https://', ''), '?') - 1
)
)
END,
'?',
1
) AS TEXT) AS url_path,
NULLIF(SPLIT_PART(SPLIT_PART(expanded_url, 'utm_source=', 2), '&', 1), '') AS utm_source,
NULLIF(SPLIT_PART(SPLIT_PART(expanded_url, 'utm_medium=', 2), '&', 1), '') AS utm_medium,
NULLIF(SPLIT_PART(SPLIT_PART(expanded_url, 'utm_campaign=', 2), '&', 1), '') AS utm_campaign,
NULLIF(SPLIT_PART(SPLIT_PART(expanded_url, 'utm_content=', 2), '&', 1), '') AS utm_content,
NULLIF(SPLIT_PART(SPLIT_PART(expanded_url, 'utm_term=', 2), '&', 1), '') AS utm_term
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. |
display_url | text | The URL as it will be displayed. |
expanded_url | text | The fully expanded URL. |
index | integer | The index of the URL within the tweet (ie if there are multiple URLs) |
indices | text | The start and end point of where the URL is placed in the tweet text. |
tweet_id | bigint | The ID of the related tweet. |
url | text | The 't.co' shortened URL. |
base_url | text | The base URL of the ad, extracted from the `expanded_url`. |
url_host | text | The URL host of the ad, extracted from the `expanded_url`. |
url_path | text | The URL path of the ad, extracted from the `expanded_url`. |
utm_source | text | The utm_source parameter of the ad, extracted from the `expanded_url`. |
utm_medium | text | The utm_medium parameter of the ad, extracted from the `expanded_url`. |
utm_campaign | text | The utm_campaign parameter of the ad, extracted from the `expanded_url`. |
utm_content | text | The utm_content parameter of the ad, extracted from the `expanded_url`. |
utm_term | text | The utm_term parameter of the ad, extracted from the `expanded_url`. |
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 LIMIT 0 ensures no rows are returned. This appears to be a template or placeholder query, likely used for initializing or testing purposes in a dbt (data build tool) model.
OtherSELECT
CAST(NULL AS TEXT) AS _dbt_source_relation
LIMIT 0
Name | Type | Comment |
---|---|---|
_fivetran_synced | text | None |
display_url | text | None |
expanded_url | text | None |
index | integer | None |
indices | text | None |
tweet_id | bigint | None |
url | text | None |