Advertisement
roganhamby

ICLs as lost items

Feb 9th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- books lost to other libraries by checkout
  2.  
  3. -- first let's get a list of what books have been checked out and gone to lost
  4.  
  5. select * from config.copy_status;
  6. -- 3 is lost
  7.  
  8. select count(id) from asset.copy where status = 3;
  9. -- 125,705
  10.  
  11. -- now we need to see if these went lost on checkouts to other library systems  
  12.  
  13.  
  14. select count(circ.id)
  15. from action.circulation circ
  16. join asset.copy ac on ac.id = circ.target_copy
  17. join actor.org_unit copychild on copychild.id = ac.circ_lib
  18. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  19. join actor.org_unit circchild on circchild.id = circ.circ_lib
  20. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  21. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  22. ;
  23.  
  24. -- 1,230 went to lost while checked out to another library system than who owned the book in 2013
  25.  
  26. -- so, how many were later found?
  27.  
  28. select count(circ.id)
  29. from action.circulation circ
  30. join asset.copy ac on ac.id = circ.target_copy
  31. join actor.org_unit copychild on copychild.id = ac.circ_lib
  32. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  33. join actor.org_unit circchild on circchild.id = circ.circ_lib
  34. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  35. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  36. and ac.status not in (0,6,1,8)
  37. ;
  38.  
  39. -- 881
  40. -- so, 1230 - 881 = 349 so that suggests that 349 items came back into use in some way but there could be wierdness
  41. -- let's look at those explicitly still lost
  42.  
  43. select count(circ.id)
  44. from action.circulation circ
  45. join asset.copy ac on ac.id = circ.target_copy
  46. join actor.org_unit copychild on copychild.id = ac.circ_lib
  47. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  48. join actor.org_unit circchild on circchild.id = circ.circ_lib
  49. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  50. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  51. and ac.status = 3
  52. ;
  53.  
  54. -- 863, a difference of 18 only a 2% difference from 881
  55. -- so of those lost of 1,230 881 end up still lost by Feb so 72% stay lost and 28% come back still
  56.  
  57. -- now let's break it down by system of owning material
  58.  
  59. select count(circ.id), copyparent.shortname as "copy_owner"
  60. from action.circulation circ
  61. join asset.copy ac on ac.id = circ.target_copy
  62. join actor.org_unit copychild on copychild.id = ac.circ_lib
  63. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  64. join actor.org_unit circchild on circchild.id = circ.circ_lib
  65. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  66. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  67. and ac.status not in (0,6,1,8)
  68. group by 2
  69. order by 2 asc
  70. ;
  71.  
  72. -- of who lost what
  73.  
  74.  
  75. select count(circ.id), circparent.shortname as "library_of_losing_patron"
  76. from action.circulation circ
  77. join asset.copy ac on ac.id = circ.target_copy
  78. join actor.org_unit copychild on copychild.id = ac.circ_lib
  79. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  80. join actor.org_unit circchild on circchild.id = circ.circ_lib
  81. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  82. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  83. and ac.status not in (0,6,1,8)
  84. group by 2
  85. order by 2 asc
  86. ;
  87.  
  88. -- both?
  89.  
  90. select count(circ.id), copyparent.shortname as "copy_owner", circparent.shortname as "library_of_losing_patron"
  91. from action.circulation circ
  92. join asset.copy ac on ac.id = circ.target_copy
  93. join actor.org_unit copychild on copychild.id = ac.circ_lib
  94. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  95. join actor.org_unit circchild on circchild.id = circ.circ_lib
  96. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  97. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  98. and ac.status not in (0,6,1,8)
  99. group by 2, 3
  100. order by 2, 3 ASC
  101. ;
  102.  
  103. -- now, let's see if I can get SQL to do the rest of the work instead of me doing it manaully
  104. -- I could do this next step as as a subquery but I'm lazy and rather than waiting for the big search each time incase I do a lot of tweaking
  105. -- I'm going to make a temp table of it which will make things super fast
  106.  
  107.  
  108. create table rogan.icls_lost as
  109. select count(circ.id) as "lost_count", copyparent.shortname as "owning_lib", circparent.shortname as "circ_lib"
  110. from action.circulation circ
  111. join asset.copy ac on ac.id = circ.target_copy
  112. join actor.org_unit copychild on copychild.id = ac.circ_lib
  113. join actor.org_unit copyparent on copyparent.id = copychild.parent_ou
  114. join actor.org_unit circchild on circchild.id = circ.circ_lib
  115. join actor.org_unit circparent on circparent.id = circchild.parent_ou
  116. where copyparent.id != circparent.id and date(circ.xact_start) >= '2013-01-01' and date(circ.xact_start) <= '2013-12-31' and circ.stop_fines = 'LOST'
  117. and ac.status not in (0,6,1,8)
  118. group by 2, 3
  119. order by 2, 3 ASC
  120. ;
  121.  
  122. -- time for the seldom (for me anywya) (in evergreen) used cross join
  123.  
  124. -- let's match them up now
  125.  
  126. select lost.lost_count as "a_lost_count", lost.owning_lib as "a_owning_lib", lost.circ_lib as "a_circ_lib",
  127. owning.lost_count as "b_lost_count", owning.owning_lib as "b.owning_lib", owning.circ_lib as "b.circ_lib"
  128. from rogan.icls_lost lost
  129. cross join rogan.icls_lost owning
  130. where owning.owning_lib = lost.circ_lib and owning.circ_lib = lost.owning_lib
  131. ;
  132.  
  133. -- now let's do math
  134.  
  135. select (lost.lost_count - owning.lost_count) as "sum_count", lost.owning_lib as "a_owning_lib", lost.circ_lib as "a_circ_lib",
  136. owning.owning_lib as "b.owning_lib", owning.circ_lib as "b.circ_lib"
  137. from rogan.icls_lost lost
  138. cross join rogan.icls_lost owning
  139. where owning.owning_lib = lost.circ_lib and owning.circ_lib = lost.owning_lib
  140. ;
  141.  
  142. -- now notice that the values will a variety of real numbers, positves, negatives and zeros
  143. -- and a value like 9 will have an inverse pair with a value of -9, let's get rid of these and the zeros.  The zeros are literally zeroed out
  144. -- so we only care about negative or postive values and both describe the same relationship from different vectors
  145. -- so we can arbitrarily pick either
  146.  
  147.  
  148. select (lost.lost_count - owning.lost_count) as "sum_count", lost.owning_lib as "a_owning_lib", lost.circ_lib as "a_circ_lib",
  149. owning.owning_lib as "b.owning_lib", owning.circ_lib as "b.circ_lib"
  150. from rogan.icls_lost lost
  151. cross join rogan.icls_lost owning
  152. where owning.owning_lib = lost.circ_lib and owning.circ_lib = lost.owning_lib
  153. group by 1, 2, 3, 4, 5
  154. having (lost.lost_count - owning.lost_count) > 0
  155. ;
  156.  
  157. -- let's do more cleanup
  158.  
  159. select (lost.lost_count - owning.lost_count) as "sum_count", lost.owning_lib as "owning_lib", lost.circ_lib as "library_of_losing_patron"
  160. from rogan.icls_lost lost
  161. cross join rogan.icls_lost owning
  162. where owning.owning_lib = lost.circ_lib and owning.circ_lib = lost.owning_lib
  163. group by 1, 2, 3
  164. having (lost.lost_count - owning.lost_count) > 0
  165. order by 2, 3 asc
  166. ;
  167.  
  168.  
  169. select * from rogan.icls_lost;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement