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