From ba4a404980341a766cda171ca36bbe713520a0f5 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 25 Oct 2017 10:45:24 -0400 Subject: [PATCH] added partion by major item, and then sort on import id --- col_balance.pgsql | 9 +++++---- loan_bal.pgsql | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/col_balance.pgsql b/col_balance.pgsql index 09e726a..8f812f3 100644 --- a/col_balance.pgsql +++ b/col_balance.pgsql @@ -7,7 +7,7 @@ SELECT WHEN '02IN Raw Material' THEN 13097563.42 WHEN '03IN Finished Goods' THEN 35790696.52 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 ,(("CollateralBalance" - "Ineligible Amount")*("AdvanceRate"/100))::NUMERIC(20,2) qualified_collateral_nl FROM @@ -15,7 +15,8 @@ FROM LEFT JOIN LATERAL jsonb_populate_record(null::tps.pncl, rec) r ON TRUE WHERE srce = 'PNCL' - AND "PostDate" >= '2017-10-12' - --rec @> '{"Schedule#":"03IN Finished Goods"}' + --AND rec @> '{"Schedule#":"03IN Finished Goods"}' ORDER BY - r."PostDate" asc \ No newline at end of file + "Schedule#" asc + ,r."PostDate" asc + ,rec->>'id' asc \ No newline at end of file diff --git a/loan_bal.pgsql b/loan_bal.pgsql index 60da152..a149817 100644 --- a/loan_bal.pgsql +++ b/loan_bal.pgsql @@ -1,12 +1,14 @@ \timing SELECT 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 tps.trans LEFT JOIN LATERAL jsonb_populate_record(null::tps.pnco, rec) r ON TRUE WHERE rec @> '{"Loan#":"606780191"}' ORDER BY - r."Post Date" asc + r."Loan#" + ,r."Post Date" asc + ,rec->>'id' asc ,r."Reference #" asc