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