2 * Copyright (C) 2008 Equinox Software, Inc.
3 * Mike Rylander <miker@esilibrary.com>
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.
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.
17 DROP SCHEMA IF EXISTS extend_reporter CASCADE;
21 CREATE SCHEMA extend_reporter;
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
28 CREATE OR REPLACE VIEW extend_reporter.full_circ_count AS
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
35 CREATE OR REPLACE VIEW extend_reporter.global_bibs_by_holding_update AS
36 SELECT DISTINCT ON (id) id, holding_update, update_type
38 LAST(cp.create_date) AS holding_update,
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)
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)
56 ORDER BY id, holding_update;
58 CREATE OR REPLACE VIEW extend_reporter.copy_count_per_org AS
59 SELECT acn.record AS bibid,
62 max(ac.edit_date) AS last_edit_time,
63 min(ac.deleted::integer) AS has_only_deleted_copies,
66 WHEN ac.deleted THEN ac.id
68 END) AS deleted_count,
71 WHEN NOT ac.deleted THEN ac.id
73 END) AS visible_count,
74 count(*) AS total_count
75 FROM asset.call_number acn,
77 WHERE ac.call_number = acn.id
78 GROUP BY acn.record, acn.owning_lib, ac.circ_lib;