added partion by major item, and then sort on import id

This commit is contained in:
Paul Trowbridge 2017-10-25 10:45:24 -04:00
parent 44373bc616
commit ba4a404980
2 changed files with 9 additions and 6 deletions

View File

@ -7,7 +7,7 @@ SELECT
WHEN '02IN Raw Material' THEN 13097563.42 WHEN '02IN Raw Material' THEN 13097563.42
WHEN '03IN Finished Goods' THEN 35790696.52 WHEN '03IN Finished Goods' THEN 35790696.52
ELSE 0 ELSE 0
END + SUM("Sales"+"Credits & Adjustments"-"Gross Collections") OVER (ORDER BY "PostDate" ASC, rec->>'id' ASC) running_bal END + SUM("Sales"+"Credits & Adjustments"-"Gross Collections") OVER (PARTITION BY "Schedule#" ORDER BY "Schedule#" ASC, "PostDate" ASC, rec->>'id' ASC) running_bal
,(LEAST("CollateralBalance" - "Ineligible Amount","MaxEligible")*("AdvanceRate"/100))::NUMERIC(20,2) qualified_collateral ,(LEAST("CollateralBalance" - "Ineligible Amount","MaxEligible")*("AdvanceRate"/100))::NUMERIC(20,2) qualified_collateral
,(("CollateralBalance" - "Ineligible Amount")*("AdvanceRate"/100))::NUMERIC(20,2) qualified_collateral_nl ,(("CollateralBalance" - "Ineligible Amount")*("AdvanceRate"/100))::NUMERIC(20,2) qualified_collateral_nl
FROM FROM
@ -15,7 +15,8 @@ FROM
LEFT JOIN LATERAL jsonb_populate_record(null::tps.pncl, rec) r ON TRUE LEFT JOIN LATERAL jsonb_populate_record(null::tps.pncl, rec) r ON TRUE
WHERE WHERE
srce = 'PNCL' srce = 'PNCL'
AND "PostDate" >= '2017-10-12' --AND rec @> '{"Schedule#":"03IN Finished Goods"}'
--rec @> '{"Schedule#":"03IN Finished Goods"}'
ORDER BY ORDER BY
r."PostDate" asc "Schedule#" asc
,r."PostDate" asc
,rec->>'id' asc

View File

@ -1,12 +1,14 @@
\timing \timing
SELECT SELECT
r.*, r.*,
SUM(r."Advances"+r."Adjustments"-r."Payments") OVER (ORDER BY r."Post Date" asc ,r."Reference #" asc) SUM(r."Advances"+r."Adjustments"-r."Payments") OVER (PARTITION BY "Loan#" ORDER BY r."Post Date" asc ,rec->>'id' asc, r."Reference #" asc)
FROM FROM
tps.trans tps.trans
LEFT JOIN LATERAL jsonb_populate_record(null::tps.pnco, rec) r ON TRUE LEFT JOIN LATERAL jsonb_populate_record(null::tps.pnco, rec) r ON TRUE
WHERE WHERE
rec @> '{"Loan#":"606780191"}' rec @> '{"Loan#":"606780191"}'
ORDER BY ORDER BY
r."Post Date" asc r."Loan#"
,r."Post Date" asc
,rec->>'id' asc
,r."Reference #" asc ,r."Reference #" asc