diff --git a/route_sql/increment_orders.sql b/route_sql/increment_orders.sql new file mode 100644 index 0000000..e69de29 diff --git a/route_sql/last_price.sql b/route_sql/last_price.sql index e69de29..f78c965 100644 --- a/route_sql/last_price.sql +++ b/route_sql/last_price.sql @@ -0,0 +1,122 @@ +BEGIN; +with p 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 +) +--SELECT * FROM p WHERE rn = 1 +,ins AS ( + SELECT + b.plnt + ,b."ddord#" + ,b."dditm#" + ,b."fgbol#" + ,b."fgent#" + ,b."diinv#" + ,b."dilin#" + ,b.promo + ,b.return_reas + ,b.terms + ,b.custpo + ,b.dhincr + ,b.diext + ,b.ditdis + ,b.dcodat + ,b.ddqdat + ,b.dcmdat + ,b.dhidat + ,b.fspr + ,b.remit_to + ,b.bill_class + ,b.bill_cust + ,b.bill_rep + ,b.bill_terr + ,b.ship_class + ,b.ship_cust + ,b.ship_rep + ,b.ship_terr + ,b.quota_rep + ,b.account + ,b.shipgrp + ,b.geo + ,b.chan + ,b.orig_ctry + ,b.orig_prov + ,b.orig_post + ,b.dest_ctry + ,b.dest_prov + ,b.dest_post + ,b.part + ,b.ord_gldc + ,b.majg + ,b.ming + ,b.majs + ,b.mins + ,b.gldc + ,b.glec + ,b.harm + ,b.clss + ,b.brand + ,b.assc + ,b.fs_line + ,b.r_currency + ,b.r_rate + ,b.c_currency + ,b.c_rate + ,b.ddqtoi + ,b.ddqtsi + ,b.fgqshp + ,b.diqtsh + ,b.fb_qty + ,b.fb_cst_loc + ,b.fb_cst_loc_cur + ,b.fb_cst_loc_fut + ,b.fb_qty * p.price - b.fb_val_loc fb_val_loc + ,b.fb_val_loc_pl + ,b.calc_status + ,b.flag + ,b.orderdate + ,b.requestdate + ,b.shipdate + ,b.adj_orderdate + ,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 + FROM + p + WHERE + p.part = f.part + AND p.bill_cust = f.bill_cust + AND p.ship_cust = f.ship_cust +) +ROLLBACK; +*/ \ No newline at end of file diff --git a/route_sql/open_timing.sql b/route_sql/open_timing.sql new file mode 100644 index 0000000..ca296d0 --- /dev/null +++ b/route_sql/open_timing.sql @@ -0,0 +1,103 @@ +WITH +mseq AS ( + SELECT * FROM + ( + VALUES + ('Jun',1,6,-1) + ,('Jul',2,7,-1) + ,('Aug',3,8,-1) + ,('Sep',4,9,-1) + ,('Oct',5,10,-1) + ,('Nov',6,11,-1) + ,('Dec',7,12,-1) + ,('Jan',8,1,0) + ,('Feb',9,2,0) + ,('Mar',10,3,0) + ,('Apr',11,4,0) + ,('May',12,5,0) + ) x(m,s,cal,yr) +) +,alldates AS ( + SELECT + shipgrp + ,to_char(orderdate,'Mon') orderm + ,extract(month FROM age(requestdate,orderdate)) rmo + ,(sum((shipdate - requestdate)* (fb_val_loc * r_rate))/sum(fb_val_loc * r_rate))::int wav_lag + --,sum(fb_val_loc * r_rate) value_usd + FROM + rlarp.osm + WHERE + adj_orderdate >= '2017-06-01' + AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments + AND adj_orderdate <= adj_shipdate + AND fspr IS NOT NULL + GROUP BY + shipgrp + ,to_char(orderdate,'Mon') + ,extract(month FROM age(requestdate,orderdate)) + HAVING + sum(fb_val_loc * r_rate) <> 0 +) +,lag AS ( + SELECT + shipgrp + ,orderm + ,rmo + ,wav_lag + FROM + alldates + ORDER BY + shipgrp + ,orderm + ,rmo +) +,opord AS ( +SELECT + shipgrp + ,orderdate + ,requestdate + ,shipdate + ,to_char(orderdate,'Mon') orderm + ,extract(month FROM age(requestdate,orderdate)) rmo + ,sum(fb_val_loc * r_rate) value_usd +FROM + rlarp.osm +WHERE + calc_status NOT IN ('CANCELED','CLOSED') +GROUP BY + shipgrp + ,orderdate + ,requestdate + ,shipdate + ,to_char(orderdate,'Mon') + ,extract(month FROM age(requestdate,orderdate)) +ORDER BY + value_usd DESC +) +,rbld AS ( +SELECT + o.shipgrp + ,o.orderdate + ,o.requestdate + ,o.shipdate + ,o.orderm + ,o.rmo + ,o.value_usd + ,lag.wav_lag + ,o.requestdate + lag.wav_lag + 15 rship +FROM + opord o + LEFT OUTER JOIN lag ON + lag.shipgrp = o.shipgrp + AND lag.orderm = o.orderm + AND lag.rmo = o.rmo +ORDER BY + value_usd desc +) +select + sum(value_usd) filter (where rship >= '2019-04-01') + ,sum(value_usd) filter (where rship < '2019-04-01') +from + rbld +where + shipdate < '2019-04-01' \ No newline at end of file