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 WITH
mseq AS ( target AS (select 50000 incr)
,mseq AS (
SELECT * FROM SELECT * FROM
( (
VALUES VALUES
('Jun',1) ('Jun',1,6,-1)
,('Jul',2) ,('Jul',2,7,-1)
,('Aug',3) ,('Aug',3,8,-1)
,('Sep',4) ,('Sep',4,9,-1)
,('Oct',5) ,('Oct',5,10,-1)
,('Nov',6) ,('Nov',6,11,-1)
,('Dec',7) ,('Dec',7,12,-1)
,('Jan',8) ,('Jan',8,1,0)
,('Feb',9) ,('Feb',9,2,0)
,('Mar',10) ,('Mar',10,3,0)
,('Apr',11) ,('Apr',11,4,0)
,('May',12) ,('May',12,5,0)
) x(m,s) ) x(m,s,cal,yr)
) )
,alldates AS ( ,alldates AS (
SELECT SELECT
@ -63,14 +64,17 @@ mseq AS (
GROUP BY GROUP BY
extract(day FROM orderdate) 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 ( ,mmix AS (
SELECT SELECT
to_char(orderdate,'Mon') _month to_char(orderdate,'Mon') _month
,seq ,seq
,promo ,promo
,extract(day from orderdate) odom ,sum(extract(day from orderdate)*value_usd) dom_wa
,requestdate-orderdate rlag --,requestdate-orderdate rlag
,shipdate - requestdate slag ,sum((requestdate-orderdate)*(value_usd)) rlag_wa
--,shipdate - requestdate slag
,sum((shipdate - requestdate)*(value_usd)) slag_wa
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
@ -78,37 +82,37 @@ mseq AS (
to_char(orderdate,'Mon') to_char(orderdate,'Mon')
,seq ,seq
,promo ,promo
,extract(day from orderdate) --,extract(day from orderdate)
,requestdate-orderdate --,requestdate-orderdate
,shipdate - requestdate --,shipdate - requestdate
) )
,mmixp AS ( ,mmixp AS (
SELECT SELECT
_month _month
,seq ,seq
,promo ,promo
,odom ,round((dom_wa/value_usd)::numeric,0)::int odom
,rlag ,round((rlag_wa/value_usd)::numeric,0)::int rlag
,slag ,round((slag_wa/value_usd)::numeric,0)::int slag
,value_usd ,value_usd/sum(value_usd) over (partition by _month) momix
,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc --,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc
FROM FROM
mmix mmix
) )
--SELECT * FROM mmixp ORDER BY seq ASC, vperc desc --SELECT * FROM mmixp order by seq asc, vperc desc
,basemix AS ( ,basemix AS (
SELECT SELECT
plnt ---master data plnt ---master data
,null::numeric(11,0) "ddord#" --0 ,0::numeric(11,0) "ddord#" --0
,null::numeric(11,0) "dditm#" --0 ,0::numeric(11,0) "dditm#" --0
,null::numeric(11,0) "fgbol#" --0 ,0::numeric(11,0) "fgbol#" --0
,null::numeric(11,0) "fgent#" --0 ,0::numeric(11,0) "fgent#" --0
,null::numeric(9,0) "diinv#" --0 ,0::numeric(9,0) "diinv#" --0
,null::numeric(3,0) "dilin#" --0 ,0::numeric(3,0) "dilin#" --0
,null::text promo --history date mix ,null::text promo --history date mix
,null::text return_reas --0 ,null::text return_reas --0
,terms ,terms
,null::text custpo --0 ,''::text custpo --0
,'I' dhincr --0 ,'I' dhincr --0
,sum(diext) diext ,sum(diext) diext
,sum(ditdis) ditdis ,sum(ditdis) ditdis
@ -174,6 +178,36 @@ SELECT
,null::date adj_shipdate --history ,null::date adj_shipdate --history
,'b20' "version" --calculated ,'b20' "version" --calculated
,'adjustment' iter --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 FROM
rlarp.osm_fcpool rlarp.osm_fcpool
WHERE WHERE
@ -229,7 +263,32 @@ SELECT
,c_rate --master data ,c_rate --master data
,calc_status ,calc_status
,flag ,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 SELECT
b.plnt --master data b.plnt --master data
,b."ddord#" --0 ,b."ddord#" --0
@ -291,24 +350,58 @@ SELECT
,b.ddqtsi --0 ,b.ddqtsi --0
,b.fgqshp --0 ,b.fgqshp --0
,b.diqtsh --0 ,b.diqtsh --0
,b.fb_qty --history value ,b.fb_qty*s.factor*m.momix fb_qty
,b.fb_cst_loc --history part mix ,b.fb_cst_loc*s.factor*m.momix fb_cst_loc
,b.fb_cst_loc_cur --master data ,b.fb_cst_loc_cur*s.factor*m.momix fb_cst_loc_cur
,b.fb_cst_loc_fut --master data ,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut
,b.fb_val_loc --history value ,b.fb_val_loc*s.factor*m.momix fb_val_loc
,b.fb_val_loc_pl --0 ,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
,b.calc_status --0 ,b.calc_status --0
,b.flag --0 ,b.flag --0
,b.orderdate --history date mix ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
,b.requestdate --history date mix ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
,b.shipdate --history date mix ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
,b.adj_orderdate --history ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
,b.adj_requestdate --history ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
,b.adj_shipdate --history ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
,b.version --calculated ,b.version --calculated
,b.iter --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 FROM
basemix b basemix b
ORDER BY CROSS JOIN scale s
valperc desc CROSS JOIN mmixp m
LEFT OUTER JOIN mseq ON
mseq.m = m._month
WHERE
m._month = 'Jul'
)