From e6770c243b77df1b505dd2815feea0c802f149eb Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 24 Sep 2018 16:47:38 -0400 Subject: [PATCH] convert rollfoward to function --- schema/functions/rollforward.sql | 171 +++++++++++++++---------------- 1 file changed, 85 insertions(+), 86 deletions(-) diff --git a/schema/functions/rollforward.sql b/schema/functions/rollforward.sql index 4148262..c79503b 100644 --- a/schema/functions/rollforward.sql +++ b/schema/functions/rollforward.sql @@ -1,88 +1,87 @@ -WITH ---min and max periods -minmax AS ( - SELECT - (SELECT lower(dur) FROM evt.fspr WHERE id = '2018.08') mind - ,(SELECT lower(dur) FROM evt.fspr WHERE id = '2018.11') maxd -) ---list each period in min and max -,prng AS ( - SELECT - id - ,dur - ,prop - FROM - evt.fspr f - CROSS JOIN minmax - WHERE - lower(f.dur) >= mind - AND lower(f.dur) <= maxd -) ---get every account involved in target range -,arng AS ( - SELECT DISTINCT - acct - FROM - evt.bal b - INNER JOIN prng ON - prng.id = b.fspr -) -,bld AS ( - WITH RECURSIVE rf (acct, id, dur, propr, obal, debits, credits, cbal) AS - ( - SELECT - a.acct - ,f.id - ,f.dur - ,f.prop - ,COALESCE(b.obal,0)::numeric(12,2) - ,COALESCE(b.debits,0)::numeric(12,2) - ,COALESCE(b.credits,0)::numeric(12,2) - ,COALESCE(b.cbal,0)::numeric(12,2) +CREATE FUNCTION evt.balrf(_mind tstzrange, _maxd tstzrange) RETURNS void AS +$func$ +BEGIN + WITH + --list each period in min and max + prng AS ( + SELECT + id + ,dur + ,prop FROM - arng a - INNER JOIN evt.fspr f ON - lower(f.dur) = (SELECT mind FROM minmax) - LEFT OUTER JOIN evt.bal b ON - b.acct = a.acct - AND b.fspr = f.id - - UNION ALL - - SELECT - rf.acct - ,f.id - ,f.dur - ,f.prop - ,rf.cbal - ,COALESCE(b.debits,0)::numeric(12,2) - ,COALESCE(b.credits,0)::numeric(12,2) - ,(rf.cbal + COALESCE(b.debits,0) + COALESCE(b.credits,0))::NUMERIC(12,2) - FROM - rf - INNER JOIN evt.fspr f ON - lower(f.dur) = upper(rf.dur) - LEFT OUTER JOIN evt.bal b ON - b.acct = rf.acct - AND b.fspr = f.id + evt.fspr f WHERE - lower(f.dur) <= (SELECT maxd FROM minmax) - ) - select * from rf -) -INSERT INTO - evt.bal -SELECT - acct - ,id - ,obal - ,debits - ,credits - ,cbal -FROM - bld -ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET - obal = EXCLUDED.obal - ,debits = EXCLUDED.debits - ,credits = EXCLUDED.credits - ,cbal = EXCLUDED.cbal \ No newline at end of file + f.dur >= _mind + AND f.dur <= _maxd + ) + --get every account involved in target range + ,arng AS ( + SELECT DISTINCT + acct + FROM + evt.bal b + INNER JOIN prng ON + prng.id = b.fspr + ) + ,bld AS ( + WITH RECURSIVE rf (acct, id, dur, propr, obal, debits, credits, cbal) AS + ( + SELECT + a.acct + ,f.id + ,f.dur + ,f.prop + ,COALESCE(b.obal,0)::numeric(12,2) + ,COALESCE(b.debits,0)::numeric(12,2) + ,COALESCE(b.credits,0)::numeric(12,2) + ,COALESCE(b.cbal,0)::numeric(12,2) + FROM + arng a + INNER JOIN evt.fspr f ON + lower(f.dur) = (SELECT mind FROM minmax) + LEFT OUTER JOIN evt.bal b ON + b.acct = a.acct + AND b.fspr = f.id + + UNION ALL + + SELECT + rf.acct + ,f.id + ,f.dur + ,f.prop + ,rf.cbal + ,COALESCE(b.debits,0)::numeric(12,2) + ,COALESCE(b.credits,0)::numeric(12,2) + ,(rf.cbal + COALESCE(b.debits,0) + COALESCE(b.credits,0))::NUMERIC(12,2) + FROM + rf + INNER JOIN evt.fspr f ON + lower(f.dur) = upper(rf.dur) + LEFT OUTER JOIN evt.bal b ON + b.acct = rf.acct + AND b.fspr = f.id + WHERE + lower(f.dur) <= (SELECT maxd FROM minmax) + ) + select * from rf + ) + INSERT INTO + evt.bal + SELECT + acct + ,id + ,obal + ,debits + ,credits + ,cbal + FROM + bld + ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET + obal = EXCLUDED.obal + ,debits = EXCLUDED.debits + ,credits = EXCLUDED.credits + ,cbal = EXCLUDED.cbal; +END; +$func$ +LANGUAGE plpgsql \ No newline at end of file