Advertisement
roganhamby

Lost Item Report

Oct 18th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP TABLE IF EXISTS rogan.lost_items;
  2. DROP TABLE IF EXISTS rogan.bought_items;
  3. DROP TABLE IF EXISTS rogan.bought_summary;
  4. DROP TABLE IF EXISTS rogan.lost_summary;
  5.  
  6. CREATE TABLE rogan.lost_items AS
  7. SELECT ac.barcode, aou.shortname AS branch, parent.shortname AS SYSTEM, acn.label AS "call number",
  8. acl.NAME AS LOCATION, ccs.NAME AS status, ac.circ_modifier,
  9. ssr.author, ssr.title, ssr.pubdate AS "publication date", ac.COST AS "item price",
  10. date(ac.edit_date) AS "last edit date"
  11. FROM asset.COPY ac
  12. JOIN config.copy_status ccs ON ccs.id = ac.status
  13. JOIN actor.org_unit aou ON aou.id = ac.circ_lib
  14. JOIN asset.call_number acn ON acn.id = ac.call_number
  15. JOIN reporter.super_simple_record ssr ON ssr.id = acn.record
  16. JOIN asset.copy_location acl ON acl.id = ac.LOCATION
  17. JOIN actor.org_unit parent ON parent.id = aou.parent_ou
  18. WHERE ccs.id IN (SELECT id FROM config.copy_status WHERE NAME ILIKE '%lost%')
  19. AND ac.deleted = FALSE
  20. AND date(ac.edit_date) >= '2014-07-01' AND date(ac.edit_date) <= '2015-06-30'
  21. ;
  22.  
  23. CREATE TABLE rogan.bought_items AS
  24. SELECT ac.barcode, aou.shortname AS branch, parent.shortname AS SYSTEM, acn.label AS "call number",
  25. acl.NAME AS LOCATION, ccs.NAME AS status, ac.circ_modifier,
  26. ssr.author, ssr.title, ssr.pubdate AS "publication date", ac.COST AS "item price",
  27. date(ac.create_date) AS "create date"
  28. FROM asset.COPY ac
  29. JOIN config.copy_status ccs ON ccs.id = ac.status
  30. JOIN actor.org_unit aou ON aou.id = ac.circ_lib
  31. JOIN asset.call_number acn ON acn.id = ac.call_number
  32. JOIN reporter.super_simple_record ssr ON ssr.id = acn.record
  33. JOIN asset.copy_location acl ON acl.id = ac.LOCATION
  34. JOIN actor.org_unit parent ON parent.id = aou.parent_ou
  35. WHERE date(ac.create_date) >= '2014-07-01' AND date(ac.create_date) <= '2015-06-30'
  36. ;
  37.  
  38. /*
  39. SELECT * FROM rogan.lost_items
  40. ORDER BY SYSTEM, branch, LOCATION, "call number", author, title;
  41. */
  42.  
  43. CREATE TABLE rogan.lost_summary AS
  44. SELECT a.SYSTEM, a.item_count AS "total lost items", b.circ_modifier, b.item_count AS "lost items",
  45. ROUND((cast(b.item_count AS NUMERIC)/cast(a.item_count AS NUMERIC))*100,2) AS percent
  46. FROM (SELECT COUNT(barcode) AS item_count, SYSTEM FROM rogan.lost_items GROUP BY 2) a
  47. LEFT JOIN (SELECT COUNT(barcode) AS item_count, SYSTEM, circ_modifier FROM rogan.lost_items GROUP BY 2, 3) b ON b.SYSTEM = a.SYSTEM
  48. -- WHERE circ_modifier = 'VIDEO'
  49. ORDER BY 1, 3
  50. ;
  51.  
  52. select * from rogan.lost_summary;
  53.  
  54. CREATE TABLE rogan.bought_summary AS
  55. SELECT a.SYSTEM, a.item_count AS "total acquired items", b.circ_modifier, b.item_count AS "bought items",
  56. ROUND((cast(b.item_count AS NUMERIC)/cast(a.item_count AS NUMERIC))*100,2) AS percent
  57. FROM (SELECT COUNT(barcode) AS item_count, SYSTEM FROM rogan.bought_items GROUP BY 2) a
  58. LEFT JOIN (SELECT COUNT(barcode) AS item_count, SYSTEM, circ_modifier FROM rogan.bought_items GROUP BY 2, 3) b ON b.SYSTEM = a.SYSTEM
  59. -- WHERE circ_modifier = 'VIDEO'
  60. ORDER BY 1, 3
  61. ;
  62.  
  63. select * from rogan.bought_summary;
  64.  
  65. SELECT l.SYSTEM, l.circ_modifier, l."total lost items", l.percent AS "lost percent", l."lost items" AS "lost items of circ mod",
  66. b."total acquired items", b."bought items" AS "acquired items of circ mod", b.percent AS "acquired percent"
  67. FROM (SELECT * FROM rogan.lost_summary) l
  68. JOIN (SELECT * FROM rogan.bought_summary) b ON b.SYSTEM = l.SYSTEM
  69. WHERE b.circ_modifier = l.circ_modifier AND b.circ_modifier = 'AUDIO'
  70. ;
  71.  
  72. /*
  73.  
  74. Evergreen doesn't track bills per item but per the account. So, I know that a bill is for a lost item because
  75. it is the billing type but I can't see which bill is for what lost item.  The closest I can do is I could list
  76. lost items associated with an account that also owes lost item bills.  If anything has changed that item status
  77. though it wouldn't sync up.
  78.  
  79. For this I'm also pulling the lost materials only.  If libraries charge a lost item processing fee I could also
  80. include that.  
  81.  
  82. I've included the barcodes so the accounts can be easily looked up that way but because patrons can have multiple
  83. valid barcodes I've put them into an array because otherwise it will duplicate entries for them.  
  84.  
  85. -- 'Lost Materials Processing Fee'
  86.  
  87. */
  88.  
  89. select array_agg(acard.barcode) as barcode, au.first_given_name, au.family_name, aou.shortname as library,
  90. sum(x.total_owed) as "total owed", sum(x.total_paid) as "total paid", sum(x.balance_owed) as "balance owed"
  91. from money.billing b
  92. join money.billable_xact_summary x on x.id = b.xact
  93. join actor.usr au on au.id = x.usr
  94. join actor.org_unit aou on aou.id = au.home_ou
  95. join actor.card acard on acard.usr = au.id
  96. where b.billing_type in ('Lost Materials') and aou.shortname ilike 'bcl%' and x.balance_owed > 0
  97. and acard.active = true
  98. group by au.id, au.first_given_name, au.family_name, aou.shortname
  99. order by aou.shortname, au.family_name, au.first_given_name
  100. ;
  101.  
  102. /*
  103.  
  104. Now an overview of all libraries by last fiscal year but this time including
  105.  
  106. */
  107.  
  108. select c.library, c.lost_items, b.total_owed, b.total_paid, b.balance_owed,
  109. round(b.total_paid/c.lost_items,2) as "amount recovered per item"
  110. from ( SELECT COUNT(barcode) AS lost_items, system as library FROM rogan.lost_items GROUP BY 2 ) c
  111. join (
  112.     select aou2.shortname as library,
  113.     sum(x.total_owed) as total_owed, sum(x.total_paid) as total_paid, sum(x.balance_owed) as balance_owed
  114.     from money.billing b
  115.     join money.billable_xact_summary x on x.id = b.xact
  116.     join actor.usr au on au.id = x.usr
  117.     join actor.org_unit aou on aou.id = au.home_ou
  118.     join actor.card acard on acard.usr = au.id
  119.     join actor.org_unit aou2 on aou2.id = aou.parent_ou
  120.     where b.billing_type in ('Lost Materials')and x.balance_owed > 0
  121.     and acard.active = true and date(x.xact_start) >= '2014-07-01' and date(x.xact_start) <= '2015-06-30'
  122.     group by aou2.shortname ) b on b.library = c.library
  123. order by library asc
  124. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement