diff --git a/route_sql/new_basket.sql b/route_sql/new_basket.sql index 847f63a..96786ac 100644 --- a/route_sql/new_basket.sql +++ b/route_sql/new_basket.sql @@ -2,7 +2,7 @@ WITH /* the volume must be expressed in terms of units, since that is what it will be scaling */ -target AS (select $$replace_request$$::json def) +target AS (select '{"scenario":{"quota_rep_descr":"30033 - ROBERT NELSON","mod_chan":"DISTRIBUTOR","shipto_group":"KINNEY BONDED","version":"b20","iter":["copy"]},"stamp":"2019-03-22 03:24:28","user":"Trowbridge, Paul","source":"adj","type":"new_part","months":{"Jun":{"amount":"49649.1264","qty":"266248"},"Jul":{"amount":"37780.1","qty":"111786"},"Aug":{"amount":"36449.5","qty":"200396"},"Sep":{"amount":"29804.36","qty":"142327"},"Oct":{"amount":"48592.24","qty":"195356"},"Nov":{"amount":"33608.52","qty":"158100"},"Dec":{"amount":"50410.33","qty":"185462"},"Jan":{"amount":"60578.05","qty":"180712"},"Feb":{"amount":"36233.39","qty":"171236"},"Mar":{"amount":68208.7799685946,"qty":"223464.85292"},"Apr":{"amount":57397.3621278386,"qty":"263108.09419"},"May":{"amount":78626.8773843847,"qty":"324395.89234"}},"newpart":"XNT0TQT2G18B220 - 01.00 Q PER PRP CX BKBK","basket":[{"part_descr":"XNT0TQT2G18B220 - 01.00 Q PER PRP CX BKBK","bill_cust_descr":"KINN0002 - KINNEY BONDED WAREHOUSE INC","ship_cust_descr":"KINN0002 - KINNEY BONDED WAREHOUSE INC","mix":0.61885609384386},{"part_descr":"XNT0TQT2G18B220 - 01.00 Q PER PRP CX BKBK","bill_cust_descr":"KINN0002 - KINNEY BONDED WAREHOUSE INC","ship_cust_descr":"KINN0003 - KINNEY BONDED WAREHOUSE INC","mix":0.381143906156143}]}'::json def) --select * from newm ,GLD AS ( SELECT @@ -179,7 +179,13 @@ target AS (select $$replace_request$$::json def) mseq.m = order_month WHERE -----------------scenario---------------------------- - where_clause + --where_clause + quota_rep_descr = '30033 - ROBERT NELSON' + AND mod_chan = 'DISTRIBUTOR' + AND shipto_group = 'KINNEY BONDED' + AND "version" = 'b20' + AND iter = 'copy' + --probably dont want to include adjustments that have blown away volume and good sources of dating info -----------------additional params------------------- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate @@ -281,7 +287,8 @@ FROM LIMIT 1 ) ) ---SELECT * FROM mmixp order by seq asc, vperc desc +--SELECT * FROM mxm +--this basemix is probably join going to be part-bill-ship with all master data joined later since it is not known if any original is salvaged ,basemix AS ( SELECT null::text plnt ---master data @@ -291,7 +298,7 @@ SELECT ,0::numeric(11,0) "fgent#" ,0::numeric(9,0) "diinv#" ,0::numeric(3,0) "dilin#" - ,null::text promo --history + ,mxm.promo --history ,null::text return_reas ,null::text terms ,''::text custpo @@ -303,27 +310,27 @@ SELECT ,null::date dcmdat ,null::date dhidat ,null::text fspr - ,remit_to - ,bill_class - ,bill_cust - ,bill_rep - ,bill_terr - ,ship_class - ,ship_cust - ,ship_rep - ,ship_terr - ,quota_rep - ,account - ,shipgrp - ,geo - ,chan - ,orig_ctry - ,orig_prov - ,orig_post - ,dest_ctry - ,dest_prov - ,dest_post - ,null::text part --history part mix + ,null::text remit_to + ,null::text bill_class + ,(regexp_match(jr.bill_cust_descr,'(.*?)(?= - |$)'))[1] bill_cust + ,null:: text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,(regexp_match(jr.ship_cust_descr,'(.*?)(?= - |$)'))[1] ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,null::text quota_rep + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,(regexp_match(jr.part_descr,'(.*?)(?= - |$)'))[1] part ,null::text ord_gldc ,null::text majg ,null::text ming @@ -335,227 +342,75 @@ SELECT ,null::text clss ,null::text brand ,null::text assc - ,fs_line - ,r_currency - ,r_rate + ,null::text fs_line + ,null::text r_currency + ,null::numeric r_rate ,null::text c_currency ,null::numeric c_rate ,0::numeric(15,5) ddqtoi ,0::numeric(15,5) ddqtsi ,0::numeric(15,5) fgqshp ,0::numeric(15,5) diqtsh - ,0::numeric fb_qty --history value + ,mxm.qty*mxm.momix*jr.mix fb_qty --history value ,0::numeric fb_cst_loc --history part mix ,0::numeric fb_cst_loc_cur ,0::numeric fb_cst_loc_fut - ,0::numeric fb_val_loc --history value + ,mxm.amount*mxm.momix*jr.mix fb_val_loc --history value ,0::numeric fb_val_loc_pl - ,calc_status - ,flag - ,null::date orderdate --history - ,null::date requestdate --history - ,null::date shipdate --history - ,null::date adj_orderdate --history - ,null::date adj_requestdate --history - ,null::date adj_shipdate --history + ,null::text calc_status + ,null::text flag + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) orderdate + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag requestdate + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag shipdate + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) adj_orderdate + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag adj_requestdate + ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate ,null::text "version" ,null::text iter ---------------ui columns------------------------- - ,null::numeric order_season - ,null::text order_month - ,null::numeric ship_season - ,null::text ship_month - ,null::numeric request_season - ,null::text request_month - ,null::text part_descr + ,od.ssyr order_season + ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month + ,sd.ssyr ship_season + ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month + ,rd.ssyr request_season + ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month + ,jr.part_descr part_descr ,null::text part_family ,null::text part_group ,null::text branding ,null::text color ,null::text segm - ,bill_cust_descr - ,billto_group - ,ship_cust_descr - ,shipto_group + ,jr.bill_cust_descr + ,null::text billto_group + ,jr.ship_cust_descr + ,null::text shipto_group ,null::text majg_descr ,null::text ming_descr ,null::text majs_descr ,null::text mins_descr - ,mod_chan - ,mod_chansub - ,quota_rep_descr - ,director_descr + ,null::text mod_chan + ,null::text mod_chansub + ,null::text quota_rep_descr + ,null::text director_descr ,null::numeric value_loc ,null::numeric value_usd ,null::numeric cost_loc ,null::numeric cost_usd ,null::numeric units - ,sum(coalesce(fb_val_loc * r_rate,0)) agg + ,jr.mix FROM - rlarp.osm_fcpool - WHERE - -----------------scenario---------------------------- - where_clause - -----------------additional params------------------- - AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments - AND adj_orderdate <= adj_shipdate - GROUP BY - remit_to - ,bill_class - ,bill_cust - ,bill_rep - ,bill_terr - ,ship_class - ,ship_cust - ,ship_rep - ,ship_terr - ,quota_rep - ,account - ,shipgrp - ,geo - ,chan - ,orig_ctry - ,orig_prov - ,orig_post - ,dest_ctry - ,dest_prov - ,dest_post - ,fs_line - ,r_currency - ,r_rate - ,calc_status - ,flag - ,bill_cust_descr - ,billto_group - ,ship_cust_descr - ,shipto_group - ,mod_chan - ,mod_chansub - ,quota_rep_descr - ,director_descr -) -,applyx AS ( -SELECT - b.plnt --master data - ,b."ddord#" - ,b."dditm#" - ,b."fgbol#" - ,b."fgent#" - ,b."diinv#" - ,b."dilin#" - ,mxm.promo --history - ,b.return_reas - ,b.terms --history cust mix - ,b.custpo - ,b.dhincr - ,b.diext - ,b.ditdis - ,b.dcodat - ,b.ddqdat - ,b.dcmdat - ,b.dhidat - ,sd.fspr fspr - ,b.remit_to --master data - ,b.bill_class --master data - ,b.bill_cust --history cust mix - ,b.bill_rep --master data - ,b.bill_terr --master data - ,b.ship_class --master data - ,b.ship_cust --history cust mix - ,b.ship_rep --master data - ,b.ship_terr --master data - ,b.quota_rep --master data - ,b.account --master data - ,b.shipgrp --master data - ,b.geo --master data - ,b.chan --master data - ,b.orig_ctry --master data - ,b.orig_prov --master data - ,b.orig_post --master data - ,b.dest_ctry --master data - ,b.dest_prov --master data - ,b.dest_post --master data - ,(SELECT (regexp_match(def->>'newpart','(.*) - '::text))[1] FROM target) part --history part mix - ,b.ord_gldc --master data - ,b.majg --master data - ,b.ming --master data - ,b.majs --master data - ,b.mins --master data - ,b.gldc --master data - ,b.glec --master data - ,b.harm --master data - ,b.clss --master data - ,b.brand --master data - ,b.assc --master data - ,b.fs_line --master data - ,b.r_currency --history cust mix - ,b.r_rate --master data - ,b.c_currency --master data - ,b.c_rate --master data - ,b.ddqtoi - ,b.ddqtsi - ,b.fgqshp - ,b.diqtsh - ,(b.agg/sum(b.agg) OVER (partition by mxm.*))*mxm.momix*mxm.qty fb_qty - ,b.fb_cst_loc - ,b.fb_cst_loc_cur - ,b.fb_cst_loc_fut - ,(b.agg/sum(b.agg) OVER (partition by mxm.*))*mxm.momix*mxm.amount fb_val_loc - ,b.fb_val_loc_pl - ,b.calc_status - ,b.flag - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) orderdate - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag requestdate - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag shipdate - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) adj_orderdate - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag adj_requestdate - ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate - ,'replace_version' "version" - ,'replace_source'||' new part' iter - -----------------------ui columns-------------------------------- - ,'replace_iterdet' iterdet - ,$$replace_iterdef$$::json iterdef - ,od.ssyr order_season - ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month - ,sd.ssyr ship_season - ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month - ,rd.ssyr request_season - ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month - ,b.part_descr - ,b.part_family - ,b.part_group - ,b.branding - ,b.color - ,b.segm - ,b.bill_cust_descr - ,b.billto_group - ,b.ship_cust_descr - ,b.shipto_group - ,b.majg_descr - ,b.ming_descr - ,b.majs_descr - ,b.mins_descr - ,b.mod_chan - ,b.mod_chansub - ,b.quota_rep_descr - ,b.director_descr - ,b.value_loc - ,b.value_usd - ,b.cost_loc - ,b.cost_usd - ,b.units - --,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix - --,mxm.* -FROM - basemix b - CROSS JOIN mxm - LEFT OUTER JOIN gld od ON - make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat - LEFT OUTER JOIN gld rd ON - make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag BETWEEN rd.sdat AND rd.edat - LEFT OUTER JOIN gld sd ON - make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat + target + JOIN LATERAL json_array_elements(def->'basket') as ae(e) ON true + JOIN LATERAL json_to_record(ae.e) as jr(part_descr text, bill_cust_descr text, ship_cust_descr text, mix numeric) ON true + CROSS JOIN mxm + LEFT OUTER JOIN gld od ON + make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat + LEFT OUTER JOIN gld rd ON + make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag BETWEEN rd.sdat AND rd.edat + LEFT OUTER JOIN gld sd ON + make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat ) +--SELECT * FROM basemix ,final AS ( SELECT COALESCE(m.avdplt,p.awdplt) plnt --master data