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 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 --list each period in min and max
,prng AS ( 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 (
@ -85,4 +81,7 @@ 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