now working and totaling up to match target adjustment

This commit is contained in:
Paul Trowbridge 2019-03-05 10:48:21 -05:00
parent 1a1a4424e5
commit 9d20bee6d1

View File

@ -1,21 +1,22 @@
WITH
mseq AS (
target AS (select 50000 incr)
,mseq AS (
SELECT * FROM
(
VALUES
('Jun',1)
,('Jul',2)
,('Aug',3)
,('Sep',4)
,('Oct',5)
,('Nov',6)
,('Dec',7)
,('Jan',8)
,('Feb',9)
,('Mar',10)
,('Apr',11)
,('May',12)
) x(m,s)
('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
@ -63,14 +64,17 @@ mseq AS (
GROUP BY
extract(day FROM orderdate)
)
---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)---------------
,mmix AS (
SELECT
to_char(orderdate,'Mon') _month
,seq
,promo
,extract(day from orderdate) odom
,requestdate-orderdate rlag
,shipdate - requestdate slag
,sum(extract(day from orderdate)*value_usd) dom_wa
--,requestdate-orderdate rlag
,sum((requestdate-orderdate)*(value_usd)) rlag_wa
--,shipdate - requestdate slag
,sum((shipdate - requestdate)*(value_usd)) slag_wa
,sum(value_usd) value_usd
FROM
alldates
@ -78,37 +82,37 @@ mseq AS (
to_char(orderdate,'Mon')
,seq
,promo
,extract(day from orderdate)
,requestdate-orderdate
,shipdate - requestdate
--,extract(day from orderdate)
--,requestdate-orderdate
--,shipdate - requestdate
)
,mmixp AS (
SELECT
_month
,seq
,promo
,odom
,rlag
,slag
,value_usd
,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc
,round((dom_wa/value_usd)::numeric,0)::int odom
,round((rlag_wa/value_usd)::numeric,0)::int rlag
,round((slag_wa/value_usd)::numeric,0)::int slag
,value_usd/sum(value_usd) over (partition by _month) momix
--,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc
FROM
mmix
)
--SELECT * FROM mmixp ORDER BY seq ASC, vperc desc
--SELECT * FROM mmixp order by seq asc, vperc desc
,basemix AS (
SELECT
plnt ---master data
,null::numeric(11,0) "ddord#" --0
,null::numeric(11,0) "dditm#" --0
,null::numeric(11,0) "fgbol#" --0
,null::numeric(11,0) "fgent#" --0
,null::numeric(9,0) "diinv#" --0
,null::numeric(3,0) "dilin#" --0
,0::numeric(11,0) "ddord#" --0
,0::numeric(11,0) "dditm#" --0
,0::numeric(11,0) "fgbol#" --0
,0::numeric(11,0) "fgent#" --0
,0::numeric(9,0) "diinv#" --0
,0::numeric(3,0) "dilin#" --0
,null::text promo --history date mix
,null::text return_reas --0
,terms
,null::text custpo --0
,''::text custpo --0
,'I' dhincr --0
,sum(diext) diext
,sum(ditdis) ditdis
@ -174,6 +178,36 @@ SELECT
,null::date adj_shipdate --history
,'b20' "version" --calculated
,'adjustment' iter --calculated
---------------ui columns-------------------------
,null order_season
,null order_month
,null ship_season
,null ship_month
,null request_season
,null request_month
,part_descr
,part_family
,part_group
,branding
,color
,segm
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
,null value_loc
,null value_usd
,null cost_loc
,null cost_usd
,null units
FROM
rlarp.osm_fcpool
WHERE
@ -229,7 +263,32 @@ SELECT
,c_rate --master data
,calc_status
,flag
,part_descr
,part_family
,part_group
,branding
,color
,segm
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
)
,scale AS (
SELECT
(SELECT incr::numeric FROM target) incr
,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base
,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) FROM basemix) factor
)
,final AS (
SELECT
b.plnt --master data
,b."ddord#" --0
@ -291,24 +350,58 @@ SELECT
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --0
,b.fb_qty --history value
,b.fb_cst_loc --history part mix
,b.fb_cst_loc_cur --master data
,b.fb_cst_loc_fut --master data
,b.fb_val_loc --history value
,b.fb_val_loc_pl --0
,b.fb_qty*s.factor*m.momix fb_qty
,b.fb_cst_loc*s.factor*m.momix fb_cst_loc
,b.fb_cst_loc_cur*s.factor*m.momix fb_cst_loc_cur
,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut
,b.fb_val_loc*s.factor*m.momix fb_val_loc
,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
,b.calc_status --0
,b.flag --0
,b.orderdate --history date mix
,b.requestdate --history date mix
,b.shipdate --history date mix
,b.adj_orderdate --history
,b.adj_requestdate --history
,b.adj_shipdate --history
,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
,b.version --calculated
,b.iter --calculated
,(b.fb_val_loc * r_rate)/sum(b.fb_val_loc * r_rate) over () valperc
-----------------------ui columns--------------------------------
,b.order_season
,b.order_month
,b.ship_season
,b.ship_month
,b.request_season
,b.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
FROM
basemix b
ORDER BY
valperc desc
CROSS JOIN scale s
CROSS JOIN mmixp m
LEFT OUTER JOIN mseq ON
mseq.m = m._month
WHERE
m._month = 'Jul'
)