Advertisement
roganhamby

Active Patron Count

Feb 19th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. drop table if exists rogan.active_sip;
  2. drop table if exists rogan.active_circ;
  3. drop table if exists rogan.active_expire;
  4. drop table if exists rogan.active_usrs;
  5.  
  6. create table rogan.active_sip as
  7. SELECT au.id, au.home_ou, MAX(aua.event_time) AS TIME
  8. FROM actor.usr au
  9. JOIN actor.usr_activity aua ON aua.usr = au.id
  10. join actor.org_unit aou on aou.id = au.home_ou
  11. WHERE aou.shortname ilike 'dcl%'
  12. GROUP BY 1, 2
  13. ;
  14.  
  15. create table rogan.active_circ as
  16. SELECT au.id, au.home_ou, MAX(acirc.xact_start) AS TIME
  17. FROM actor.usr au
  18. JOIN ACTION.circulation acirc ON acirc.usr = au.id
  19. join actor.org_unit aou on aou.id = au.home_ou
  20. WHERE aou.shortname ilike 'dcl%'
  21. GROUP BY 1, 2
  22. ;
  23.  
  24. create table rogan.active_expire as
  25. select au.id, au.home_ou, au.expire_date as TIME
  26. from actor.usr au
  27. join actor.org_unit aou on aou.id = au.home_ou
  28. where aou.shortname ilike 'dcl%' and au.deleted = false and au.active = true
  29. ;
  30.  
  31. create table rogan.active_usrs as
  32. select distinct id, home_ou
  33. from ( select * from rogan.active_sip
  34.     UNION ALL
  35.     select * from rogan.active_expire
  36.     UNION ALL
  37.     select * from rogan.active_circ
  38.     ) peel
  39. where time >= now() - interval '3 years'
  40. ;
  41.  
  42. -- active users
  43. select emma.shortname as library, emma.active, peel.inactive from
  44. ( select count(au.id) as active, aou.shortname
  45.     from rogan.active_usrs au
  46.     join actor.org_unit aou on aou.id = au.home_ou
  47.     group by 2 ) emma
  48. join ( SELECT count(au.id) as inactive, aou.shortname
  49.     FROM actor.usr au
  50.     join actor.org_unit aou on aou.id = au.home_ou
  51.     WHERE aou.shortname ilike 'dcl%' and au.id not in (select id from rogan.active_usrs)
  52.     GROUP BY 2 ) peel on peel.shortname = emma.shortname
  53. ;
  54.  
  55.  
  56.  
  57. /*** profile counts ***/
  58.  
  59. DROP TABLE if exists rogan.active_sip;
  60. DROP TABLE if exists rogan.active_circ;
  61. DROP TABLE if exists rogan.active_expire;
  62. DROP TABLE if exists rogan.active_usrs;
  63.  
  64. CREATE TABLE rogan.active_sip AS
  65. SELECT au.id, au.home_ou, au.profile, MAX(aua.event_time) AS TIME
  66. FROM actor.usr au
  67. JOIN actor.usr_activity aua ON aua.usr = au.id
  68. JOIN actor.org_unit aou ON aou.id = au.home_ou
  69. WHERE aou.shortname ILIKE 'dcl%'
  70. GROUP BY 1, 2, 3
  71. ;
  72.  
  73. CREATE TABLE rogan.active_circ AS
  74. SELECT au.id, au.home_ou, au.profile, MAX(acirc.xact_start) AS TIME
  75. FROM actor.usr au
  76. JOIN ACTION.circulation acirc ON acirc.usr = au.id
  77. JOIN actor.org_unit aou ON aou.id = au.home_ou
  78. WHERE aou.shortname ILIKE 'dcl%'
  79. GROUP BY 1, 2, 3
  80. ;
  81.  
  82. CREATE TABLE rogan.active_expire AS
  83. SELECT au.id, au.home_ou, au.profile, au.expire_date AS TIME
  84. FROM actor.usr au
  85. JOIN actor.org_unit aou ON aou.id = au.home_ou
  86. WHERE aou.shortname ILIKE 'dcl%' AND au.deleted = FALSE AND au.active = TRUE
  87. ;
  88.  
  89. CREATE TABLE rogan.active_usrs AS
  90. SELECT DISTINCT id, home_ou, profile
  91. FROM ( SELECT * FROM rogan.active_sip
  92.     UNION ALL
  93.     SELECT * FROM rogan.active_expire
  94.     UNION ALL
  95.     SELECT * FROM rogan.active_circ
  96.     ) peel
  97. WHERE TIME >= NOW() - INTERVAL '3 years'
  98. ;
  99.  
  100. SELECT aou.shortname as library, pgt.name as profile, COUNT(au.id) AS active
  101.     FROM rogan.active_usrs au
  102.     JOIN actor.org_unit aou ON aou.id = au.home_ou
  103.     join permission.grp_tree pgt on pgt.id = au.profile
  104.     GROUP BY 1, 2
  105.     Order by 1, 2
  106. ;
  107.    
  108.    
  109. SELECT aou.shortname as library, pgt.name as profile, COUNT(au.id) AS inactive
  110.     FROM actor.usr au
  111.     JOIN actor.org_unit aou ON aou.id = au.home_ou
  112.     join permission.grp_tree pgt on pgt.id = au.profile
  113.     WHERE aou.shortname ILIKE 'dcl%' AND au.id NOT IN (SELECT id FROM rogan.active_usrs)
  114.     GROUP BY 1, 2
  115.     order by 1, 2
  116. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement