diff --git a/src/meta_api_grabber/db_schema.sql b/src/meta_api_grabber/db_schema.sql index d2aafa9..f07593d 100644 --- a/src/meta_api_grabber/db_schema.sql +++ b/src/meta_api_grabber/db_schema.sql @@ -507,6 +507,30 @@ FROM ( ) base; +-- ============================================================================ +-- UNIFIED VIEW +DROP VIEW IF EXISTS unified_account_insights CASCADE; + +CREATE VIEW unified_account_insights AS +SELECT + g.time, + g.account_id::varchar as google_account_id, + m.account_id as meta_account_id, + + sum(g.impressions + m.impressions) as impressions, + sum(g.clicks + m.clicks) as clicks, + sum(g.clicks + m.link_click) as link_click, + sum(g.leads + m.lead) as lead, + sum(g.cost + m.spend) as spend + + +FROM account_metadata as am +JOIN g_account_insights as g ON g.account_id::varchar = am.google_account_id +JOIN account_insights as m ON m.account_id = am.meta_account_id AND m.time = g.time +GROUP BY g.time, 2, 3 +ORDER BY g.time; + +