diff --git a/route_sql/build/copy_orders.sql b/route_sql/build/copy_orders.sql index bc3045a..2242194 100644 --- a/route_sql/build/copy_orders.sql +++ b/route_sql/build/copy_orders.sql @@ -1,511 +1,547 @@ -BEGIN; ---\timing -truncate table rlarp.osmfs; - -WITH -gld AS ( - SELECT - N1COMP COMP - ,N1CCYY FSYR - ,KPMAXP PERDS - ,N1FSPP PERD - ,to_char(N1FSYP,'FM0000') FSPR - ,N1SD01 SDAT - ,N1ED01 EDAT - ,to_char(N1ED01,'yymm') CAPR - ,N1ED01 - N1SD01 +1 NDAYS - ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR - ,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR - FROM - LGDAT.GLDATREF - INNER JOIN LGDAT.GLDATE ON - KPCOMP = N1COMP AND - KPCCYY = N1CCYY - WHERE - N1COMP = 93 - --AND DIGITS(N1FSYP) = '1901' -) ---SELECT * FROM gld -,cop AS ( - SELECT - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,o.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,sum(o.fb_qty) fb_qty - ,sum(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 - ,o.calc_status - ,o.flag - ,o.orderdate - ,o.requestdate - ,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate - ,o.adj_orderdate - ,o.adj_requestdate - ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate - ,'9p3' "version" - ,'actual' iter - FROM - rlarp.osm o - LEFT OUTER JOIN gld ON - gld.fspr = o.fspr - WHERE - ( - o.adj_orderdate BETWEEN '2018-06-01' AND '2019-03-24' - OR o.calc_status IN ('OPEN','BACKORDER') - OR o.fspr BETWEEN '1901' AND '1910' - ) - AND fs_line = '41010' - AND calc_status <> 'CANCELED' - AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments - --AND adj_orderdate <= adj_shipdate --exclude poorly date - GROUP BY - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,o.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,o.calc_status - ,o.flag - ,o.orderdate - ,o.requestdate - ,greatest(least(o.shipdate,gld.edat),gld.sdat) - ,o.adj_orderdate - ,o.adj_requestdate - ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) - UNION ALL - SELECT - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,o.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,sum(o.fb_qty) fb_qty - ,sum(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 - ,'CLOSED' calc_status - ,'SHIPMENT' flag - ,o.orderdate - ,o.requestdate - ,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate - ,o.adj_orderdate - ,o.adj_requestdate - ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate - ,'9p3' "version" - ,'forecast' iter - FROM - rlarp.osmf o - LEFT OUTER JOIN gld ON - gld.fspr = o.fspr - WHERE - o.adj_orderdate BETWEEN '2019-03-25' AND '2019-05-31' - AND fs_line = '41010' - AND calc_status <> 'CANCELED' - AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments - AND adj_orderdate <= adj_shipdate --exclude bogus ship date orders - GROUP BY - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,o.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,o.orderdate - ,o.requestdate - ,greatest(least(o.shipdate,gld.edat),gld.sdat) - ,o.adj_orderdate - ,o.adj_requestdate - ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) -) -,incr AS ( - SELECT - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,gld.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,sum(o.fb_qty) fb_qty - ,sum(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 - ,o.calc_status - ,o.flag - ----these dates should already be snapped since the derive from the adjusted set - ,o.orderdate + interval '1 year' orderdate - ,o.requestdate + interval '1 year' requestdate - ,o.shipdate + interval '1 year' shipdate - ,o.adj_orderdate + interval '1 year' adj_orderdate - ,o.adj_requestdate + interval '1 year' adj_requestdate - ,o.adj_shipdate + interval '1 year' adj_shipdate - ,'b20' "version" - ,'copy' iter - FROM - cop o - LEFT OUTER JOIN gld ON - (o.adj_shipdate + interval '1 year') BETWEEN gld.sdat AND gld.edat - WHERE - adj_orderdate BETWEEN '2018-06-01' AND '2019-05-31' - GROUP BY - o.plnt - ,o."ddord#" - ,o."dditm#" - ,o."fgbol#" - ,o."fgent#" - ,o."diinv#" - ,o."dilin#" - ,o.promo - ,o.return_reas - ,o.terms - ,o.custpo - ,o.dhincr - ,o.diext - ,o.ditdis - ,o.dcodat - ,o.ddqdat - ,o.dcmdat - ,o.dhidat - ,gld.fspr - ,o.remit_to - ,o.bill_class - ,o.bill_cust - ,o.bill_rep - ,o.bill_terr - ,o.ship_class - ,o.ship_cust - ,o.ship_rep - ,o.ship_terr - ,o.quota_rep - ,o.account - ,o.shipgrp - ,o.geo - ,o.chan - ,o.orig_ctry - ,o.orig_prov - ,o.orig_post - ,o.dest_ctry - ,o.dest_prov - ,o.dest_post - ,o.part - ,o.ord_gldc - ,o.majg - ,o.ming - ,o.majs - ,o.mins - ,o.gldc - ,o.glec - ,o.harm - ,o.clss - ,o.brand - ,o.assc - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.ddqtoi - ,o.ddqtsi - ,o.fgqshp - ,o.diqtsh - ,o.calc_status - ,o.flag - ,o.orderdate + interval '1 year' - ,o.requestdate + interval '1 year' - ,o.shipdate + interval '1 year' - ,o.adj_orderdate + interval '1 year' - ,o.adj_requestdate + interval '1 year' - ,o.adj_shipdate + interval '1 year' -) -INSERT INTO rlarp.osmfs -SELECT * FROM incr -UNION ALL -SELECT * FROM cop; - ----identify short ships: causes disconnect with actual sales------------------------------------------------------------------- ---UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; - ----identify goofy ship dates: causes disconnect with sales when splicing in a forecast that has this problem------------------- ---UPDATE rlarp.osmfs SET iter = 'bad date' WHERE adj_shipdate < adj_orderdate; +BEGIN; +--\timing +truncate table rlarp.osmfs; + +WITH +gld AS ( + SELECT + N1COMP COMP + ,N1CCYY FSYR + ,KPMAXP PERDS + ,N1FSPP PERD + ,to_char(N1FSYP,'FM0000') FSPR + ,N1SD01 SDAT + ,N1ED01 EDAT + ,to_char(N1ED01,'yymm') CAPR + ,N1ED01 - N1SD01 +1 NDAYS + ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR + ,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR + FROM + LGDAT.GLDATREF + INNER JOIN LGDAT.GLDATE ON + KPCOMP = N1COMP AND + KPCCYY = N1CCYY + WHERE + N1COMP = 93 + --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 + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,o.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,sum(o.fb_qty) fb_qty + ,sum(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 + ,o.calc_status + ,o.flag + ,o.orderdate + ,o.requestdate + ,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate + ,o.adj_orderdate + ,o.adj_requestdate + ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate + ,'9p3' "version" + ,'actual' iter + FROM + rlarp.osm o + LEFT OUTER JOIN gld ON + gld.fspr = o.fspr + WHERE + ( + o.adj_orderdate BETWEEN '2018-06-01' AND '2019-03-24' + OR o.calc_status IN ('OPEN','BACKORDER') + OR o.fspr BETWEEN '1901' AND '1910' + ) + AND fs_line = '41010' + AND calc_status <> 'CANCELED' + AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments + --AND adj_orderdate <= adj_shipdate --exclude poorly date + GROUP BY + o.plnt + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,o.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.calc_status + ,o.flag + ,o.orderdate + ,o.requestdate + ,greatest(least(o.shipdate,gld.edat),gld.sdat) + ,o.adj_orderdate + ,o.adj_requestdate + ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) + UNION ALL + SELECT + o.plnt + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,o.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,sum(o.fb_qty) fb_qty + ,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(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 + ,o.requestdate + ,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate + ,o.adj_orderdate + ,o.adj_requestdate + ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate + ,'9p3' "version" + ,'forecast' iter + FROM + 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' + AND calc_status <> 'CANCELED' + AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments + AND adj_orderdate <= adj_shipdate --exclude bogus ship date orders + GROUP BY + o.plnt + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,o.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.orderdate + ,o.requestdate + ,greatest(least(o.shipdate,gld.edat),gld.sdat) + ,o.adj_orderdate + ,o.adj_requestdate + ,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) +) +,incr AS ( + SELECT + o.plnt + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,gld.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,sum(o.fb_qty) fb_qty + ,sum(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 + ,o.calc_status + ,o.flag + ----these dates should already be snapped since the derive from the adjusted set + ,o.orderdate + interval '1 year' orderdate + ,o.requestdate + interval '1 year' requestdate + ,o.shipdate + interval '1 year' shipdate + ,o.adj_orderdate + interval '1 year' adj_orderdate + ,o.adj_requestdate + interval '1 year' adj_requestdate + ,o.adj_shipdate + interval '1 year' adj_shipdate + ,'b20' "version" + ,'copy' iter + FROM + cop o + LEFT OUTER JOIN gld ON + (o.adj_shipdate + interval '1 year') BETWEEN gld.sdat AND gld.edat + WHERE + adj_orderdate BETWEEN '2018-06-01' AND '2019-05-31' + GROUP BY + o.plnt + ,o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.dhincr + ,o.diext + ,o.ditdis + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.dhidat + ,gld.fspr + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.quota_rep + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.ord_gldc + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.calc_status + ,o.flag + ,o.orderdate + interval '1 year' + ,o.requestdate + interval '1 year' + ,o.shipdate + interval '1 year' + ,o.adj_orderdate + interval '1 year' + ,o.adj_requestdate + interval '1 year' + ,o.adj_shipdate + interval '1 year' +) +INSERT INTO rlarp.osmfs +SELECT * FROM incr +UNION ALL +SELECT * FROM cop; + +---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- +--UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; + +---identify goofy ship dates: causes disconnect with sales when splicing in a forecast that has this problem------------------- +--UPDATE rlarp.osmfs SET iter = 'bad date' WHERE adj_shipdate < adj_orderdate; COMMIT; \ No newline at end of file diff --git a/route_sql/build/last_price.sql b/route_sql/build/last_price.sql index f78c965..10c2d18 100644 --- a/route_sql/build/last_price.sql +++ b/route_sql/build/last_price.sql @@ -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; -*/ \ No newline at end of file +, ins AS ( + INSERT INTO rlarp.osmfs SELECT * FROM incr RETURNING * +) +SELECT fspr, sum(fb_val_loc *r_rate) FROM ins GROUP BY fspr; + +COMMIT; \ No newline at end of file