Advertisement
roganhamby

SQL Workshop Evergreen Conference 2015

May 14th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 1 Cup of Coffee
  3.  
  4. These reports are straightforward without difficult transforms or complicated joins but may require
  5. you to be a little more awake to notice the (in hindsight sometimes) obvious.
  6.  
  7. "untargeted" holds -- i.e. records/items with holds on them where all copies have gone
  8. lost/missing/claimed returned/etc. Ideally run regularly so we can promptly follow up with ILL or replacement.
  9.  
  10. Reports making use of local stat cats & their variables
  11.  
  12. Extract circulation stats for a specific month/year
  13. */
  14.  
  15. select count(acirc.id) as Circulation, extract(year from acirc.xact_start) as Year,
  16. acl.name as "Copy Location"
  17. from action.circulation acirc
  18. join asset.copy ac on ac.id = acirc.target_copy
  19. join asset.copy_location acl on acl.id = ac.location
  20. where ac.circ_lib = 127
  21. group by 2, 3
  22. ;
  23.  
  24. select count(aih.id), ssr.author, ssr.title
  25. from action.in_house_use aih
  26. join asset.copy ac on ac.id = aih.item
  27. join asset.call_number acn on acn.id = ac.call_number
  28. join reporter.super_simple_record ssr on ssr.id = acn.record
  29. where date(aih.use_time) >= '2015-01-01'
  30. group by 2, 3
  31. ;
  32.  
  33. select ac.*
  34. from asset.copy ac
  35. left join action.circulation acirc on acirc.target_copy = ac.id
  36. where date(ac.create_date) >= now() - interval '1 year'
  37. and date(ac.create_date) <= now() - interval '6 months'
  38. and acirc.id is null
  39. limit 100
  40. ;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. select * from action.hold_request limit 10;
  52.  
  53. select ac.barcode as "Item's Barcode"
  54. from action.hold_request ahr
  55. join asset.copy ac on ac.id = ahr.target
  56. where ahr.fulfillment_time is null and ahr.capture_time is null
  57. and ahr.hold_type = 'C' and ac.status in (101,14,13,16,4)
  58. and ahr.cancel_time is null
  59. limit 10
  60. ;
  61.  
  62. UNION ALL
  63. select ac.barcode
  64. from action.hold_request ahr
  65. join biblio.record_entry bre on bre.id = ahr.target
  66. join asset.call_number acn on acn.record = bre.id
  67. join asset.copy ac on ac.call_number = acn.id
  68. where ahr.fulfillment_time is null and ahr.capture_time is null
  69. and ahr.hold_type = 'T' and ac.status in (101,14,13,16,4)
  70. and ahr.cancel_time is null
  71. ;
  72.  
  73.  
  74.  
  75. select * from config.copy_status;
  76.  
  77.  
  78.  
  79. /*
  80. Will Need Edits
  81.  
  82. Now we get into the reports that require a bit more planning and we may find ourselves going back and tweaking a fair bit.
  83.  
  84. Purchase alert report *
  85. Compare number of active holds on a title to number of holdable items on same title**
  86. Identify active holds that have remained unfilled despite newer holds on the same title being filled
  87. Weeding report *
  88.  
  89. * These two reports are one that I have well traveled versions of.  We will probably do these and I'll start with
  90. the planning and though process but part way skip and show my completed versions and how they work.  
  91. ** This is really a subset of the purchase alert but I'll show how it can be looked at a little differently if
  92. the crowd is interested.
  93. */
  94.  
  95.  
  96.  
  97. SELECT bd.bib_id, parent.shortname AS "org unit", bd.title, bd.author, bd.isbn, SUM(bd.copies) AS copies, SUM(bd.holds_count) AS "holds count",
  98. ROUND(SUM(bd.holds_count)/SUM(bd.copies)) AS "holds count per copy"
  99.  FROM (
  100.         SELECT all_holds.bib_id, aou.id AS org, rmsr.title, rmsr.author, rmsr.isbn,
  101.         COUNT(DISTINCT ac.id) AS "copies", COUNT(all_holds.bib_id)/COUNT(DISTINCT ac.id) AS "holds_count"
  102.         FROM ((
  103.         SELECT hold_request.target, hold_request.pickup_lib
  104.         FROM ACTION.hold_request
  105.         WHERE hold_request.hold_type = 'T'::TEXT
  106.         AND hold_request.fulfillment_time IS NULL
  107.         AND hold_request.cancel_time IS NULL
  108.         UNION ALL
  109.         SELECT bre.id, ahr.pickup_lib
  110.         FROM ACTION.hold_request ahr
  111.         JOIN asset.call_number acn ON ahr.target = acn.id
  112.         JOIN biblio.record_entry bre ON acn.record = bre.id
  113.         WHERE ahr.hold_type = 'V'::TEXT
  114.         AND ahr.fulfillment_time IS NULL
  115.         AND ahr.cancel_time IS NULL)
  116.         UNION ALL
  117.         SELECT bre.id, ahr.pickup_lib
  118.         FROM ACTION.hold_request ahr
  119.         JOIN asset.COPY ac ON ahr.target = ac.id
  120.         JOIN asset.call_number acn ON ac.call_number = acn.id
  121.         JOIN biblio.record_entry bre ON acn.record = bre.id
  122.         WHERE ahr.hold_type = 'C'::TEXT
  123.         AND ahr.fulfillment_time IS NULL
  124.         AND ahr.cancel_time IS NULL)
  125.         all_holds(bib_id, pickup_lib)
  126.         JOIN reporter.materialized_simple_record rmsr ON rmsr.id = all_holds.bib_id
  127.         JOIN actor.org_unit aou ON aou.id = all_holds.pickup_lib
  128.         JOIN asset.call_number acn ON acn.record = all_holds.bib_id
  129.         JOIN asset.COPY ac ON ac.call_number = acn.id
  130.         WHERE ac.circ_lib = all_holds.pickup_lib
  131.         GROUP BY 1, 2, 3, 4, 5
  132.         HAVING COUNT(all_holds.bib_id)/COUNT(DISTINCT ac.id) > 3
  133. ) bd
  134. JOIN actor.org_unit child ON child.id = bd.org
  135. JOIN actor.org_unit parent ON parent.id = child.parent_ou
  136. GROUP BY 1, 2, 3, 4, 5
  137. ORDER BY 2 ASC, 5 DESC
  138. ;
  139.  
  140. select *
  141. from metabib.real_full_rec mrf
  142. where mrf.tag = '856' and mrf.value ilike '%loc.gov%'
  143. limit 50
  144. ;
  145. SELECT SUM(a.copy_id) AS "Videos", a.org2_shortname
  146. FROM (
  147.         SELECT COUNT(COPY.id) AS copy_id, org2.shortname AS org2_shortname
  148.         FROM asset.COPY COPY
  149.         JOIN actor.org_unit org ON org.id = COPY.circ_lib
  150.         JOIN actor.org_unit org2 ON org2.id = org.parent_ou
  151.         JOIN asset.call_number acn ON acn.id = COPY.call_number
  152.         LEFT OUTER JOIN metabib.real_full_rec meta ON meta.record = acn.record
  153.         WHERE COPY.deleted = FALSE AND date(COPY.create_date) <= '2013-06-30'
  154.         AND meta.tag = 'LDR' AND SUBSTRING(meta.VALUE FROM 7 FOR 1) = 'g'
  155.         GROUP BY 2
  156. UNION ALL
  157.         SELECT COUNT(COPY.id) AS copy_id, org2.shortname AS org2_shortname
  158.         FROM asset.COPY COPY
  159.         JOIN actor.org_unit org ON org.id = COPY.circ_lib
  160.         JOIN actor.org_unit org2 ON org2.id = org.parent_ou
  161.         JOIN asset.call_number acn ON acn.id = COPY.call_number
  162.         LEFT OUTER JOIN metabib.real_full_rec meta ON meta.record = acn.record
  163.         WHERE COPY.deleted = TRUE AND date(COPY.create_date) <= '2013-06-30'
  164.         AND date(COPY.edit_date) >= '2012-07-01' AND date(COPY.edit_date) <= '2013-06-30'
  165.         AND meta.tag = 'LDR' AND SUBSTRING(meta.VALUE FROM 7 FOR 1) = 'g'
  166.         GROUP BY 2
  167. ) a
  168. GROUP BY 2
  169. ORDER BY 2
  170. ;
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /*
  178. Pretend they didn't ask
  179.  
  180. billing reports
  181. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement