From 2190f55acb56d0c44aa591d2db3ce70fe626cdde Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 19 Oct 2017 11:20:52 -0400 Subject: [PATCH] add running collateral & loan balances --- col_balance.pgsql | 17 +++++++++++++++++ loan_bal.pgsql | 12 ++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 col_balance.pgsql create mode 100644 loan_bal.pgsql diff --git a/col_balance.pgsql b/col_balance.pgsql new file mode 100644 index 0000000..ee2ada9 --- /dev/null +++ b/col_balance.pgsql @@ -0,0 +1,17 @@ + +SELECT + id, + rec->>'id', + r.*, + CASE "Schedule#" + 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) +FROM + tps.trans + LEFT JOIN LATERAL jsonb_populate_record(null::tps.pncl, rec) r ON TRUE +WHERE + rec @> '{"Schedule#":"01AR"}' +ORDER BY + r."PostDate" asc \ No newline at end of file diff --git a/loan_bal.pgsql b/loan_bal.pgsql new file mode 100644 index 0000000..240c296 --- /dev/null +++ b/loan_bal.pgsql @@ -0,0 +1,12 @@ + +SELECT + r.*, + SUM(r."Advances"+r."Adjustments"-r."Payments") OVER (ORDER BY r."Post Date" 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."Reference #" asc