convert rollfoward to function
This commit is contained in:
parent
57d9f08699
commit
e6770c243b
@ -1,33 +1,29 @@
|
|||||||
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
|
SELECT
|
||||||
id
|
id
|
||||||
,dur
|
,dur
|
||||||
,prop
|
,prop
|
||||||
FROM
|
FROM
|
||||||
evt.fspr f
|
evt.fspr f
|
||||||
CROSS JOIN minmax
|
|
||||||
WHERE
|
WHERE
|
||||||
lower(f.dur) >= mind
|
f.dur >= _mind
|
||||||
AND lower(f.dur) <= maxd
|
AND f.dur <= _maxd
|
||||||
)
|
)
|
||||||
--get every account involved in target range
|
--get every account involved in target range
|
||||||
,arng AS (
|
,arng AS (
|
||||||
SELECT DISTINCT
|
SELECT DISTINCT
|
||||||
acct
|
acct
|
||||||
FROM
|
FROM
|
||||||
evt.bal b
|
evt.bal b
|
||||||
INNER JOIN prng ON
|
INNER JOIN prng ON
|
||||||
prng.id = b.fspr
|
prng.id = b.fspr
|
||||||
)
|
)
|
||||||
,bld AS (
|
,bld AS (
|
||||||
WITH RECURSIVE rf (acct, id, dur, propr, obal, debits, credits, cbal) AS
|
WITH RECURSIVE rf (acct, id, dur, propr, obal, debits, credits, cbal) AS
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
@ -69,20 +65,23 @@ minmax AS (
|
|||||||
lower(f.dur) <= (SELECT maxd FROM minmax)
|
lower(f.dur) <= (SELECT maxd FROM minmax)
|
||||||
)
|
)
|
||||||
select * from rf
|
select * from rf
|
||||||
)
|
)
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
evt.bal
|
evt.bal
|
||||||
SELECT
|
SELECT
|
||||||
acct
|
acct
|
||||||
,id
|
,id
|
||||||
,obal
|
,obal
|
||||||
,debits
|
,debits
|
||||||
,credits
|
,credits
|
||||||
,cbal
|
,cbal
|
||||||
FROM
|
FROM
|
||||||
bld
|
bld
|
||||||
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
|
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
|
||||||
obal = EXCLUDED.obal
|
obal = EXCLUDED.obal
|
||||||
,debits = EXCLUDED.debits
|
,debits = EXCLUDED.debits
|
||||||
,credits = EXCLUDED.credits
|
,credits = EXCLUDED.credits
|
||||||
,cbal = EXCLUDED.cbal
|
,cbal = EXCLUDED.cbal;
|
||||||
|
END;
|
||||||
|
$func$
|
||||||
|
LANGUAGE plpgsql
|
Loading…
Reference in New Issue
Block a user