Advertisement
cmptrwz

Record Stats Query

Feb 25th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Note: Assuming that the 6 months parts are pulled from a setting of some sort, and not hardcoded or anything.
  2. SELECT
  3.     -- For grouping and record identification
  4.     bre.id,
  5.     -- For use in activity metric work to be done later
  6.     count(DISTINCT CASE WHEN p.relname = 'copy' THEN acp.id ELSE NULL END) AS copies,
  7.     count(DISTINCT CASE WHEN p.relname = 'unit' THEN acp.id ELSE NULL END) AS serials,
  8.     count(DISTINCT CASE WHEN acp.holdable AND acl.holdable AND ccs.holdable THEN acp.id ELSE NULL END) as holdable,
  9.     count(DISTINCT CASE WHEN acp.circulate AND acl.circulate THEN acp.id ELSE NULL END) as circulatable,
  10.     -- Circs could be on deleted copies, but we aren't including those here
  11.     count(DISTINCT CASE WHEN (ac.stop_fines IS NULL OR ac.stop_fines IN ('MAXFINES')) AND ac.xact_finish IS NULL AND ac.checkin_time IS NULL THEN ac.id ELSE NULL END) AS open_circs,
  12.     count(DISTINCT CASE WHEN ac.xact_start > NOW() - '6 months'::INTERVAL THEN ac.id ELSE NULL END)
  13.         + count(DISTINCT CASE WHEN aac.xact_start > NOW() - '6 months'::INTERVAL THEN aac.id ELSE NULL END) AS aged_circs,
  14.     count(DISTINCT ac.id) + count(DISTINCT aac.id) AS all_circs,
  15.     -- For actual attempts at some visibility speedups. Note the sub-selects are for making NULLs go away, array_agg doesn't toss them out.
  16.     COALESCE((SELECT ARRAY_AGG(x) FROM UNNEST(ARRAY_AGG(DISTINCT acp.circ_lib)) AS x WHERE x IS NOT NULL), ARRAY[]::INT[]) AS all_copy_libs,
  17.     COALESCE((SELECT ARRAY_AGG(x) FROM UNNEST(ARRAY_AGG(DISTINCT CASE WHEN acp.opac_visible AND ccs.opac_visible AND acl.opac_visible AND aou.opac_visible THEN acp.circ_lib ELSE NULL END)) AS x WHERE x IS NOT NULL), ARRAY[]::INT[]) AS opac_copy_libs
  18. FROM asset.copy acp
  19.     JOIN asset.call_number acn ON acp.call_number = acn.id
  20.     JOIN config.copy_status ccs ON acp.status = ccs.id
  21.     JOIN asset.copy_location acl ON acp.location = acl.id
  22.     JOIN actor.org_unit aou ON acp.circ_lib = aou.id
  23.     JOIN pg_class p ON acp.tableoid = p.oid
  24.     -- Join against action.circulation and action.aged_circulation instead of using the view for speed reasons
  25.     LEFT JOIN action.circulation ac ON acp.id = ac.target_copy
  26.     LEFT JOIN action.aged_circulation aac ON acp.id = aac.target_copy
  27.     RIGHT JOIN biblio.record_entry bre ON acn.record = bre.id
  28. WHERE
  29.     (acp IS NULL OR NOT acp.deleted)
  30. GROUP BY bre.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement