Advertisement
roganhamby

Gale Analytics - Marketing report

May 28th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. drop table if exists rogan.gale_marketing;
  2.  
  3. create table rogan.gale_marketing as
  4. select au.id, aua.street1, aua.street2, aua.city, aua.state, aua.post_code as zip
  5. from actor.usr au
  6. join actor.usr_address aua on aua.id = au.mailing_address
  7. join actor.org_unit child_org on child_org.id = au.home_ou
  8. join actor.org_unit parent_org on parent_org.id = child_org.parent_ou
  9. where parent_org.shortname = 'YCL' and au.deleted = false and au.active = true
  10. and au.expire_date > now() - interval '2 years'
  11. -- and (aua.post_code ilike '297%' or aua.post_code ilike '28%')
  12. and aua.county = 'York' and aua.post_code != '' and street1 != ''
  13. group by 1, 2, 3, 4, 5, 6
  14. ;
  15.  
  16. alter table rogan.gale_marketing add column event_time timestamptz;
  17. alter table rogan.gale_marketing add column circ_time timestamptz;
  18.  
  19. update rogan.gale_marketing set event_time = r.ret
  20. from ( select rgm.id as rd, max(aua.event_time) as ret
  21. from rogan.gale_marketing rgm
  22. join actor.usr_activity aua on aua.usr = rgm.id
  23. group by 1) r
  24. where r.rd = id ;
  25.  
  26. update rogan.gale_marketing set circ_time = r.ret
  27. from ( select rgm.id as rd, max(acirc.create_time) as ret
  28. from rogan.gale_marketing rgm
  29. join action.circulation acirc on acirc.usr = rgm.id
  30. group by 1) r
  31. where r.rd = id ;
  32.  
  33.  
  34.  
  35. select * from rogan.gale_marketing limit 10;
  36.  
  37. select street1 as address, street2, city, state, zip, date(greatest(circ_time,event_time)) as "date"
  38. from rogan.gale_marketing
  39. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement