]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/extend-reporter.sql
Stamping upgrade for truncate-to-max-fine
[working/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, COALESCE(c.circ_count, 0::bigint) + COALESCE(count(DISTINCT circ.id), 0::bigint) + COALESCE(count(DISTINCT acirc.id), 0::bigint) AS circ_count
30    FROM asset."copy" cp
31    LEFT JOIN extend_reporter.legacy_circ_count c USING (id)
32    LEFT JOIN "action".circulation circ ON circ.target_copy = cp.id
33    LEFT JOIN "action".aged_circulation acirc ON acirc.target_copy = cp.id
34   GROUP BY cp.id, c.circ_count;
35
36 CREATE OR REPLACE VIEW extend_reporter.global_bibs_by_holding_update AS
37   SELECT DISTINCT ON (id) id, holding_update, update_type
38     FROM (SELECT  b.id,
39                   LAST(cp.create_date) AS holding_update,
40                   'add' AS update_type
41             FROM  biblio.record_entry b
42                   JOIN asset.call_number cn ON (cn.record = b.id)
43                   JOIN asset.copy cp ON (cp.call_number = cn.id)
44             WHERE NOT cp.deleted
45                   AND b.id > 0
46             GROUP BY b.id
47               UNION
48           SELECT  b.id,
49                   LAST(cp.edit_date) AS holding_update,
50                   'delete' AS update_type
51             FROM  biblio.record_entry b
52                   JOIN asset.call_number cn ON (cn.record = b.id)
53                   JOIN asset.copy cp ON (cp.call_number = cn.id)
54             WHERE cp.deleted
55                   AND b.id > 0
56             GROUP BY b.id)x
57     ORDER BY id, holding_update;
58         
59 COMMIT;
60