update building of forecast to integrate last price paid

This commit is contained in:
Paul Trowbridge 2019-03-25 02:31:31 -04:00
parent 878ed265ca
commit 4907170629
2 changed files with 568 additions and 534 deletions

View File

@ -26,6 +26,28 @@ gld AS (
--AND DIGITS(N1FSYP) = '1901'
)
--SELECT * FROM gld
,price AS (
SELECT
part
,bill_cust
,ship_cust
,round(fb_val_loc/fb_qty,10) AS price
,orderdate
,row_number() OVER (PARTITION BY part,bill_cust, ship_cust ORDER BY orderdate DESC) rn
FROM
rlarp.osm
WHERE
fs_line = '41010'
AND calc_status <> 'CANCELED'
AND COALESCE(part,'') <> ''
AND fb_qty <> 0
AND orderdate IS NOT NULL
ORDER BY
part
,bill_cust
,ship_cust
,orderdate
)
,cop AS (
SELECT
o.plnt
@ -250,11 +272,11 @@ gld AS (
,o.fgqshp
,o.diqtsh
,sum(o.fb_qty) fb_qty
,sum(o.fb_cst_loc) fb_cst_loc
,sum(COALESCE(o.fb_qty * coalesce(m.cgstcs, pr.chstcs, r.y0stcs),o.fb_cst_loc)) fb_cst_loc
,sum(o.fb_cst_loc_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,sum(COALESCE(o.fb_qty * p.price,o.fb_val_loc)) fb_val_loc
,sum(o.fb_val_loc) fb_val_loc_pl
,'CLOSED' calc_status
,'SHIPMENT' flag
,o.orderdate
@ -269,6 +291,20 @@ gld AS (
rlarp.osmf o
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
LEFT OUTER JOIN price p ON
p.part = o.part
AND p.bill_cust = o.bill_cust
AND p.ship_cust = o.ship_cust
AND p.rn = 1
LEFT OUTER JOIN lgdat.icstm m ON
m.cgpart = o.part
AND m.cgplnt = o.plnt
LEFT OUTER JOIN lgdat.icstp pr ON
pr.chpart = o.part
AND pr.chplnt = o.plnt
LEFT OUTER JOIN lgdat.icstr r ON
y0part = o.part
AND y0plnt = o.plnt
WHERE
o.adj_orderdate BETWEEN '2019-03-25' AND '2019-05-31'
AND fs_line = '41010'

View File

@ -1,4 +1,5 @@
BEGIN;
with p AS (
SELECT
part
@ -22,7 +23,7 @@ with p AS (
,orderdate
)
--SELECT * FROM p WHERE rn = 1
,ins AS (
,incr AS (
SELECT
b.plnt
,b."ddord#"
@ -84,12 +85,12 @@ with p AS (
,b.ddqtsi
,b.fgqshp
,b.diqtsh
,b.fb_qty
,b.fb_cst_loc
,b.fb_cst_loc_cur
,b.fb_cst_loc_fut
,0 fb_qty
,0 fb_cst_loc
,0 fb_cst_loc_cur
,0 fb_cst_loc_fut
,b.fb_qty * p.price - b.fb_val_loc fb_val_loc
,b.fb_val_loc_pl
,0 fb_val_loc_pl
,b.calc_status
,b.flag
,b.orderdate
@ -99,24 +100,21 @@ with p AS (
,b.adj_requestdate
,b.adj_shipdate
,b."version"
,b.iter
WHERE
orderdate BETWEEN '2019-03-25' AND '2019-05-31'
AND version = '9p3'
AND iter = 'forecast'
)
,
/*
,upd AS (
UPDATE rlarp.osmfs SET
fb_val_loc = fb_qty * p.price
,'last price paid' iter
FROM
p
rlarp.osmfs b
INNER JOIN p ON
p.part = b.part
AND p.bill_cust = b.bill_cust
AND p.ship_cust = b.ship_cust
AND p.rn = 1
WHERE
p.part = f.part
AND p.bill_cust = f.bill_cust
AND p.ship_cust = f.ship_cust
b.orderdate BETWEEN '2019-03-25' AND '2019-05-31'
OR b.orderdate BETWEEN '2020-03-25' AND '2020-05-31'
)
ROLLBACK;
*/
, ins AS (
INSERT INTO rlarp.osmfs SELECT * FROM incr RETURNING *
)
SELECT fspr, sum(fb_val_loc *r_rate) FROM ins GROUP BY fspr;
COMMIT;