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' --AND DIGITS(N1FSYP) = '1901'
) )
--SELECT * FROM gld --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 ( ,cop AS (
SELECT SELECT
o.plnt o.plnt
@ -250,11 +272,11 @@ gld AS (
,o.fgqshp ,o.fgqshp
,o.diqtsh ,o.diqtsh
,sum(o.fb_qty) fb_qty ,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_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,sum(o.fb_val_loc) fb_val_loc ,sum(COALESCE(o.fb_qty * p.price,o.fb_val_loc)) fb_val_loc
,sum(o.fb_val_loc_pl) fb_val_loc_pl ,sum(o.fb_val_loc) fb_val_loc_pl
,'CLOSED' calc_status ,'CLOSED' calc_status
,'SHIPMENT' flag ,'SHIPMENT' flag
,o.orderdate ,o.orderdate
@ -269,6 +291,20 @@ gld AS (
rlarp.osmf o rlarp.osmf o
LEFT OUTER JOIN gld ON LEFT OUTER JOIN gld ON
gld.fspr = o.fspr 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 WHERE
o.adj_orderdate BETWEEN '2019-03-25' AND '2019-05-31' o.adj_orderdate BETWEEN '2019-03-25' AND '2019-05-31'
AND fs_line = '41010' AND fs_line = '41010'

View File

@ -1,4 +1,5 @@
BEGIN; BEGIN;
with p AS ( with p AS (
SELECT SELECT
part part
@ -22,7 +23,7 @@ with p AS (
,orderdate ,orderdate
) )
--SELECT * FROM p WHERE rn = 1 --SELECT * FROM p WHERE rn = 1
,ins AS ( ,incr AS (
SELECT SELECT
b.plnt b.plnt
,b."ddord#" ,b."ddord#"
@ -84,12 +85,12 @@ with p AS (
,b.ddqtsi ,b.ddqtsi
,b.fgqshp ,b.fgqshp
,b.diqtsh ,b.diqtsh
,b.fb_qty ,0 fb_qty
,b.fb_cst_loc ,0 fb_cst_loc
,b.fb_cst_loc_cur ,0 fb_cst_loc_cur
,b.fb_cst_loc_fut ,0 fb_cst_loc_fut
,b.fb_qty * p.price - b.fb_val_loc fb_val_loc ,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.calc_status
,b.flag ,b.flag
,b.orderdate ,b.orderdate
@ -99,24 +100,21 @@ with p AS (
,b.adj_requestdate ,b.adj_requestdate
,b.adj_shipdate ,b.adj_shipdate
,b."version" ,b."version"
,b.iter ,'last price paid' 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
FROM 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 WHERE
p.part = f.part b.orderdate BETWEEN '2019-03-25' AND '2019-05-31'
AND p.bill_cust = f.bill_cust OR b.orderdate BETWEEN '2020-03-25' AND '2020-05-31'
AND p.ship_cust = f.ship_cust
) )
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;