Advertisement
roganhamby

Statistics - Circulation

Jan 13th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Feb 2015, ripping up and redoing this old work from a few years ago to make it cleaner and more flexible
  3. will include pre-calculated circ tables by user profile, location and circ mod by org unit and month / fiscal year
  4. will include add on data sources to add value, population, poverty rates, value of items
  5. some templates will show it's power in the reporter but eventual goal is a director's dashboard
  6. */
  7.  
  8.  
  9. /* eventually we could have this on a cron task to do it monthly and auto add each month for review */
  10.  
  11. /* let's create a table to define the fiscal years that we want to group by */
  12.  
  13. create table rogan.statistical_years (
  14.     id      serial,
  15.     type    text    not null,
  16.     name    text    not null,
  17.     year_start  timestamp with time zone    not null,
  18.     year_end    timestamp with time zone    not null
  19. );
  20.  
  21. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2008/09','2008-07-01 00:00:00.000000-05','2009-06-30 23:59:59.000000-05');
  22. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2009/10','2009-07-01 00:00:00.000000-05','2010-06-30 23:59:59.000000-05');
  23. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2010/11','2010-07-01 00:00:00.000000-05','2011-06-30 23:59:59.000000-05');
  24. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2011/12','2011-07-01 00:00:00.000000-05','2012-06-30 23:59:59.000000-05');
  25. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2012/13','2012-07-01 00:00:00.000000-05','2013-06-30 23:59:59.000000-05');
  26. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2013/14','2013-07-01 00:00:00.000000-05','2014-06-30 23:59:59.000000-05');
  27. insert into rogan.statistical_years (type,name,year_start,year_end) values ('fiscal','2014/15','2014-07-01 00:00:00.000000-05','2015-06-30 23:59:59.000000-05');
  28.  
  29. select * from rogan.statistical_years;
  30.  
  31. /* now let's design a table to hold monthly circulation stats */
  32.  
  33. create table rogan.stats_circs (
  34.     id          serial,
  35.     circs       int,
  36.     circ_lib    int,
  37.     circ_modifier   text,
  38.     location    int,
  39.     stats_year  int,
  40.     month       int,
  41.     usr_profile int
  42. );
  43.  
  44.  
  45. /* now we can insert each additional year by just using the y.id = to increment once the appropriate year is in the
  46. statistical_year table */
  47.  
  48. insert into rogan.stats_circs (circs, circ_lib, circ_modifier, location, stats_year, month, usr_profile) (
  49.     select count(acirc.id) as circs, acirc.circ_lib, ac.circ_modifier, acl.id as location, y.id as stats_year,
  50.     extract(month from acirc.xact_start) as month, au.profile as usr_profile
  51.     from action.circulation acirc
  52.     join asset.copy ac on ac.id = acirc.target_copy
  53.     join actor.org_unit aou on aou.id = acirc.circ_lib
  54.     join rogan.statistical_years y on y.id = 6
  55.     join asset.copy_location acl on acl.id = ac.location
  56.     join actor.usr au on au.id = acirc.usr
  57.     where acirc.xact_start >= y.year_start and acirc.xact_start <= y.year_end
  58.     group by 2, 3, 4, 5, 6, 7
  59. );
  60.  
  61. /* now let's add in the stats only checkouts */
  62. insert into rogan.stats_circs (circs, circ_lib, circ_modifier, location, stats_year, month, usr_profile) (
  63.     select count(ncc.id) as circs, ncc.circ_lib, nct.name as circ_modifier, 1 as location, y.id as stats_year,
  64.     extract(month from ncc.circ_time) as month, au.profile as usr_profile
  65.     from action.non_cataloged_circulation ncc
  66.     join actor.org_unit aou on aou.id = ncc.circ_lib
  67.     join config.non_cataloged_type nct on nct.id = ncc.item_type
  68.     join rogan.statistical_years y on y.id = 6
  69.     join actor.usr au on au.id = ncc.patron
  70.     where ncc.circ_time >= y.year_start and ncc.circ_time <= y.year_end
  71.     group by 2, 3, 4, 5, 6, 7
  72. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement