get rid of ins with block table

This commit is contained in:
Paul Trowbridge 2018-09-24 15:50:25 -04:00
parent 55757373f4
commit bc239d2a6b
2 changed files with 28 additions and 46 deletions

View File

@ -193,22 +193,13 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
acct acct
,fspr ,fspr
) )
,ins AS (
SELECT
acct
,fspr
,debits
,credits
FROM
agg
)
,list AS ( ,list AS (
SELECT SELECT
b.acct b.acct
,least(min(lower(f.dur)),min(lower(g.dur))) minp ,least(min(lower(f.dur)),min(lower(g.dur))) minp
,greatest(max(lower(f.dur)),max(lower(g.dur))) maxp ,greatest(max(lower(f.dur)),max(lower(g.dur))) maxp
FROM FROM
ins b agg b
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
f.id = b.fspr f.id = b.fspr
LEFT OUTER JOIN evt.bal e ON LEFT OUTER JOIN evt.bal e ON
@ -228,9 +219,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,f.id ,f.id
,f.dur ,f.dur
,COALESCE(b.obal::numeric(12,2),0) ,COALESCE(b.obal::numeric(12,2),0)
,COALESCE(b.debits::numeric(12,2),0) + COALESCE(ins.debits,0) ,COALESCE(b.debits::numeric(12,2),0) + COALESCE(agg.debits,0)
,COALESCE(b.credits::numeric(12,2),0) + COALESCE(ins.credits,0) ,COALESCE(b.credits::numeric(12,2),0) + COALESCE(agg.credits,0)
,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0) ,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(agg.debits,0) + COALESCE(agg.credits,0)
FROM FROM
list list
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -238,9 +229,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = list.acct b.acct = list.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON LEFT OUTER JOIN agg ON
ins.acct = list.acct agg.acct = list.acct
AND ins.fspr = f.id AND agg.fspr = f.id
UNION ALL UNION ALL
@ -251,9 +242,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,f.id ,f.id
,f.dur ,f.dur
,COALESCE(rf.cbal,0)::numeric(12,2) ,COALESCE(rf.cbal,0)::numeric(12,2)
,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(ins.debits,0) ,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(agg.debits,0)
,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(ins.credits,0) ,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(agg.credits,0)
,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0) ,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(agg.debits,0) + COALESCE(agg.credits,0)
FROM FROM
rf rf
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -261,9 +252,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rf.acct b.acct = rf.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON LEFT OUTER JOIN agg ON
ins.acct = rf.acct agg.acct = rf.acct
AND ins.fspr = f.id AND agg.fspr = f.id
WHERE WHERE
lower(f.dur) <= rf.maxp lower(f.dur) <= rf.maxp
) )

View File

@ -18,22 +18,13 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
acct acct
,fspr ,fspr
) )
,ins AS (
SELECT
acct
,fspr
,debits
,credits
FROM
agg
)
,list AS ( ,list AS (
SELECT SELECT
b.acct b.acct
,least(min(lower(f.dur)),min(lower(g.dur))) minp ,least(min(lower(f.dur)),min(lower(g.dur))) minp
,greatest(max(lower(f.dur)),max(lower(g.dur))) maxp ,greatest(max(lower(f.dur)),max(lower(g.dur))) maxp
FROM FROM
ins b agg b
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
f.id = b.fspr f.id = b.fspr
LEFT OUTER JOIN evt.bal e ON LEFT OUTER JOIN evt.bal e ON
@ -53,9 +44,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,f.id ,f.id
,f.dur ,f.dur
,COALESCE(b.obal::numeric(12,2),0) ,COALESCE(b.obal::numeric(12,2),0)
,COALESCE(b.debits::numeric(12,2),0) + COALESCE(ins.debits,0) ,COALESCE(b.debits::numeric(12,2),0) + COALESCE(agg.debits,0)
,COALESCE(b.credits::numeric(12,2),0) + COALESCE(ins.credits,0) ,COALESCE(b.credits::numeric(12,2),0) + COALESCE(agg.credits,0)
,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0) ,COALESCE(b.cbal::numeric(12,2),0) + COALESCE(agg.debits,0) + COALESCE(agg.credits,0)
FROM FROM
list list
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -63,9 +54,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = list.acct b.acct = list.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON LEFT OUTER JOIN agg ON
ins.acct = list.acct agg.acct = list.acct
AND ins.fspr = f.id AND agg.fspr = f.id
UNION ALL UNION ALL
@ -76,9 +67,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
,f.id ,f.id
,f.dur ,f.dur
,COALESCE(rf.cbal,0)::numeric(12,2) ,COALESCE(rf.cbal,0)::numeric(12,2)
,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(ins.debits,0) ,COALESCE(b.debits,0)::numeric(12,2) + COALESCE(agg.debits,0)
,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(ins.credits,0) ,COALESCE(b.credits,0)::numeric(12,2) + COALESCe(agg.credits,0)
,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(ins.debits,0) + COALESCE(ins.credits,0) ,(COALESCE(rf.cbal,0) + COALESCE(b.debits,0) + COALESCE(b.credits,0))::numeric(12,2) + COALESCE(agg.debits,0) + COALESCE(agg.credits,0)
FROM FROM
rf rf
INNER JOIN evt.fspr f ON INNER JOIN evt.fspr f ON
@ -86,9 +77,9 @@ CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LEFT OUTER JOIN evt.bal b ON LEFT OUTER JOIN evt.bal b ON
b.acct = rf.acct b.acct = rf.acct
AND b.fspr = f.id AND b.fspr = f.id
LEFT OUTER JOIN ins ON LEFT OUTER JOIN agg ON
ins.acct = rf.acct agg.acct = rf.acct
AND ins.fspr = f.id AND agg.fspr = f.id
WHERE WHERE
lower(f.dur) <= rf.maxp lower(f.dur) <= rf.maxp
) )