Advertisement
roganhamby

Dispersal SQL Report

Jan 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. -- dispersal report SQL version
  3. -- working on a variation off weeding
  4.  
  5. -- what's different from a weeding in part is that the items aren't actively circulating here anymore
  6. -- so we want things that haven't circulated in the last 6 months rather than three years
  7.  
  8. -- instead of all we also want a set number of them and to order by how recently they had activity
  9. -- so as to get slightly more recent items
  10.  
  11. -- very simple, superceded by the dispersal postgres functions
  12. SELECT * FROM (
  13.         SELECT COUNT(acirc.id), ac.id AS copy_id, ac.barcode AS "barcode", acl.NAME AS "shelving location",
  14.         acn.label AS "call number", msr.title, MAX(acirc.xact_start) AS "last circulation"
  15.         FROM asset.COPY ac
  16.         JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  17.         LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  18.         JOIN asset.call_number acn ON acn.id = ac.call_number
  19.         JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  20.         JOIN actor.org_unit child ON child.id = ac.circ_lib
  21.         JOIN actor.org_unit parent ON parent.id = child.parent_ou
  22.         WHERE acl.NAME ILIKE 'adult large print'
  23.         AND acirc.xact_start IS NULL
  24.         AND date(ac.create_date) <= NOW() - INTERVAL '6 months'
  25.         AND child.shortname = 'YCL-RH'
  26.         AND ac.deleted = FALSE
  27.         GROUP BY 2, 3, 4, 5, 6
  28.         UNION ALL
  29.         SELECT COUNT(acirc.id), ac.id AS copy_id, ac.barcode AS "barcode", acl.NAME AS "shelving location",
  30.         acn.label AS "call number", msr.title, MAX(acirc.xact_start) AS "last circulation"
  31.         FROM asset.COPY ac
  32.         JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  33.         LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  34.         JOIN asset.call_number acn ON acn.id = ac.call_number
  35.         JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  36.         JOIN actor.org_unit child ON child.id = ac.circ_lib
  37.         JOIN actor.org_unit parent ON parent.id = child.parent_ou
  38.         WHERE acl.NAME ILIKE 'adult large print'
  39.         AND date(ac.create_date) <= NOW() - INTERVAL '6 months'
  40.         AND child.shortname = 'YCL-RH'
  41.         AND ac.deleted = FALSE
  42.         GROUP BY 2, 3, 4, 5, 6
  43.         HAVING MAX(acirc.xact_start) < NOW() - INTERVAL '6 months'
  44.         ORDER BY 4
  45. ) disperse ORDER BY "last circulation" DESC LIMIT 200
  46. ;
  47.  
  48.  
  49. -- rewritten to account for different circ libraries within a single shelving location
  50. -- not needed long term 2015/02/04
  51.  
  52. SELECT * FROM (
  53.         SELECT COUNT(acirc.id), ac.id AS copy_id, ac.barcode AS "barcode", acl.NAME AS "shelving location",
  54.         acn.label AS "call number", msr.title, MAX(acirc.xact_start) AS "last circulation"
  55.         FROM asset.COPY ac
  56.         JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  57.         LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  58.         JOIN asset.call_number acn ON acn.id = ac.call_number
  59.         JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  60.         JOIN actor.org_unit child ON child.id = ac.circ_lib
  61.         JOIN actor.org_unit parent ON parent.id = child.parent_ou
  62.         WHERE acl.NAME ILIKE 'adult large print'
  63.         AND acirc.xact_start IS NULL
  64.         AND date(ac.create_date) <= NOW() - INTERVAL '6 months'
  65.         AND child.shortname = 'YCL-RH'
  66.         AND ac.deleted = FALSE
  67.         AND ac.circ_lib = 127
  68.         GROUP BY 2, 3, 4, 5, 6
  69.         UNION ALL
  70.         SELECT COUNT(acirc.id), ac.id AS copy_id, ac.barcode AS "barcode", acl.NAME AS "shelving location",
  71.         acn.label AS "call number", msr.title, MAX(acirc.xact_start) AS "last circulation"
  72.         FROM asset.COPY ac
  73.         JOIN asset.copy_location acl ON ac.LOCATION = acl.id
  74.         LEFT JOIN ACTION.circulation acirc ON acirc.target_copy = ac.id
  75.         JOIN asset.call_number acn ON acn.id = ac.call_number
  76.         JOIN reporter.materialized_simple_record msr ON msr.id = acn.record
  77.         JOIN actor.org_unit child ON child.id = ac.circ_lib
  78.         JOIN actor.org_unit parent ON parent.id = child.parent_ou
  79.         WHERE acl.NAME ILIKE 'adult large print'
  80.         AND date(ac.create_date) <= NOW() - INTERVAL '6 months'
  81.         AND child.shortname = 'YCL-RH'
  82.         AND ac.deleted = FALSE
  83.         AND ac.circ_lib = 127
  84.         GROUP BY 2, 3, 4, 5, 6
  85.         HAVING MAX(acirc.xact_start) < NOW() - INTERVAL '6 months'
  86.         ORDER BY 4
  87. ) disperse ORDER BY "last circulation" DESC LIMIT 50
  88. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement