try balance trigger
This commit is contained in:
parent
1c1f86fb3c
commit
e29b3cea2e
@ -6,57 +6,47 @@ CREATE OR REPLACE FUNCTION evt.bal_insert() RETURNS trigger
|
|||||||
$func$
|
$func$
|
||||||
BEGIN
|
BEGIN
|
||||||
WITH
|
WITH
|
||||||
seq AS (
|
list AS (
|
||||||
WITH RECURSIVE rf (acct, fspr, minrange, maxrange, dur, id, obal, debits, credits, cbal) AS
|
SELECT
|
||||||
|
acct
|
||||||
|
,min(lower(dur)) minp
|
||||||
|
,max(lower(dur)) maxp
|
||||||
|
FROM
|
||||||
|
ins b
|
||||||
|
INNER JOIN evt.fspr f ON
|
||||||
|
f.id = b.fspr
|
||||||
|
GROUP BY
|
||||||
|
acct
|
||||||
|
)
|
||||||
|
,seq AS (
|
||||||
|
WITH RECURSIVE rf (acct, minp, maxp, id, dur, obal, debits, credits, cbal) AS
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
rng.acct
|
list.acct
|
||||||
,rng.fspr
|
,list.minp
|
||||||
,rng.minrange
|
,list.maxp
|
||||||
,rng.maxrange
|
|
||||||
,f.dur
|
|
||||||
,f.id
|
,f.id
|
||||||
|
,f.dur
|
||||||
,b.obal::numeric(12,2)
|
,b.obal::numeric(12,2)
|
||||||
,b.debits::numeric(12,2)
|
,b.debits::numeric(12,2)
|
||||||
,b.credits::numeric(12,2)
|
,b.credits::numeric(12,2)
|
||||||
,b.cbal::numeric(12,2)
|
,b.cbal::numeric(12,2)
|
||||||
FROM
|
FROM
|
||||||
(
|
list
|
||||||
--for each item determine if a gap exists between new an previous period (if any)
|
|
||||||
SELECT
|
|
||||||
ins.acct
|
|
||||||
,ins.fspr
|
|
||||||
,lower(f.dur) dur
|
|
||||||
,CASE WHEN lower(f.dur) > max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END minrange
|
|
||||||
,CASE WHEN lower(f.dur) < max(lower(bp.dur)) THEN max(lower(bp.dur)) ELSE lower(f.dur) END maxrange
|
|
||||||
FROM
|
|
||||||
ins
|
|
||||||
INNER JOIN evt.fspr f ON
|
INNER JOIN evt.fspr f ON
|
||||||
f.id = ins.fspr
|
lower(f.dur) = list.minp
|
||||||
LEFT OUTER JOIN evt.bal b ON
|
LEFT OUTER JOIN evt.bal b ON
|
||||||
b.acct = ins.acct
|
b.acct = list.acct
|
||||||
LEFT OUTER JOIN evt.fspr bp ON
|
|
||||||
bp.id = b.fspr
|
|
||||||
GROUP BY
|
|
||||||
ins.acct
|
|
||||||
,ins.fspr
|
|
||||||
,f.dur
|
|
||||||
) rng
|
|
||||||
INNER JOIN evt.fspr f ON
|
|
||||||
lower(f.dur) = minrange
|
|
||||||
INNER JOIN evt.bal b ON
|
|
||||||
b.acct = rng.acct
|
|
||||||
AND b.fspr = f.id
|
AND b.fspr = f.id
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
rf.acct
|
rf.acct
|
||||||
,rf.fspr
|
,rf.minp
|
||||||
,rf.minrange
|
,rf.maxp
|
||||||
,rf.maxrange
|
|
||||||
,f.dur
|
|
||||||
,f.id
|
,f.id
|
||||||
|
,f.dur
|
||||||
,COALESCE(rf.cbal,0)::numeric(12,2)
|
,COALESCE(rf.cbal,0)::numeric(12,2)
|
||||||
,COALESCE(b.debits,0)::numeric(12,2)
|
,COALESCE(b.debits,0)::numeric(12,2)
|
||||||
,COALESCE(b.credits,0)::numeric(12,2)
|
,COALESCE(b.credits,0)::numeric(12,2)
|
||||||
@ -69,9 +59,9 @@ CREATE OR REPLACE FUNCTION evt.bal_insert() RETURNS trigger
|
|||||||
b.acct = rf.acct
|
b.acct = rf.acct
|
||||||
AND b.fspr = f.id
|
AND b.fspr = f.id
|
||||||
WHERE
|
WHERE
|
||||||
lower(f.dur) <= rf.maxrange
|
lower(f.dur) <= rf.maxp
|
||||||
)
|
)
|
||||||
SELECT * FROM rf
|
select * from rf
|
||||||
)
|
)
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
evt.bal (acct, fspr, obal, debits, credits, cbal)
|
evt.bal (acct, fspr, obal, debits, credits, cbal)
|
||||||
|
Loading…
Reference in New Issue
Block a user