From 8102b007acf0bcd0ecd126d8896b0e137b5c0858 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 21 Sep 2018 00:39:25 -0400 Subject: [PATCH] start work on rolling forward account balances --- schema/triggers/bal_insert.sql | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 schema/triggers/bal_insert.sql diff --git a/schema/triggers/bal_insert.sql b/schema/triggers/bal_insert.sql new file mode 100644 index 0000000..ec0b204 --- /dev/null +++ b/schema/triggers/bal_insert.sql @@ -0,0 +1,41 @@ +---------------------------handle new gl lines---------------------------------------- + +CREATE OR REPLACE FUNCTION evt.bal_insert() RETURNS trigger + LANGUAGE plpgsql + AS + $func$ + BEGIN + WITH + --incoming new accounts and any other periods used for the same accounts + rng AS ( + --for each item determine if a gap exists between new an previous period (if any) + SELECT + ins.acct + ,ins.fspr + ,lower(f.dur) dur + ,max(lower(bp.dur)) maxp + ,min(lower(bp.dur)) minp + FROM + evt.bal ins + INNER JOIN evt.fspr f ON + f.id = ins.fspr + LEFT OUTER JOIN evt.bal b ON + b.acct = ins.acct + LEFT OUTER JOIN evt.fspr bp ON + bp.id = b.fspr + WHERE ins.fspr = '2018.11' + GROUP BY + ins.acct + ,ins.fspr + ,f.dur + ) + select + RETURN NULL; + END; + $func$; + +CREATE TRIGGER bal_insert + AFTER INSERT ON evt.bal + REFERENCING NEW TABLE AS ins + FOR EACH STATEMENT + EXECUTE PROCEDURE evt.bal_insert(); \ No newline at end of file