Advertisement
roganhamby

Gale Analytics - Patron Profile and Collection Intelligence

May 28th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1.  
  2. \timing on
  3. \set search_path to gale_analytics, public, evergreen;
  4. \set county '''sample_county_name'''
  5. \set orgunit '''sample_org_unit'''
  6. \set parentorg '''sample_parent_org_unit'''
  7.  
  8. drop table if exists gale;
  9. drop table if exists apts;
  10.  
  11. -- patron profile data
  12. create table gale as
  13. select aua.street1, aua.street2, aua.city, aua.state, aua.post_code, circ.id as circ_id, circ.xact_start,
  14. acl.name as "copy_location", ac.circ_modifier
  15. from action.circulation circ
  16. join actor.usr au on au.id = circ.usr
  17. join actor.usr_address aua on aua.id = au.mailing_address
  18. join actor.org_unit child_org on child_org.id = au.home_ou
  19. join actor.org_unit parent_org on parent_org.id = child_org.parent_ou
  20. join asset.copy ac on ac.id = circ.target_copy
  21. join asset.copy_location acl on acl.id = circ.copy_location
  22. where parent_org.shortname = :orgunit and circ.xact_start > now() - interval '3 years'
  23. -- where child_org.shortname = :orgunit and circ.xact_start > now() - interval '3 years'
  24. -- several of these optional restrictions are to help them create more geographically shaped data
  25. -- and (aua.post_code ilike '%' or aua.post_code ilike '%')
  26. -- and aua.county ilike :county
  27. and aua.post_code != '' and street1 != ''
  28. ;
  29.  
  30. -- data for the collection development module
  31. insert into gale (street1, street2, city, state, post_code, circ_id, xact_start, copy_location, circ_modifier) (
  32. select aua.street1, aua.street2, aua.city, aua.state, aua.post_code, circ.id, circ.circ_time, nct.name, nct.name
  33. from action.non_cataloged_circulation circ
  34. join actor.usr au on au.id = circ.patron
  35. join actor.usr_address aua on aua.id = au.mailing_address
  36. join actor.org_unit child_org on child_org.id = au.home_ou
  37. join actor.org_unit parent_org on parent_org.id = child_org.parent_ou
  38. join config.non_cataloged_type nct on nct.id = circ.item_type
  39. where parent_org.shortname = :parent_org and circ.circ_time > now() - interval '3 years'
  40. and nct.name not ilike 'Kinderbox%'
  41. -- where child_org.shortname = 'YCL-BK' and circ.circ_time > now() - interval '3 years'
  42. -- and (aua.post_code ilike '%' or aua.post_code ilike '%')
  43. -- and aua.county ilike :county
  44. and aua.post_code != '' and street1 != ''
  45. );
  46.  
  47. update gale set copy_location = 'STATS ONLY' where circ_modifier = copy_location;
  48.  
  49. alter table gale add column id serial;
  50.  
  51. -- swap apartments lines so gale will read them
  52. create table apts as
  53. select id, substring(street1 from '^(.+?),') as street, substring(street1 from ',.*$') as apt,
  54. street1, street2 from gale where street1 ilike '%,%'
  55. and street1 ilike '%apt%';
  56. update gale rg set street1 = ra.apt from (select id, apt from apts) ra where ra.id = rg.id;
  57. update gale rg set street2 = ra.street from (select id, street from apts) ra where ra.id = rg.id;
  58. update gale set street1 = trim(leading ',' from street1), street2 = trim(leading ',' from street2) where circ_id is not null;
  59. update gale set street1 = trim(leading ' ' from street1), street2 = trim(leading ' ' from street2) where circ_id is not null;
  60. update gale set street1 = trim(trailing ',' from street1), street2 = trim(trailing ',' from street2) where circ_id is not null;
  61. update gale set street1 = trim(trailing ' ' from street1), street2 = trim(trailing ' ' from street2) where circ_id is not null;
  62.  
  63. -- cleans up SIP activity not tied to any discernable account
  64. -- should be redundant now with the criteria set above but still here just in case
  65. delete from gale where post_code = '99999';
  66.  
  67. -- cleanup as these will get kicked otherwise
  68. update gale set street2 = null where street2 = '';
  69.  
  70.  
  71. -- select * from gale where id in (select id from apts);
  72. -- test
  73.  
  74. -- adding in the electronic activity for collection development
  75. insert into gale (street1, street2, city, state, post_code, circ_id, xact_start, copy_location, circ_modifier) (
  76. select aua.street1, aua.street2, aua.city, aua.state, aua.post_code, usr_act.id, usr_act.event_time, act_type.ewho, act_type.ewho
  77. from actor.usr_activity usr_act
  78. join actor.usr au on au.id = usr_act.usr
  79. join actor.usr_address aua on aua.id = au.mailing_address
  80. join actor.org_unit child_org on child_org.id = au.home_ou
  81. join actor.org_unit parent_org on parent_org.id = child_org.parent_ou
  82. join config.usr_activity_type act_type on act_type.id = usr_act.etype
  83. where parent_org.shortname = :parentorg and usr_act.event_time > now() - interval '3 months'
  84. and aua.county ilike :orgunit and aua.post_code != '' and street1 != ''
  85. and act_type.ehow = 'sip2' and act_type.ewhat = 'verify'
  86. -- and act_type.ewho = 'overdrive'
  87. );
  88.  
  89.  
  90. -- now let's clean up shelving to be a cleaner audience expector
  91. -- a major pain because of other library's locations
  92.  
  93. update gale set copy_location = 'Adult' where copy_location ilike 'Adult%';
  94. update gale set copy_location = 'Juvenile' where copy_location ilike 'Juv%';
  95. update gale set copy_location = 'Teen' where copy_location ilike 'Teen%';
  96. update gale set copy_location = 'Teen' where copy_location ilike 'YA%';
  97. update gale set copy_location = 'Juvenile' where copy_location ilike 'Child%';
  98. update gale set copy_location = 'Juvenile' where copy_location ilike 'Easy%';
  99. update gale set copy_location = 'Teen' where copy_location ilike 'Young%';
  100. update gale set copy_location = 'Juvenile' where copy_location ilike 'Board%';
  101. update gale set copy_location = 'Adult' where copy_location not in ('Adult','Juvenile','Teen','SIP2');
  102.  
  103. -- just to see if we got everything
  104. select count(circ_modifier), copy_location from gale where copy_location not in ('Adult','Juvenile','Teen','SIP2') group by 2;
  105.  
  106. -- ----------------------------------------------------------------------------------------------------------
  107. -- Patron profiles gale report ------------------------------------------------------------------------------
  108. -- ----------------------------------------------------------------------------------------------------------
  109.  
  110.  
  111. select street1 as "ADDRESS", street2 as "SUITE (OPTIONAL)", city as "CITY", state as "STATE", post_code as "ZIP CODE",
  112. count(circ_id) as "CHECKOUTS (OPTIONAL)", max(date(xact_start)) as "DATE"
  113. from gale
  114. -- where state ilike 'sc' or state ilike 'nc'
  115. -- where post_code ilike '297%'
  116. group by 1, 2, 3, 4, 5;
  117. -- remember after pasting into excel to remove the 'null's with actual blanks, stupid copying and pasting
  118.  
  119. -- ----------------------------------------------------------------------------------------------------------
  120. -- collection intelligence gale report ----------------------------------------------------------------------
  121. -- only generating the first file, second file not needed ---------------------------------------------------
  122. -- ----------------------------------------------------------------------------------------------------------
  123.  
  124. -- \o collection_intelligence.txt
  125.  
  126. select rg.circ_modifier as "FORMAT", rg.copy_location as "SUBJECT/GENRE", street1 as "STREET ADDRESS",
  127. street2 as "STREET ADDRESS LINE 2", city as "CITY", state as "STATE", post_code as "ZIP"
  128. from gale rg
  129. where rg.circ_modifier is not null
  130. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement