convert rollfoward to function
This commit is contained in:
parent
57d9f08699
commit
e6770c243b
@ -1,88 +1,87 @@
|
|||||||
WITH
|
CREATE FUNCTION evt.balrf(_mind tstzrange, _maxd tstzrange) RETURNS void AS
|
||||||
--min and max periods
|
$func$
|
||||||
minmax AS (
|
BEGIN
|
||||||
SELECT
|
WITH
|
||||||
(SELECT lower(dur) FROM evt.fspr WHERE id = '2018.08') mind
|
--list each period in min and max
|
||||||
,(SELECT lower(dur) FROM evt.fspr WHERE id = '2018.11') maxd
|
prng AS (
|
||||||
)
|
|
||||||
--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
|
SELECT
|
||||||
a.acct
|
id
|
||||||
,f.id
|
,dur
|
||||||
,f.dur
|
,prop
|
||||||
,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
|
FROM
|
||||||
arng a
|
evt.fspr f
|
||||||
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
|
WHERE
|
||||||
lower(f.dur) <= (SELECT maxd FROM minmax)
|
f.dur >= _mind
|
||||||
|
AND f.dur <= _maxd
|
||||||
)
|
)
|
||||||
select * from rf
|
--get every account involved in target range
|
||||||
)
|
,arng AS (
|
||||||
INSERT INTO
|
SELECT DISTINCT
|
||||||
evt.bal
|
acct
|
||||||
SELECT
|
FROM
|
||||||
acct
|
evt.bal b
|
||||||
,id
|
INNER JOIN prng ON
|
||||||
,obal
|
prng.id = b.fspr
|
||||||
,debits
|
)
|
||||||
,credits
|
,bld AS (
|
||||||
,cbal
|
WITH RECURSIVE rf (acct, id, dur, propr, obal, debits, credits, cbal) AS
|
||||||
FROM
|
(
|
||||||
bld
|
SELECT
|
||||||
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
|
a.acct
|
||||||
obal = EXCLUDED.obal
|
,f.id
|
||||||
,debits = EXCLUDED.debits
|
,f.dur
|
||||||
,credits = EXCLUDED.credits
|
,f.prop
|
||||||
,cbal = EXCLUDED.cbal
|
,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
|
Loading…
Reference in New Issue
Block a user