get fuctional through basemix

This commit is contained in:
Paul Trowbridge 2019-03-22 03:33:45 -04:00
parent f18b35364d
commit 966b072f72

View File

@ -2,7 +2,7 @@ WITH
/* /*
the volume must be expressed in terms of units, since that is what it will be scaling 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 --select * from newm
,GLD AS ( ,GLD AS (
SELECT SELECT
@ -179,7 +179,13 @@ target AS (select $$replace_request$$::json def)
mseq.m = order_month mseq.m = order_month
WHERE WHERE
-----------------scenario---------------------------- -----------------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------------------- -----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND adj_orderdate <= adj_shipdate
@ -281,7 +287,8 @@ FROM
LIMIT 1 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 ( ,basemix AS (
SELECT SELECT
null::text plnt ---master data null::text plnt ---master data
@ -291,7 +298,7 @@ SELECT
,0::numeric(11,0) "fgent#" ,0::numeric(11,0) "fgent#"
,0::numeric(9,0) "diinv#" ,0::numeric(9,0) "diinv#"
,0::numeric(3,0) "dilin#" ,0::numeric(3,0) "dilin#"
,null::text promo --history ,mxm.promo --history
,null::text return_reas ,null::text return_reas
,null::text terms ,null::text terms
,''::text custpo ,''::text custpo
@ -303,27 +310,27 @@ SELECT
,null::date dcmdat ,null::date dcmdat
,null::date dhidat ,null::date dhidat
,null::text fspr ,null::text fspr
,remit_to ,null::text remit_to
,bill_class ,null::text bill_class
,bill_cust ,(regexp_match(jr.bill_cust_descr,'(.*?)(?= - |$)'))[1] bill_cust
,bill_rep ,null:: text bill_rep
,bill_terr ,null::text bill_terr
,ship_class ,null::text ship_class
,ship_cust ,(regexp_match(jr.ship_cust_descr,'(.*?)(?= - |$)'))[1] ship_cust
,ship_rep ,null::text ship_rep
,ship_terr ,null::text ship_terr
,quota_rep ,null::text quota_rep
,account ,null::text account
,shipgrp ,null::text shipgrp
,geo ,null::text geo
,chan ,null::text chan
,orig_ctry ,null::text orig_ctry
,orig_prov ,null::text orig_prov
,orig_post ,null::text orig_post
,dest_ctry ,null::text dest_ctry
,dest_prov ,null::text dest_prov
,dest_post ,null::text dest_post
,null::text part --history part mix ,(regexp_match(jr.part_descr,'(.*?)(?= - |$)'))[1] part
,null::text ord_gldc ,null::text ord_gldc
,null::text majg ,null::text majg
,null::text ming ,null::text ming
@ -335,219 +342,66 @@ SELECT
,null::text clss ,null::text clss
,null::text brand ,null::text brand
,null::text assc ,null::text assc
,fs_line ,null::text fs_line
,r_currency ,null::text r_currency
,r_rate ,null::numeric r_rate
,null::text c_currency ,null::text c_currency
,null::numeric c_rate ,null::numeric c_rate
,0::numeric(15,5) ddqtoi ,0::numeric(15,5) ddqtoi
,0::numeric(15,5) ddqtsi ,0::numeric(15,5) ddqtsi
,0::numeric(15,5) fgqshp ,0::numeric(15,5) fgqshp
,0::numeric(15,5) diqtsh ,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 --history part mix
,0::numeric fb_cst_loc_cur ,0::numeric fb_cst_loc_cur
,0::numeric fb_cst_loc_fut ,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 ,0::numeric fb_val_loc_pl
,calc_status ,null::text calc_status
,flag ,null::text 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 "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
,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
,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::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
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) orderdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag requestdate ,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) + rlag + slag shipdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) adj_orderdate ,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 adj_requestdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate
,'replace_version' "version" ,null::text "version"
,'replace_source'||' new part' iter ,null::text iter
-----------------------ui columns-------------------------------- ---------------ui columns-------------------------
,'replace_iterdet' iterdet
,$$replace_iterdef$$::json iterdef
,od.ssyr order_season ,od.ssyr order_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month
,sd.ssyr ship_season ,sd.ssyr ship_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month
,rd.ssyr request_season ,rd.ssyr request_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month
,b.part_descr ,jr.part_descr part_descr
,b.part_family ,null::text part_family
,b.part_group ,null::text part_group
,b.branding ,null::text branding
,b.color ,null::text color
,b.segm ,null::text segm
,b.bill_cust_descr ,jr.bill_cust_descr
,b.billto_group ,null::text billto_group
,b.ship_cust_descr ,jr.ship_cust_descr
,b.shipto_group ,null::text shipto_group
,b.majg_descr ,null::text majg_descr
,b.ming_descr ,null::text ming_descr
,b.majs_descr ,null::text majs_descr
,b.mins_descr ,null::text mins_descr
,b.mod_chan ,null::text mod_chan
,b.mod_chansub ,null::text mod_chansub
,b.quota_rep_descr ,null::text quota_rep_descr
,b.director_descr ,null::text director_descr
,b.value_loc ,null::numeric value_loc
,b.value_usd ,null::numeric value_usd
,b.cost_loc ,null::numeric cost_loc
,b.cost_usd ,null::numeric cost_usd
,b.units ,null::numeric units
--,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix ,jr.mix
--,mxm.* FROM
FROM target
basemix b 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 CROSS JOIN mxm
LEFT OUTER JOIN gld od ON LEFT OUTER JOIN gld od ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat
@ -556,6 +410,7 @@ FROM
LEFT OUTER JOIN gld sd ON LEFT OUTER JOIN gld sd ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
) )
--SELECT * FROM basemix
,final AS ( ,final AS (
SELECT SELECT
COALESCE(m.avdplt,p.awdplt) plnt --master data COALESCE(m.avdplt,p.awdplt) plnt --master data