This commit is contained in:
Paul Trowbridge 2018-09-24 00:02:41 -04:00
parent cdef822870
commit 1c1f86fb3c
2 changed files with 87 additions and 107 deletions

View File

@ -194,78 +194,62 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,fspr ,fspr
) )
,ins AS ( ,ins AS (
INSERT INTO
evt.bal
SELECT SELECT
acct acct
,fspr ,fspr
,0 obal
,debits ,debits
,credits ,credits
,debits + credits
FROM FROM
agg agg
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET )
debits = evt.bal.debits + EXCLUDED.debits ,list AS (
,credits = evt.bal.credits + EXCLUDED.credits SELECT
,cbal = evt.bal.cbal + EXCLUDED.debits + EXCLUDED.credits acct
RETURNING * ,min(lower(dur)) minp
,max(lower(dur)) maxp
FROM
ins b
INNER JOIN evt.fspr f ON
f.id = b.fspr
GROUP BY
acct
) )
,seq AS ( ,seq AS (
WITH RECURSIVE rf (acct, fspr, minrange, maxrange, dur, id, obal, debits, credits, cbal) AS WITH RECURSIVE rf (acct, minp, maxp, id, dur, obal, debits, credits, cbal) AS
( (
SELECT SELECT
rng.acct list.acct
,rng.fspr ,list.minp
,rng.minrange ,list.maxp
,rng.maxrange
,f.dur
,f.id ,f.id
,b.obal::numeric(12,2)
,b.debits::numeric(12,2)
,b.credits::numeric(12,2)
,b.cbal::numeric(12,2)
FROM
(
--for each item determine if a gap exists between new an previous period (if any)
SELECT
ins.acct
,ins.fspr
,lower(f.dur) dur
,CASE WHEN lower(f.dur) > max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END minrange
,CASE WHEN lower(f.dur) < max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END maxrange
FROM
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
GROUP BY
ins.acct
,ins.fspr
,f.dur ,f.dur
) rng ,COALESCE(b.obal::numeric(12,2),0)
,COALESCE(b.debits::numeric(12,2),0) + COALESCE(ins.debits,0)
,COALESCE(b.credits::numeric(12,2),0) + COALESCE(ins.credits,0)
,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0)
FROM
list
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
lower(f.dur) = minrange upper(f.dur) = list.minp
INNER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rng.acct b.acct = list.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON
ins.acct = list.acct
AND ins.fspr = f.id
UNION ALL UNION ALL
SELECT SELECT
rf.acct rf.acct
,rf.fspr ,rf.minp
,rf.minrange ,rf.maxp
,rf.maxrange
,f.dur
,f.id ,f.id
,f.dur
,COALESCE(rf.cbal,0)::numeric(12,2) ,COALESCE(rf.cbal,0)::numeric(12,2)
,COALESCE(b.debits,0)::numeric(12,2) ,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(ins.debits,0)
,COALESCE(b.credits,0)::numeric(12,2) ,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(ins.credits,0)
,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) ,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0)
FROM FROM
rf rf
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -273,13 +257,16 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rf.acct b.acct = rf.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON
ins.acct = rf.acct
AND ins.fspr = f.id
WHERE WHERE
lower(f.dur) <= rf.maxrange lower(f.dur) <= rf.maxp
) )
SELECT * FROM rf SELECT * FROM rf WHERE lower(dur) >= minp
) )
INSERT INTO INSERT INTO
evt.balx (acct, fspr, obal, debits, credits, cbal) evt.bal (acct, fspr, obal, debits, credits, cbal)
SELECT SELECT
acct acct
,id ,id
@ -288,7 +275,12 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,credits ,credits
,cbal ,cbal
FROM FROM
seq; seq
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
obal = EXCLUDED.obal
,debits = EXCLUDED.debits
,credits = EXCLUDED.credits
,cbal = EXCLUDED.cbal;
RETURN NULL; RETURN NULL;
END; END;
$func$; $func$;

View File

@ -19,77 +19,62 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,fspr ,fspr
) )
,ins AS ( ,ins AS (
INSERT INTO
evt.bal
SELECT SELECT
acct acct
,fspr ,fspr
,0 obal
,debits ,debits
,credits ,credits
,debits + credits
FROM FROM
agg agg
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET )
debits = evt.bal.debits + EXCLUDED.debits ,list AS (
,credits = evt.bal.credits + EXCLUDED.credits SELECT
,cbal = evt.bal.cbal + EXCLUDED.debits + EXCLUDED.credits; acct
,min(lower(dur)) minp
,max(lower(dur)) maxp
FROM
ins b
INNER JOIN evt.fspr f ON
f.id = b.fspr
GROUP BY
acct
) )
,seq AS ( ,seq AS (
WITH RECURSIVE rf (acct, fspr, minrange, maxrange, dur, id, obal, debits, credits, cbal) AS WITH RECURSIVE rf (acct, minp, maxp, id, dur, obal, debits, credits, cbal) AS
( (
SELECT SELECT
rng.acct list.acct
,rng.fspr ,list.minp
,rng.minrange ,list.maxp
,rng.maxrange
,f.dur
,f.id ,f.id
,b.obal::numeric(12,2)
,b.debits::numeric(12,2)
,b.credits::numeric(12,2)
,b.cbal::numeric(12,2)
FROM
(
--for each item determine if a gap exists between new an previous period (if any)
SELECT
ins.acct
,ins.fspr
,lower(f.dur) dur
,CASE WHEN lower(f.dur) > max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END minrange
,CASE WHEN lower(f.dur) < max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END maxrange
FROM
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
GROUP BY
ins.acct
,ins.fspr
,f.dur ,f.dur
) rng ,COALESCE(b.obal::numeric(12,2),0)
,COALESCE(b.debits::numeric(12,2),0) + COALESCE(ins.debits,0)
,COALESCE(b.credits::numeric(12,2),0) + COALESCE(ins.credits,0)
,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0)
FROM
list
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
lower(f.dur) = minrange upper(f.dur) = list.minp
INNER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rng.acct b.acct = list.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON
ins.acct = list.acct
AND ins.fspr = f.id
UNION ALL UNION ALL
SELECT SELECT
rf.acct rf.acct
,rf.fspr ,rf.minp
,rf.minrange ,rf.maxp
,rf.maxrange
,f.dur
,f.id ,f.id
,f.dur
,COALESCE(rf.cbal,0)::numeric(12,2) ,COALESCE(rf.cbal,0)::numeric(12,2)
,COALESCE(b.debits,0)::numeric(12,2) ,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(ins.debits,0)
,COALESCE(b.credits,0)::numeric(12,2) ,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(ins.credits,0)
,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) ,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0)
FROM FROM
rf rf
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -97,10 +82,13 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rf.acct b.acct = rf.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON
ins.acct = rf.acct
AND ins.fspr = f.id
WHERE WHERE
lower(f.dur) <= rf.maxrange lower(f.dur) <= rf.maxp
) )
SELECT * FROM rf SELECT * FROM rf WHERE lower(dur) >= minp
) )
INSERT INTO INSERT INTO
evt.bal (acct, fspr, obal, debits, credits, cbal) evt.bal (acct, fspr, obal, debits, credits, cbal)