Compare commits

..

No commits in common. "a83708a7ca969e854d694323f38419f9a7097cd7" and "7635380ccc21f2a4ed291df3d558991ce0bc33ea" have entirely different histories.

2 changed files with 562 additions and 358 deletions

View File

@ -9,20 +9,21 @@ WITH
target AS (select target_increment incr) target AS (select target_increment incr)
,testv AS ( ,testv AS (
SELECT SELECT
sum(units) tot sum(fb_qty) tot
,sum(units) FILTER (WHERE iter = 'copy') base ,sum(fb_qty) FILTER (WHERE iter = 'copy') base
,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart ,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart
,sum(value_loc *r_rate) totsales ,sum(fb_val_loc *r_rate) totsales
,sum(value_loc *r_rate) FILTER (WHERE iter = 'copy') basesales ,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales ,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales
FROM FROM
rlarp.osm_pool rlarp.osm_fcpool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
-----------------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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
) )
,flagv AS ( ,flagv AS (
SELECT SELECT
@ -67,7 +68,6 @@ target AS (select target_increment incr)
,to_char(N1FSYP,'FM0000') FSPR ,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT ,N1SD01 SDAT
,N1ED01 EDAT ,N1ED01 EDAT
,daterange(n1sd01, n1ed01) drange
,to_char(N1ED01,'yymm') CAPR ,to_char(N1ED01,'yymm') CAPR
,N1ED01 - N1SD01 +1 NDAYS ,N1ED01 - N1SD01 +1 NDAYS
,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
@ -85,18 +85,18 @@ target AS (select target_increment incr)
SELECT * FROM SELECT * FROM
( (
VALUES VALUES
('01 - Jun',1,6,-1) ('Jun',1,6,-1)
,('02 - Jul',2,7,-1) ,('Jul',2,7,-1)
,('03 - Aug',3,8,-1) ,('Aug',3,8,-1)
,('04 - Sep',4,9,-1) ,('Sep',4,9,-1)
,('05 - Oct',5,10,-1) ,('Oct',5,10,-1)
,('06 - Nov',6,11,-1) ,('Nov',6,11,-1)
,('07 - Dec',7,12,-1) ,('Dec',7,12,-1)
,('08 - Jan',8,1,0) ,('Jan',8,1,0)
,('09 - Feb',9,2,0) ,('Feb',9,2,0)
,('10 - Mar',10,3,0) ,('Mar',10,3,0)
,('11 - Apr',11,4,0) ,('Apr',11,4,0)
,('12 - May',12,5,0) ,('May',12,5,0)
) x(m,s,cal,yr) ) x(m,s,cal,yr)
) )
,alldates AS ( ,alldates AS (
@ -105,68 +105,68 @@ target AS (select target_increment incr)
,terms ,terms
,order_month ,order_month
,mseq.s seq ,mseq.s seq
,order_date ,orderdate
,request_date ,requestdate
,ship_date ,shipdate
,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM FROM
rlarp.osm_pool rlarp.osm_fcpool
LEFT OUTER JOIN mseq ON LEFT OUTER JOIN mseq ON
mseq.m = order_month mseq.m = order_month
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
-----------------additional params------------------- -----------------additional params-------------------
AND version = 'b21' AND version = 'b20'
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket' WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END END
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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
promo promo
,terms ,terms
,order_month ,order_month
,mseq.s ,mseq.s
,order_date ,orderdate
,request_date ,requestdate
,ship_date ,shipdate
HAVING HAVING
sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0 sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
) )
--select * from alldates --select * from alldates
,dom AS ( ,dom AS (
SELECT SELECT
extract(day FROM order_date) dom extract(day FROM orderdate) dom
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
extract(day FROM order_date) 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)--------------- ---------------------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(order_date,'Mon') _month to_char(orderdate,'Mon') _month
,seq ,seq
,promo ,promo
,sum(extract(day from order_date)*value_usd) dom_wa ,sum(extract(day from orderdate)*value_usd) dom_wa
--,request_date-order_date rlag --,requestdate-orderdate rlag
,sum((request_date-order_date)*(value_usd)) rlag_wa ,sum((requestdate-orderdate)*(value_usd)) rlag_wa
--,ship_date - request_date slag --,shipdate - requestdate slag
,sum((ship_date - request_date)*(value_usd)) slag_wa ,sum((shipdate - requestdate)*(value_usd)) slag_wa
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
to_char(order_date,'Mon') to_char(orderdate,'Mon')
,seq ,seq
,promo ,promo
--,extract(day from order_date) --,extract(day from orderdate)
--,request_date-order_date --,requestdate-orderdate
--,ship_date - request_date --,shipdate - requestdate
) )
,targm AS (select s, m from mseq where m = 'target_month' ) ,targm AS (select s, m from mseq where m = 'target_month' )
,mmixp AS ( ,mmixp AS (
@ -255,20 +255,20 @@ SELECT
,0::numeric(15,5) ddqtsi --0 ,0::numeric(15,5) ddqtsi --0
,0::numeric(15,5) fgqshp --0 ,0::numeric(15,5) fgqshp --0
,0::numeric(15,5) diqtsh --0 ,0::numeric(15,5) diqtsh --0
,sum(coalesce(units,0)) units --history value ,sum(coalesce(fb_qty,0)) fb_qty --history value
,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix ,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix
,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data ,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data
,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data ,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data
,sum(coalesce(value_loc,0)) value_loc --history value ,sum(coalesce(fb_val_loc,0)) fb_val_loc --history value
,sum(coalesce(value_loc_pl,0)) value_loc_pl --0 ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
,calc_status --0 ,calc_status --0
,flag --0 ,flag --0
,null::date order_date --history date mix ,null::date orderdate --history date mix
,null::date request_date --history date mix ,null::date requestdate --history date mix
,null::date ship_date --history date mix ,null::date shipdate --history date mix
,null::date order_date --history ,null::date adj_orderdate --history
,null::date adj_request_date --history ,null::date adj_requestdate --history
,null::date ship_date --history ,null::date adj_shipdate --history
---------------ui columns------------------------- ---------------ui columns-------------------------
,null::numeric order_season ,null::numeric order_season
,null::text order_month ,null::text order_month
@ -300,7 +300,7 @@ SELECT
,null cost_usd ,null cost_usd
,null units ,null units
FROM FROM
rlarp.osm_pool rlarp.osm_fcpool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
@ -308,10 +308,10 @@ SELECT
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket' WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END END
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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
plnt ---master data plnt ---master data
,terms ,terms
@ -376,8 +376,8 @@ SELECT
,scale AS ( ,scale AS (
SELECT SELECT
(SELECT incr::numeric FROM target) incr (SELECT incr::numeric FROM target) incr
,(SELECT sum(units) FROM basemix) base ,(SELECT sum(fb_qty) FROM basemix) base
,(SELECT incr::numeric FROM target)/(SELECT sum(units) FROM basemix) factor ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_qty) FROM basemix) factor
) )
,final AS ( ,final AS (
SELECT SELECT
@ -441,20 +441,20 @@ SELECT
,b.ddqtsi --0 ,b.ddqtsi --0
,b.fgqshp --0 ,b.fgqshp --0
,b.diqtsh --0 ,b.diqtsh --0
,b.units*s.factor*m.momix units ,b.fb_qty*s.factor*m.momix fb_qty
,b.fb_cst_loc*s.factor*m.momix fb_cst_loc ,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_cur*s.factor*m.momix fb_cst_loc_cur
,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut ,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut
,b.value_loc*s.factor*m.momix value_loc ,b.fb_val_loc*s.factor*m.momix fb_val_loc
,b.value_loc_pl*s.factor*m.momix value_loc_pl ,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
,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag request_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_request_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
,'replace_version' "version" --calculated ,'replace_version' "version" --calculated
,'replace_source'||' volume' iter --calculated ,'replace_source'||' volume' iter --calculated
-----------------------ui columns-------------------------------- -----------------------ui columns--------------------------------
@ -484,11 +484,11 @@ SELECT
,b.mod_chansub ,b.mod_chansub
,b.quota_rep_descr ,b.quota_rep_descr
,b.director_descr ,b.director_descr
,(b.value_loc*s.factor*m.momix)::numeric value_loc ,(b.fb_val_loc*s.factor*m.momix)::numeric value_loc
,(b.value_loc*s.factor*m.momix*r_rate)::numeric value_usd ,(b.fb_val_loc*s.factor*m.momix*r_rate)::numeric value_usd
,(b.fb_cst_loc*s.factor*m.momix)::numeric cost_loc ,(b.fb_cst_loc*s.factor*m.momix)::numeric cost_loc
,(b.fb_cst_loc*s.factor*m.momix*c_rate)::numeric cost_usd ,(b.fb_cst_loc*s.factor*m.momix*c_rate)::numeric cost_usd
,(b.units*s.factor*m.momix)::numeric units ,(b.fb_qty*s.factor*m.momix)::numeric units
FROM FROM
basemix b basemix b
CROSS JOIN scale s CROSS JOIN scale s
@ -506,7 +506,7 @@ WHERE
m._month = (SELECT _month FROM closest) m._month = (SELECT _month FROM closest)
) )
, ins AS ( , ins AS (
INSERT INTO rlarp.osm_pool SELECT * FROM final RETURNING * INSERT INTO rlarp.osm_fcpool SELECT * FROM final RETURNING *
) )
,insagg AS ( ,insagg AS (
SELECT SELECT

View File

@ -5,20 +5,21 @@ the volume must be expressed in terms of units, since that is what it will be sc
target AS (select target_volume vincr, target_price pincr) target AS (select target_volume vincr, target_price pincr)
,testv AS ( ,testv AS (
SELECT SELECT
sum(units) tot sum(fb_qty) tot
,sum(units) FILTER (WHERE iter = 'copy') base ,sum(fb_qty) FILTER (WHERE iter = 'copy') base
,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart ,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart
,sum(value_loc *r_rate) totsales ,sum(fb_val_loc *r_rate) totsales
,sum(value_loc *r_rate) FILTER (WHERE iter = 'copy') basesales ,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales ,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales
FROM FROM
rlarp.osm_pool rlarp.osm_fcpool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
where_clause where_clause
-----------------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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
) )
,flagv AS ( ,flagv AS (
SELECT SELECT
@ -54,7 +55,7 @@ target AS (select target_volume vincr, target_price pincr)
FROM FROM
testv testv
) )
,GLD AS MATERIALIZED ( ,GLD AS (
SELECT SELECT
N1COMP COMP N1COMP COMP
,N1CCYY FSYR ,N1CCYY FSYR
@ -63,7 +64,6 @@ target AS (select target_volume vincr, target_price pincr)
,to_char(N1FSYP,'FM0000') FSPR ,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT ,N1SD01 SDAT
,N1ED01 EDAT ,N1ED01 EDAT
,daterange(n1sd01, n1ed01) drange
,to_char(N1ED01,'yymm') CAPR ,to_char(N1ED01,'yymm') CAPR
,N1ED01 - N1SD01 +1 NDAYS ,N1ED01 - N1SD01 +1 NDAYS
,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
@ -75,37 +75,38 @@ target AS (select target_volume vincr, target_price pincr)
KPCCYY = N1CCYY KPCCYY = N1CCYY
WHERE WHERE
N1COMP = 93 N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
) )
,mseq AS ( ,mseq AS (
SELECT * FROM SELECT * FROM
( (
VALUES VALUES
('01 - Jun',1,6,-1) ('Jun',1,6,-1)
,('02 - Jul',2,7,-1) ,('Jul',2,7,-1)
,('03 - Aug',3,8,-1) ,('Aug',3,8,-1)
,('04 - Sep',4,9,-1) ,('Sep',4,9,-1)
,('05 - Oct',5,10,-1) ,('Oct',5,10,-1)
,('06 - Nov',6,11,-1) ,('Nov',6,11,-1)
,('07 - Dec',7,12,-1) ,('Dec',7,12,-1)
,('08 - Jan',8,1,0) ,('Jan',8,1,0)
,('09 - Feb',9,2,0) ,('Feb',9,2,0)
,('10 - Mar',10,3,0) ,('Mar',10,3,0)
,('11 - Apr',11,4,0) ,('Apr',11,4,0)
,('12 - May',12,5,0) ,('May',12,5,0)
) x(m,s,cal,yr) ) x(m,s,cal,yr)
) )
,alldates AS MATERIALIZED( ,alldates AS (
SELECT SELECT
promo promo
,terms ,terms
,order_month ,order_month
,mseq.s seq ,mseq.s seq
,order_date ,orderdate
,request_date ,requestdate
,ship_date ,shipdate
,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM FROM
rlarp.osm_pool rlarp.osm_fcpool
LEFT OUTER JOIN mseq ON LEFT OUTER JOIN mseq ON
mseq.m = order_month mseq.m = order_month
WHERE WHERE
@ -115,52 +116,52 @@ target AS (select target_volume vincr, target_price pincr)
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket' WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END END
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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
promo promo
,terms ,terms
,order_month ,order_month
,mseq.s ,mseq.s
,order_date ,orderdate
,request_date ,requestdate
,ship_date ,shipdate
HAVING HAVING
sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0 sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
) )
--select * from alldates --select * from alldates
,dom AS ( ,dom AS (
SELECT SELECT
extract(day FROM order_date) DOM extract(day FROM orderdate) DOM
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
extract(day FROM order_date) 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)--------------- ---------------------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(order_date,'Mon') _month to_char(orderdate,'Mon') _month
,seq ,seq
,promo ,promo
,sum(extract(day from order_date)*value_usd) dom_wa ,sum(extract(day from orderdate)*value_usd) dom_wa
--,request_date-order_date rlag --,requestdate-orderdate rlag
,sum((request_date-order_date)*(value_usd)) rlag_wa ,sum((requestdate-orderdate)*(value_usd)) rlag_wa
--,ship_date - request_date slag --,shipdate - requestdate slag
,sum((ship_date - request_date)*(value_usd)) slag_wa ,sum((shipdate - requestdate)*(value_usd)) slag_wa
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
to_char(order_date,'Mon') to_char(orderdate,'Mon')
,seq ,seq
,promo ,promo
--,extract(day from order_date) --,extract(day from orderdate)
--,request_date-order_date --,requestdate-orderdate
--,ship_date - request_date --,shipdate - requestdate
) )
,targm AS (select s, m from mseq where m = 'target_month' ) ,targm AS (select s, m from mseq where m = 'target_month' )
,mmixp AS ( ,mmixp AS (
@ -186,45 +187,117 @@ target AS (select target_volume vincr, target_price pincr)
abs(seq - targm.s) ASC abs(seq - targm.s) ASC
LIMIT 1 LIMIT 1
) )
---------------------the role of basemix here is to get non-dated info which is then dated in the next step--------------------- --SELECT * FROM mmixp order by seq asc, vperc desc
,basemix AS ( ,basemix AS (
SELECT SELECT
--fspr in next step plnt ---master data
o.plnt ,0::numeric(11,0) "ddord#" --0
--promo in next step ,0::numeric(11,0) "dditm#" --0
--terms in next step ,0::numeric(11,0) "fgbol#" --0
,o.bill_cust_descr ,0::numeric(11,0) "fgent#" --0
,o.ship_cust_descr ,0::numeric(9,0) "diinv#" --0
,o.dsm ,0::numeric(3,0) "dilin#" --0
,o.quota_rep_descr ,null::text promo --history date mix
,o.director ,null::text return_reas --0
,o.billto_group ,terms
,o.shipto_group ,''::text custpo --0
,o.chan ,'I' dhincr --0
,o.chansub ,null::numeric diext
,o.chan_retail ,null::numeric ditdis
,o.part ,null::date dcodat --calculated date mix
,o.part_descr ,null::date ddqdat --calculated date mix
,o.part_group ,null::date dcmdat --calculated date mix
,o.branding ,null::date dhidat --calculated date mix
,o.majg_descr ,null::text fspr --calculated date mix
,o.ming_descr ,remit_to --master data
,o.majs_descr ,bill_class --master data
,o.mins_descr ,bill_cust --history cust mix
,o.segm ,bill_rep --master data
,o.fs_line ,bill_terr --master data
,o.r_currency ,ship_class --master data
,o.r_rate ,ship_cust --history cust mix
,o.c_currency ,ship_rep --master data
,o.c_rate ,ship_terr --master data
,sum(coalesce(o.units,0)) units ,quota_rep --master data
,sum(coalesce(o.value_loc,0)) value_loc ,account --master data
,sum(coalesce(o.value_usd,0)) value_usd ,shipgrp --master data
,sum(coalesce(o.cost_loc,0)) cost_usd ,geo --master data
,o.calc_status ,chan --master data
,o.flag ,orig_ctry --master data
,orig_prov --master data
,orig_post --master data
,dest_ctry --master data
,dest_prov --master data
,dest_post --master data
,part --history part mix
,ord_gldc --master data
,majg --master data
,ming --master data
,majs --master data
,mins --master data
,gldc --master data
,glec --master data
,harm --master data
,clss --master data
,brand --master data
,assc --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,0::numeric(15,5) ddqtoi --0
,0::numeric(15,5) ddqtsi --0
,0::numeric(15,5) fgqshp --0
,0::numeric(15,5) diqtsh --0
,sum(coalesce(fb_qty,0)) fb_qty --history value
,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix
,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data
,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data
,sum(coalesce(fb_val_loc,0)) fb_val_loc --history value
,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
,calc_status --0
,flag --0
,null::date orderdate --history date mix
,null::date requestdate --history date mix
,null::date shipdate --history date mix
,null::date adj_orderdate --history
,null::date adj_requestdate --history
,null::date adj_shipdate --history
,'b20' "version" --calculated
,'adjustment' iter --calculated
---------------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
,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_pool o rlarp.osm_fcpool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
where_clause where_clause
@ -232,103 +305,188 @@ SELECT
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket' WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END END
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 order_date <= ship_date AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
--fspr in next step plnt ---master data
o.plnt ,terms
--promo in next step ,remit_to --master data
--terms in next step ,bill_class --master data
,o.bill_cust_descr ,bill_cust --history cust mix
,o.ship_cust_descr ,bill_rep --master data
,o.dsm ,bill_terr --master data
,o.quota_rep_descr ,ship_class --master data
,o.director ,ship_cust --history cust mix
,o.billto_group ,ship_rep --master data
,o.shipto_group ,ship_terr --master data
,o.chan ,quota_rep --master data
,o.chansub ,account --master data
,o.chan_retail ,shipgrp --master data
,o.part ,geo --master data
,o.part_descr ,chan --master data
,o.part_group ,orig_ctry --master data
,o.branding ,orig_prov --master data
,o.majg_descr ,orig_post --master data
,o.ming_descr ,dest_ctry --master data
,o.majs_descr ,dest_prov --master data
,o.mins_descr ,dest_post --master data
,o.segm ,part --history part mix
,o.fs_line ,ord_gldc --master data
,o.r_currency ,majg --master data
,o.r_rate ,ming --master data
,o.c_currency ,majs --master data
,o.c_rate ,mins --master data
,o.calc_status ,gldc --master data
,o.flag ,glec --master data
,harm --master data
,clss --master data
,brand --master data
,assc --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,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
) )
,vscale AS ( ,vscale AS (
SELECT SELECT
(SELECT vincr::numeric FROM target) incr (SELECT vincr::numeric FROM target) incr
,(SELECT sum(units) FROM basemix) base ,(SELECT sum(fb_qty) FROM basemix) base
,(SELECT vincr::numeric FROM target)/(SELECT sum(units) FROM basemix) factor ,(SELECT vincr::numeric FROM target)/(SELECT sum(fb_qty) FROM basemix) factor
) )
--select * from SCALE --select * from SCALE
,log AS (
INSERT INTO rlarp.osm_log(doc) SELECT $$replace_iterdef$$::jsonb doc RETURNING *
)
,volume AS ( ,volume AS (
SELECT SELECT
sd.fspr b.plnt --master data
,b.plnt ,b."ddord#" --0
,m.promo ,b."dditm#" --0
,b.terms ,b."fgbol#" --0
,b.bill_cust_descr ,b."fgent#" --0
,b.ship_cust_descr ,b."diinv#" --0
,b.dsm ,b."dilin#" --0
,b.quota_rep_descr ,m.promo --history date mix
,b.director ,b.return_reas --0
,b.billto_group ,b.terms --history cust mix
,b.shipto_group ,b.custpo --0
,b.chan ,b.dhincr --0
,b.chansub ,b.diext --0
,b.chan_retail ,b.ditdis --0
,b.part ,b.dcodat --calculated date mix
,b.ddqdat --calculated date mix
,b.dcmdat --calculated date mix
,b.dhidat --calculated date mix
,b.fspr --calculated date mix
,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
,b.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 --0
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --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
,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
,'replace_version' "version" --calculated
,'replace_source'||' volume' iter
-----------------------ui columns--------------------------------
,'replace_iterdet' iterdet
,$$replace_iterdef$$::jsonb iterdef
,od.ssyr order_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom),'Mon') order_month
,sd.ssyr ship_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
,rd.ssyr request_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag,'Mon') request_month
,b.part_descr ,b.part_descr
,b.part_family
,b.part_group ,b.part_group
,b.branding ,b.branding
,b.color
,b.segm
,b.bill_cust_descr
,b.billto_group
,b.ship_cust_descr
,b.shipto_group
,b.majg_descr ,b.majg_descr
,b.ming_descr ,b.ming_descr
,b.majs_descr ,b.majs_descr
,b.mins_descr ,b.mins_descr
,b.segm ,b.mod_chan
,b.fs_line ,b.mod_chansub
,b.r_currency ,b.quota_rep_descr
,b.r_rate ,b.director_descr
,b.c_currency ,b.fb_val_loc*s.factor*m.momix value_loc
,b.c_rate ,b.fb_val_loc*s.factor*m.momix*r_rate value_usd
,b.units*s.factor*m.momix units ,b.fb_cst_loc*s.factor*m.momix cost_loc
,b.value_loc*s.factor*m.momix value_loc ,b.fb_cst_loc*s.factor*m.momix*c_rate cost_usd
,b.value_usd*s.factor*m.momix value_usd ,(b.fb_qty*s.factor*m.momix) units
,b.cost_loc*s.factor*m.momix cost_loc
,b.cost_usd*s.factor*m.momix cost_usd
,b.calc_status
,b.flag
,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom),'Mon') order_month
,od.ssyr order_season
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag request_date
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag,'Mon') request_month
,rd.ssyr request_season
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
,sd.ssyr ship_season
,'replace_version' "version"
,'replace_source'||' volume' iter
,log.id
,COALESCE(log.doc->>'message','') "comment"
,log.doc->>'type' module
FROM FROM
basemix b basemix b
CROSS JOIN vscale s CROSS JOIN vscale s
@ -348,16 +506,16 @@ WHERE
,pscale AS ( ,pscale AS (
SELECT SELECT
(SELECT pincr::numeric FROM target) incr (SELECT pincr::numeric FROM target) incr
,(SELECT sum(value_loc * r_rate) FROM volume) base ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
,CASE WHEN (SELECT sum(value_loc * r_rate) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN
0 0
ELSE ELSE
((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume))/(SELECT sum(value_loc * r_rate) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume)
END factor END factor
,CASE WHEN (SELECT sum(value_loc * r_rate) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume)) <> 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume)) <> 0 THEN
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply --if the base value is -0- but the target value hasn't been achieved, derive a price to apply
((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume))/(SELECT sum(units) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_qty) FROM volume)
ELSE ELSE
0 0
END END
@ -367,55 +525,113 @@ WHERE
) )
,price AS ( ,price AS (
SELECT SELECT
sd.fspr b.plnt --master data
,b.plnt ,b."ddord#" --0
,m.promo ,b."dditm#" --0
,b.terms ,b."fgbol#" --0
,b.bill_cust_descr ,b."fgent#" --0
,b.ship_cust_descr ,b."diinv#" --0
,b.dsm ,b."dilin#" --0
,b.quota_rep_descr ,b.promo --history date mix
,b.director ,b.return_reas --0
,b.billto_group ,b.terms --history cust mix
,b.shipto_group ,b.custpo --0
,b.chan ,b.dhincr --0
,b.chansub ,b.diext --0
,b.chan_retail ,b.ditdis --0
,b.part ,b.dcodat --calculated date mix
,b.ddqdat --calculated date mix
,b.dcmdat --calculated date mix
,b.dhidat --calculated date mix
,b.fspr --calculated date mix
,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
,b.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 --0
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --0
,0 fb_qty
,0 fb_cst_loc
,0 fb_cst_loc_cur
,0 fb_cst_loc_fut
,b.fb_val_loc*p.factor fb_val_loc
,b.fb_val_loc_pl*p.factor fb_val_loc_pl
,b.calc_status --0
,b.flag --0
,b.orderdate
,b.requestdate
,b.shipdate
,b.adj_orderdate
,b.adj_requestdate
,b.adj_shipdate
,'replace_version' "version" --calculated
,'replace_source'||' price' iter --calculateds
,'replace_iterdet' iterdet
,$$replace_iterdef$$::jsonb iterdef
,b.order_season
,b.order_month
,b.ship_season
,b.ship_month
,b.request_season
,b.request_month
,b.part_descr ,b.part_descr
,b.part_family
,b.part_group ,b.part_group
,b.branding ,b.branding
,b.color
,b.segm
,b.bill_cust_descr
,b.billto_group
,b.ship_cust_descr
,b.shipto_group
,b.majg_descr ,b.majg_descr
,b.ming_descr ,b.ming_descr
,b.majs_descr ,b.majs_descr
,b.mins_descr ,b.mins_descr
,b.segm ,b.mod_chan
,b.fs_line ,b.mod_chansub
,b.r_currency ,b.quota_rep_descr
,b.r_rate ,b.director_descr
,b.c_currency ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price/b.r_rate ELSE b.fb_val_loc*p.factor END)::numeric value_loc
,b.c_rate ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price ELSE b.fb_val_loc*p.factor END)::numeric value_usd
,0::numeric units ,0 cost_loc
,(CASE WHEN p.factor = 0 THEN b.units * p.mod_price/b.r_rate ELSE b.value_loc*p.factor END)::numeric value_loc ,0 cost_usd
,(CASE WHEN p.factor = 0 THEN b.units * p.mod_price ELSE b.value_usd*p.factor END)::numeric value_usd ,0 units
,0::numeric cost_loc
,0::numeric cost_usd
,b.calc_status
,b.flag
,b.order_date
,b.order_month
,b.order_season
,b.request_date
,b.request_month
,b.request_season
,b.ship_date
,b.ship_month
,b.ship_season
,'replace_version' "version"
,'replace_source'||' volume' iter
,log.id
,COALESCE(log.doc->>'message','') "comment"
,log.doc->>'type' module
FROM FROM
volume b volume b
CROSS JOIN pscale p CROSS JOIN pscale p
@ -423,32 +639,29 @@ FROM
p.factor <> 0 or p.mod_price <> 0 p.factor <> 0 or p.mod_price <> 0
) )
, ins AS ( , ins AS (
INSERT INTO rlarp.osm_pool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING * INSERT INTO rlarp.osm_fcpool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING *
) )
,insagg AS ( ,insagg AS (
SELECT SELECT
---------customer info-----------------
bill_cust_descr bill_cust_descr
,billto_group ,billto_group
,ship_cust_descr ,ship_cust_descr
,shipto_group ,shipto_group
,quota_rep_descr ,quota_rep_descr
,director ,director_descr
,segm ,segm
,chan ,mod_chan
,chansub ,mod_chansub
---------product info------------------
,majg_descr ,majg_descr
,ming_descr ,ming_descr
,majs_descr ,majs_descr
,mins_descr ,mins_descr
--,brand ,brand
--,part_family ,part_family
,part_group ,part_group
,branding ,branding
--,color ,color
,part_descr ,part_descr
---------dates-------------------------
,order_season ,order_season
,order_month ,order_month
,ship_season ,ship_season
@ -458,40 +671,33 @@ FROM
,promo ,promo
,version ,version
,iter ,iter
,logid
,comment
,module
--------values-------------------------
,sum(value_loc) value_loc ,sum(value_loc) value_loc
,sum(value_usd) value_usd ,sum(value_usd) value_usd
,sum(cost_loc) cost_loc ,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd ,sum(cost_usd) cost_usd
,sum(units) units ,sum(units) units
FROM FROM
ins ins
GROUP BY GROUP BY
---------customer info-----------------
bill_cust_descr bill_cust_descr
,billto_group ,billto_group
,ship_cust_descr ,ship_cust_descr
,shipto_group ,shipto_group
,quota_rep_descr ,quota_rep_descr
,director ,director_descr
,segm ,segm
,chan ,mod_chan
,chansub ,mod_chansub
---------product info------------------
,majg_descr ,majg_descr
,ming_descr ,ming_descr
,majs_descr ,majs_descr
,mins_descr ,mins_descr
--,brand ,brand
--,part_family ,part_family
,part_group ,part_group
,branding ,branding
--,color ,color
,part_descr ,part_descr
---------dates-------------------------
,order_season ,order_season
,order_month ,order_month
,ship_season ,ship_season
@ -501,8 +707,6 @@ FROM
,promo ,promo
,version ,version
,iter ,iter
,logid
,comment
,module
) )
--SELECT * FROM insagg
SELECT json_agg(row_to_json(insagg)) x from insagg SELECT json_agg(row_to_json(insagg)) x from insagg