convert rollfoward to function

This commit is contained in:
Paul Trowbridge 2018-09-24 16:47:38 -04:00
parent 57d9f08699
commit e6770c243b

View File

@ -1,22 +1,18 @@
CREATE FUNCTION evt.balrf(_mind tstzrange, _maxd tstzrange) RETURNS void AS
$func$
BEGIN
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 (
prng AS (
SELECT
id
,dur
,prop
FROM
evt.fspr f
CROSS JOIN minmax
WHERE
lower(f.dur) >= mind
AND lower(f.dur) <= maxd
f.dur >= _mind
AND f.dur <= _maxd
)
--get every account involved in target range
,arng AS (
@ -85,4 +81,7 @@ 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