]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/extend-reporter.sql
LP#1917826: add release notes entry
[Evergreen.git] / Open-ILS / src / sql / Pg / extend-reporter.sql
1 /*
2  * Copyright (C) 2008  Equinox Software, Inc.
3  * Mike Rylander <miker@esilibrary.com> 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 DROP SCHEMA IF EXISTS extend_reporter CASCADE;
18
19 BEGIN;
20
21 CREATE SCHEMA extend_reporter;
22
23 CREATE TABLE extend_reporter.legacy_circ_count (
24     id          BIGINT   PRIMARY KEY, --  REFERENCES asset.copy (id) DEFERRABLE INITIALLY DEFERRED, -- XXX could be an serial.issuance
25     circ_count  INT         NOT NULL DEFAULT 0
26 );
27
28 CREATE OR REPLACE VIEW extend_reporter.full_circ_count AS
29    SELECT cp.id,
30    COALESCE((SELECT circ_count FROM extend_reporter.legacy_circ_count WHERE id = cp.id), 0)
31    + (SELECT COUNT(*) FROM action.circulation WHERE target_copy = cp.id)
32    + (SELECT COUNT(*) FROM action.aged_circulation WHERE target_copy = cp.id) AS circ_count
33    FROM asset.copy cp;
34
35 CREATE OR REPLACE VIEW extend_reporter.global_bibs_by_holding_update AS
36   SELECT DISTINCT ON (id) id, holding_update, update_type
37     FROM (SELECT  b.id,
38                   LAST(cp.create_date) AS holding_update,
39                   'add' AS update_type
40             FROM  biblio.record_entry b
41                   JOIN asset.call_number cn ON (cn.record = b.id)
42                   JOIN asset.copy cp ON (cp.call_number = cn.id)
43             WHERE NOT cp.deleted
44                   AND b.id > 0
45             GROUP BY b.id
46               UNION
47           SELECT  b.id,
48                   LAST(cp.edit_date) AS holding_update,
49                   'delete' AS update_type
50             FROM  biblio.record_entry b
51                   JOIN asset.call_number cn ON (cn.record = b.id)
52                   JOIN asset.copy cp ON (cp.call_number = cn.id)
53             WHERE cp.deleted
54                   AND b.id > 0
55             GROUP BY b.id)x
56     ORDER BY id, holding_update;
57
58 CREATE OR REPLACE VIEW extend_reporter.copy_count_per_org AS
59  SELECT acn.record AS bibid,
60     ac.circ_lib,
61     acn.owning_lib,
62     max(ac.edit_date) AS last_edit_time,
63     min(ac.deleted::integer) AS has_only_deleted_copies,
64     count(
65         CASE
66             WHEN ac.deleted THEN ac.id
67             ELSE NULL::bigint
68         END) AS deleted_count,
69     count(
70         CASE
71             WHEN NOT ac.deleted THEN ac.id
72             ELSE NULL::bigint
73         END) AS visible_count,
74     count(*) AS total_count
75    FROM asset.call_number acn,
76     asset.copy ac
77   WHERE ac.call_number = acn.id
78   GROUP BY acn.record, acn.owning_lib, ac.circ_lib;
79
80 COMMIT;