Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. begin;
  2. drop table if exists derived.ad_clicks;
  3. commit;
  4.  
  5. begin;
  6. create table derived.ad_clicks as (
  7.  
  8. with pages_with_ad_ids as (
  9. select
  10. id,
  11. anonymous_id,
  12. received_at,
  13. substring(search, '.*[?&]gclid=([^$&]*)') as gcl_id,
  14. substring(search, '.*[?&]fbad_id=([^$&]*)') as fbad_id
  15. from marketing.pages
  16. )
  17.  
  18. select
  19. p.id,
  20. p.received_at,
  21. p.anonymous_id,
  22. case
  23. when p.gcl_id is not null and p.fbad_id is null then 'Google'
  24. when p.fbad_id is not null and p.gcl_id is null then 'Facebook'
  25. else 'wtf'
  26. end as platform,
  27. case
  28. when p.gcl_id is not null then coalesce(g3.name, '[unknown]')
  29. when p.fbad_id is not null then coalesce(f3.name, '[unknown]')
  30. else 'wtf'
  31. end as campaign_name,
  32. case
  33. when p.gcl_id is not null then coalesce(g2.name, '[unknown]')
  34. when p.fbad_id is not null then coalesce(f2.name, '[unknown]')
  35. else 'wtf'
  36. end as ad_group_name
  37. from pages_with_ad_ids p
  38. left join adwords.click_performance_reports g1 on g1.gcl_id = p.gcl_id
  39. left join adwords.ad_groups g2 on g2.id = g1.ad_group_id
  40. left join adwords.campaigns g3 on g3.id = g2.campaign_id
  41. left join facebookads.ads f1 on f1.id = p.fbad_id
  42. left join facebookads.ad_sets f2 on f2.id = f1.adset_id
  43. left join facebookads.campaigns f3 on f3.id = f2.campaign_id
  44. where p.gcl_id is not null or p.fbad_id is not null
  45.  
  46. );
  47. commit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement