work on last price

This commit is contained in:
Paul Trowbridge 2021-04-20 16:11:34 -04:00
parent 66b74e31cb
commit 683921dda0

View File

@ -4,36 +4,36 @@ WITH
------------------goal price increases--------------------- ------------------goal price increases---------------------
incr AS ( incr AS (
SELECT * FROM (VALUES SELECT * FROM (VALUES
('110','PP','B',0.25), ('110','PP','B',0.3),
('110','PP','T',0.25), ('110','PP','T',0.3),
('110','PP','L',0.4), ('110','PP','L',0.4),
('110','PP','M',0.4), ('110','PP','M',0.4),
('110','PP','P',0.4), ('110','PP','P',0.4),
('110','PP','C',0.4), ('110','PP','C',0.4),
('210','PE','B',0.25), ('210','PE','B',0.3),
('210','PE','T',0.25), ('210','PE','T',0.3),
('210','PE','L',0.25), ('210','PE','L',0.3),
('210','PE','M',0.25), ('210','PE','M',0.3),
('210','PE','P',0.25), ('210','PE','P',0.3),
('210','PE','C',0.25), ('210','PE','C',0.3),
('310','PE','B',0.15), ('310','PE','B',0.25),
('310','PE','T',0.25), ('310','PE','T',0.3),
('310','PE','L',0.25), ('310','PE','L',0.3),
('310','PE','M',0.25), ('310','PE','M',0.3),
('310','PE','P',0.25), ('310','PE','P',0.3),
('310','PE','C',0.25), ('310','PE','C',0.3),
('310','PP','B',0.13), ('310','PP','B',0.25),
('310','PP','T',0.16), ('310','PP','T',0.25),
('310','PP','L',0.16), ('310','PP','L',0.25),
('310','PP','M',0.16), ('310','PP','M',0.25),
('310','PP','P',0.16), ('310','PP','P',0.25),
('310','PP','C',0.16), ('310','PP','C',0.25),
('310','PS','B',0.13), ('310','PS','B',0.25),
('310','PS','T',0.16), ('310','PS','T',0.25),
('310','PS','L',0.16), ('310','PS','L',0.25),
('310','PS','M',0.16), ('310','PS','M',0.25),
('310','PS','P',0.16), ('310','PS','P',0.25),
('310','PS','C',0.16), ('310','PS','C',0.25),
('320','PE','B',0.25), ('320','PE','B',0.25),
('320','PE','T',0.25), ('320','PE','T',0.25),
('320','PE','L',0.25), ('320','PE','L',0.25),
@ -46,21 +46,13 @@ incr AS (
('320','PP','M',0.25), ('320','PP','M',0.25),
('320','PP','P',0.25), ('320','PP','P',0.25),
('320','PP','C',0.25), ('320','PP','C',0.25),
('910','PE','B',0.15), ('910','PE','B',0.25),
('910','PE','D',0.25), ('910','PE','D',0.25),
('910','PE','F',0.25), ('910','PE','F',0.25),
('910','PP','B',0.15), ('910','PP','B',0.25),
('910','PP','D',0.25), ('910','PP','D',0.25),
('910','PP','F',0.25), ('910','PP','F',0.25),
('910','PS','B',0.15), ('910','PS','B',0.25),
('910','','B',0.15),
('910','','D',0.25),
('910','','F',0.25),
('910','','T',0.25),
('910','','L',0.25),
('910','','M',0.25),
('910','','P',0.25),
('910','','C',0.25),
('910','PS','D',0.25), ('910','PS','D',0.25),
('910','PS','F',0.25), ('910','PS','F',0.25),
('610','','B',0.02), ('610','','B',0.02),
@ -127,7 +119,8 @@ incr AS (
,o.shipgrp ,o.shipgrp
,o.odate DESC ,o.odate DESC
) )
--SELECT * FROM p WHERE account ~ 'ACOSTA' SELECT * FROM p WHERE account ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' and chgrp = 'W' order by rn ASC
------------build global py asp------------------ ------------build global py asp------------------
,baseline AS ( ,baseline AS (
SELECT SELECT
@ -144,47 +137,47 @@ incr AS (
,assc ,assc
,chgrp ,chgrp
) )
----------pivot the pricing out into columns per customer/product--------
,pivot AS (
SELECT SELECT
p.product p.product
,p.styc --,p.styc
,p.glec --,p.glec
,p.majg ,p.majg
,p.assc ,p.assc
,p.colgrp ,p.colgrp
,p.coltier --,p.coltier
,p.sizc --,p.sizc
,p.suffix --,p.suffix
,p.chgrp ,p.chgrp
,p.account ,p.account
,p.shipgrp ,p.shipgrp
,bl.py_gasp --,bl.py_gasp
,sum(units) FILTER (WHERE oseas = 2020) py_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units ,sum(units) FILTER (WHERE oseas = 2021) cy_units
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp ,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(avg(price) FILTER (WHERE rn = 1),5) last_price ,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,max(odate) FILTER (WHERE rn = 1) last_order ,max(odate) FILTER (WHERE rn = 1) last_order
,i.rate incr_rate
,CASE p.chgrp ,CASE p.chgrp
WHEN 'D' THEN i.rate WHEN 'D' THEN .5
ELSE CASE p.majg ELSE CASE p.majg
WHEN '610' THEN .02 WHEN '610' THEN .02
ElSE CASE p.colgrp ElSE CASE p.colgrp
WHEN 'B' THEN .1 WHEN 'B' THEN .15
WHEN 'C' THEN .15 WHEN 'C' THEN .20
ELSE 1 ELSE 1
END END
END END
END rate END rate
,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
THEN 'unknown' -- THEN 'unknown'
ELSE 'new' -- ELSE 'new'
END -- END
ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
THEN 'lost' -- THEN 'lost'
ELSE 'repeat' -- ELSE 'repeat'
END -- END
END flag --END flag
FROM FROM
p p
LEFT OUTER JOIN baseline bl ON LEFT OUTER JOIN baseline bl ON
@ -197,77 +190,40 @@ FROM
AND i.assc = p.assc AND i.assc = p.assc
AND i.coltier = p.coltier AND i.coltier = p.coltier
AND p.glec <> '1RE' AND p.glec <> '1RE'
WHERE
p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W'
GROUP BY GROUP BY
p.product p.product
,p.styc --,p.styc
,p.glec --,p.glec
,p.majg ,p.majg
,p.assc ,p.assc
,p.coltier --,p.coltier
,p.colgrp ,p.colgrp
,p.sizc --,p.sizc
,p.suffix --,p.suffix
,p.chgrp ,p.chgrp
,p.account ,p.account
,p.shipgrp ,p.shipgrp
,bl.py_gasp --,bl.py_gasp
,i.rate ,i.rate
)
----------------create the new price-----------------
,adj AS (
SELECT ----------calculate pricing as it sits in the forecast--------------
p.product
,p.styc
,p.glec
,p.majg
,p.assc
,p.coltier
,p.sizc
,p.suffix
,p.account
,p.shipgrp
,p.py_gasp
,p.cy_units
,p.py_asp
,p.last_price
,p.last_order
,p.rate
,p.flag
,CASE p.flag
----------------------if repeat business then get to prior year + target %--------------------------------------------------------------
WHEN 'repeat' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
WHEN 'lost' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
----------------------if new business, move towards py_gasp + target % : lesser of py gloabl + target or last + target------------------
WHEN 'new' THEN least(last_price * COALESCE(1+rate,1) - last_price,greatest(py_gasp * COALESCE(1+rate,1) - last_price,0))
END price_increment
FROM
pivot p
)
--SELECT * FROM adj LIMIT 10000
--------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"application of last price and target increases to all forecast orders",
"tag":"last price",
"type":"build"
}$$::jsonb doc
RETURNING *
)
,poolprice AS ( ,poolprice AS (
SELECT SELECT
i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product
,o.quota_rep_descr ,o.quota_rep_descr
,substring(majg,1,3) majg
,substring(chan,1,1) chgrp
,o.billto_group ,o.billto_group
,o.shipto_group ,o.shipto_group
,order_season ,order_season
,sum(units) units ,round(sum(units) ,2) fc_units
,sum(value_loc) valloc --,round(sum(value_loc) ,2) valloc
,sum(value_usd) valusd --,round(sum(value_usd) ,2) valusd
,sum(o.value_loc)/sum(o.units) price ,round(sum(o.value_loc)/sum(o.units),5) fc_price
,jsonb_agg(DISTINCT iter) iters ,jsonb_agg(DISTINCT iter) iters
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
@ -282,125 +238,306 @@ WHERE
GROUP BY GROUP BY
i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
,o.quota_rep_descr ,o.quota_rep_descr
,substring(majg,1,3)
,substring(chan,1,1)
,o.billto_group ,o.billto_group
,o.shipto_group ,o.shipto_group
,order_season ,order_season
--AND iter <> 'upload price' --AND iter <> 'upload price'
HAVING
sum(o.units) <> 0
) )
, pooladj AS ( ----------pivot the pricing out into columns per customer/product--------
,pivot AS (
SELECT SELECT
p.product p.product
,p.quota_rep_descr --,p.styc
,p.billto_group --,p.glec
,p.shipto_group ,p.majg
,p.price ,p.assc
,a.py_gasp ,p.colgrp
,a.rate --,p.coltier
,a.last_price --,p.sizc
,a.price_increment --,p.suffix
,p.chgrp
,p.account
,p.shipgrp
,bl.py_gasp
,sum(units) FILTER (WHERE oseas = 2020) py_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,max(odate) FILTER (WHERE rn = 1) last_order
,CASE p.chgrp
WHEN 'D' THEN i.rate
ELSE CASE p.majg
WHEN '610' THEN .02
ElSE CASE p.colgrp
WHEN 'B' THEN .15
WHEN 'C' THEN .20
ELSE 1
END
END
END rate
--,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
-- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'unknown'
-- ELSE 'new'
-- END
-- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'lost'
-- ELSE 'repeat'
-- END
--END flag
FROM FROM
poolprice p p
,adj a LEFT OUTER JOIN baseline bl ON
WHERE bl.product = p.product
a.product = p.product AND bl.majg = p.majg
AND a.account = p.billto_group AND bl.assc = p.assc
AND a.shipgrp = p.shipto_group AND bl.chgrp = p.chgrp
LEFT OUTER JOIN incr i ON
i.majg = p.majg
AND i.assc = p.assc
AND i.coltier = p.coltier
AND p.glec <> '1RE'
GROUP BY
p.product
--,p.styc
--,p.glec
,p.majg
,p.assc
--,p.coltier
,p.colgrp
--,p.sizc
--,p.suffix
,p.chgrp
,p.account
,p.shipgrp
,bl.py_gasp
,i.rate
) )
SELECT * FROM pooladj WHERE product ~ 'STG06000' AND shipto_group = 'BWI' limit 100 ,test_unique AS (
--SELECT * FROM poolprice WHERE product ~ 'TCA06600' AND shipto_group = 'BWI' AND quota_rep_descr = 'BRYAN HILL' LIMIT 1000 SELECT
-------------build the iteration rows---------------- p.*
--,ins AS ( ,count(*) OVER (partition by product, chgrp, account, shipgrp) cnt
FROM
pivot p
)
SELECT * FROM test_unique where cnt > 1
--------------join forecast price-------------------
--,fcp AS (
-- SELECT -- SELECT
-- o.fspr -- pp.product
-- ,o.plnt ---master data -- ,pp.quota_rep_descr
-- ,o.promo --history date mix -- ,pp.order_season
-- ,o.terms -- ,pp.billto_group
-- ,o.bill_cust_descr --history cust mix -- ,pp.shipto_group
-- ,o.ship_cust_descr --history cust mix -- ,p.py_gasp
-- ,o.dsm -- ,p.cy_units
-- ,o.quota_rep_descr --master data -- ,p.py_asp
-- ,o.director -- ,p.last_price
-- ,o.billto_group --master data -- ,p.last_order
-- ,p.rate
-- ,p.flag
-- ,pp.fc_units
-- ,pp.fc_price
-- ,pp.iters
-- FROM
-- poolprice pp
-- LEFT OUTER JOIN pivot p ON
-- pp.product = p.product
-- AND pp.billto_group = p.account
-- AND pp.shipto_group = p.shipgrp
--)
--SELECT * FROM fcp limit 100
----------------create the new price-----------------
--,adj AS (
--SELECT
-- p.product
-- ,p.styc
-- ,p.glec
-- ,p.majg
-- ,p.assc
-- ,p.coltier
-- ,p.sizc
-- ,p.suffix
-- ,p.account
-- ,p.shipgrp
-- ,p.py_gasp
-- ,p.cy_units
-- ,p.py_asp
-- ,p.last_price
-- ,p.last_order
-- ,p.rate
-- ,p.flag
-- ,CASE p.flag
-- ----------------------if repeat business then get to prior year + target %--------------------------------------------------------------
-- WHEN 'repeat' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
-- WHEN 'lost' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
-- ----------------------if new business, move towards py_gasp + target % : lesser of py gloabl + target or last + target------------------
-- WHEN 'new' THEN least(last_price * COALESCE(1+rate,1) - last_price,greatest(py_gasp * COALESCE(1+rate,1) - last_price,0))
-- END price_increment
--FROM
-- pivot p
--)
--SELECT * FROM adj LIMIT 1000
----------------create a log entry--------------------
--,log AS (
-- INSERT INTO
-- rlarp.osm_log(doc)
-- SELECT
-- $${
-- "message":"application of last price and target increases to all forecast orders",
-- "tag":"last price",
-- "type":"build"
-- }$$::jsonb doc
-- RETURNING *
--)
--,poolprice AS (
--SELECT
-- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product
-- ,o.quota_rep_descr
-- ,o.billto_group
-- ,o.shipto_group -- ,o.shipto_group
-- ,o.chan --master data -- ,order_season
-- ,o.chansub -- ,sum(units) units
-- ,o.chan_retail -- ,sum(value_loc) valloc
-- ,o.part -- ,sum(value_usd) valusd
-- ,o.part_descr -- ,sum(o.value_loc)/sum(o.units) price
-- ,o.part_group -- ,jsonb_agg(DISTINCT iter) iters
-- ,o.branding
-- ,o.majg_descr
-- ,o.ming_descr
-- ,o.majs_descr
-- ,o.mins_descr
-- ,o.segm
-- ,o.substance
-- ,o.fs_line --master data
-- ,o.r_currency --history cust mix
-- ,o.r_rate --master data
-- ,o.c_currency --master data
-- ,o.c_rate --master data
-- ,0::numeric units
-- ,ROUND(o.units * (a.price_increment/o.r_rate),2) value_loc
-- ,ROUND(o.units * a.price_increment,2) value_usd
-- ,0::numeric cost_loc
-- ,0::numeric cost_usd
-- ,o.calc_status --0
-- ,o.flag --0
-- ,o.order_date --history date mix
-- ,o.order_month
-- ,o.order_season
-- ,o.request_date --history date mix
-- ,o.request_month
-- ,o.request_season
-- ,o.ship_date --history date mix
-- ,o.ship_month
-- ,o.ship_season
-- ,o.version
-- ---this iteration has to be listed in the master template file in order to be effectively included---
-- ,'upload price' iter
-- ,log.id
-- ,COALESCE(log.doc->>'tag','') "tag"
-- ,log.doc->>'message' "comment"
-- ,log.doc->>'type' module
-- -----------debug columns---------
-- --,value_usd/units price
-- --,a.py_gasp
-- --,a.rate
-- --,a.last_price
-- --,a.price_increment
--FROM --FROM
-- rlarp.osm_pool o -- rlarp.osm_pool o
-- ,rlarp.itemmv i -- ,rlarp.itemmv i
-- ,adj a
-- ,log
--WHERE --WHERE
-- i.item = o.part -- i.item = o.part
-- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) -- --AND o.units <> 0
-- AND a.account = o.billto_group
-- AND a.shipgrp = o.shipto_group
-- AND a.price_increment <> 0
-- AND o.units <> 0
-- ---only apply to 2022 orders---- -- ---only apply to 2022 orders----
-- AND o.order_date >= '2021-06-01' -- AND o.order_date >= '2021-06-01'
-- --only include baseline stuff--- -- --only include baseline stuff---
-- AND iter <> 'upload price' -- AND segm <> 'Retail'
--GROUP BY
-- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
-- ,o.quota_rep_descr
-- ,o.billto_group
-- ,o.shipto_group
-- ,order_season
-- --AND iter <> 'upload price'
--) --)
-------------aggregate the impact------------ --, pooladj AS (
----SELECT * FROM ins limit 10000 -- SELECT
-- p.product
-- ,p.quota_rep_descr
-- ,p.billto_group
-- ,p.shipto_group
-- ,p.price
-- ,a.py_gasp
-- ,a.rate
-- ,a.last_price
-- ,a.price_increment
-- FROM
-- poolprice p
-- ,adj a
-- WHERE
-- a.product = p.product
-- AND a.account = p.billto_group
-- AND a.shipgrp = p.shipto_group
--)
--SELECT * FROM pooladj WHERE product ~ 'STG06000' AND shipto_group = 'BWI' limit 100
----SELECT * FROM poolprice WHERE product ~ 'TCA06600' AND shipto_group = 'BWI' AND quota_rep_descr = 'BRYAN HILL' LIMIT 1000
---------------build the iteration rows----------------
----,ins AS (
----SELECT ----SELECT
---- order_season ---- o.fspr
---- ,sum(value_loc) val_loc ---- ,o.plnt ---master data
---- ,sum(value_usd) val_usd ---- ,o.promo --history date mix
---- ,o.terms
---- ,o.bill_cust_descr --history cust mix
---- ,o.ship_cust_descr --history cust mix
---- ,o.dsm
---- ,o.quota_rep_descr --master data
---- ,o.director
---- ,o.billto_group --master data
---- ,o.shipto_group
---- ,o.chan --master data
---- ,o.chansub
---- ,o.chan_retail
---- ,o.part
---- ,o.part_descr
---- ,o.part_group
---- ,o.branding
---- ,o.majg_descr
---- ,o.ming_descr
---- ,o.majs_descr
---- ,o.mins_descr
---- ,o.segm
---- ,o.substance
---- ,o.fs_line --master data
---- ,o.r_currency --history cust mix
---- ,o.r_rate --master data
---- ,o.c_currency --master data
---- ,o.c_rate --master data
---- ,0::numeric units
---- ,ROUND(o.units * (a.price_increment/o.r_rate),2) value_loc
---- ,ROUND(o.units * a.price_increment,2) value_usd
---- ,0::numeric cost_loc
---- ,0::numeric cost_usd
---- ,o.calc_status --0
---- ,o.flag --0
---- ,o.order_date --history date mix
---- ,o.order_month
---- ,o.order_season
---- ,o.request_date --history date mix
---- ,o.request_month
---- ,o.request_season
---- ,o.ship_date --history date mix
---- ,o.ship_month
---- ,o.ship_season
---- ,o.version
---- ---this iteration has to be listed in the master template file in order to be effectively included---
---- ,'upload price' iter
---- ,log.id
---- ,COALESCE(log.doc->>'tag','') "tag"
---- ,log.doc->>'message' "comment"
---- ,log.doc->>'type' module
---- -----------debug columns---------
---- --,value_usd/units price
---- --,a.py_gasp
---- --,a.rate
---- --,a.last_price
---- --,a.price_increment
----FROM ----FROM
---- ins ---- rlarp.osm_pool o
----GROUP BY ---- ,rlarp.itemmv i
---- order_season; ---- ,adj a
--,del AS ( ---- ,log
-- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING * ----WHERE
--) ---- i.item = o.part
--INSERT INTO ---- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
-- rlarp.osm_pool ---- AND a.account = o.billto_group
--SELECT * FROM ins; ---- AND a.shipgrp = o.shipto_group
-- ---- AND a.price_increment <> 0
--COMMIT; ---- AND o.units <> 0
---- ---only apply to 2022 orders----
---- AND o.order_date >= '2021-06-01'
---- --only include baseline stuff---
---- AND iter <> 'upload price'
----)
---------------aggregate the impact------------
------SELECT * FROM ins limit 10000
------SELECT
------ order_season
------ ,sum(value_loc) val_loc
------ ,sum(value_usd) val_usd
------FROM
------ ins
------GROUP BY
------ order_season;
----,del AS (
---- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING *
----)
----INSERT INTO
---- rlarp.osm_pool
----SELECT * FROM ins;
----
----COMMIT;