From b7eb94202cae8093d73ac3476544a7a2148c13f4 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 7 May 2021 10:12:33 -0400 Subject: [PATCH] work on getting to right price --- offline/last_price.sql | 315 +++++++++++++++++++---------------------- 1 file changed, 147 insertions(+), 168 deletions(-) diff --git a/offline/last_price.sql b/offline/last_price.sql index d9b2d2d..b14b3c4 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -77,6 +77,49 @@ incr AS ( ('C','P','COLOR PRINTED','Color','P') ) x(colgrp, brand, dataseg, tcol, tbrand) ) +---customer dba assigned price levels--------- +,dbap AS ( + SELECT + dba + ,jsonb_agg(DISTINCT plevel) plev + ,jsonb_agg(DISTINCT plcd) plist + FROM + rlarp.cust c + INNER JOIN rlarp.sachdef sd ON + sd.plev = c.plevel + AND '2020-05-31' between sd.fdate AND sd.tdate + WHERE + pricing <> '' + AND dba <> '' + GROUP BY + dba +) +----customer dba price list pricing------ +,plist AS ( + SELECT + dbap.dba + ,dbap.plev + ,dbap.plist + ,jcplcd + ,jcpart + ,jcunit + --,jcvoll + -----just use the lowest volume price for the part----- + ,min(jcpric) jcpric + FROM + dbap + INNER JOIN lgdat.iprcc cc ON + dbap.plist ? cc.jcplcd + WHERE + jcunit = 'M' + GROUP BY + dbap.dba + ,dbap.plev + ,dbap.plist + ,jcplcd + ,jcpart + ,jcunit +) ------------carve out pricing baseline data-------------------- ,p AS ( SELECT @@ -138,7 +181,7 @@ incr AS ( ,o.shipgrp ,o.odate DESC ) ---SELECT * FROM p WHERE account ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' and chgrp = 'W' order by rn ASC +--SELECT * FROM p WHERE account ~ 'AMA P' and product = 'AMK06000.CBXX' order by rn ASC ------------build global py asp------------------ ,baseline AS ( SELECT @@ -147,6 +190,7 @@ incr AS ( ,assc ,chgrp ,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_gasp + ,round(sum(val_usd) FILTER (WHERE oseas = 2021)/sum(units) FILTER (WHERE oseas = 2021),5) cy_gasp FROM p GROUP BY @@ -155,6 +199,7 @@ incr AS ( ,assc ,chgrp ) +--SELECT * FROM baseline WHERE product = 'AMK06000.CBXX' --SELECT -- p.product -- --,p.styc @@ -281,6 +326,7 @@ SELECT ,p.account ,p.shipgrp ,bl.py_gasp + ,jsonb_agg(DISTINCT part) item ,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 @@ -299,6 +345,7 @@ SELECT END END rate ,ms.avg_price target + ,JSONB_AGG(DISTINCT plist.jcpric/1000) jcpric --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- THEN 'unknown' @@ -334,6 +381,9 @@ FROM AND ms.data_segment = ds.dataseg AND ms.season = '2021' AND ms.region = 'ALL' + LEFT OUTER JOIN plist ON + plist.dba = p.account + AND plist.jcpart = p.part GROUP BY p.productt ,p.product @@ -371,9 +421,11 @@ GROUP BY ,pp.order_season ,pp.billto_group ,pp.shipto_group + ,pp.chgrp ,pp.fc_units ,pp.fc_price ,pp.iters + --,p.item ,p.py_gasp ,p.cy_units ,p.py_asp @@ -381,9 +433,18 @@ GROUP BY ,p.last_order ,p.target ,p.rate + ,p.jcpric --need to link in targets pricing and price list for cap purposes--------- --also need to link regional price lists so we don't blow past those------ - ,COALESCE(py_asp,py_gasp) * (1 + rate) fc_price + ,least( + min( + COALESCE(p.py_asp,p.last_price), + COALESCE(py_gasp,target) + ) * (1 + p.rate), + jcprice->>0 + ) fc_price + --last season price + % capped at list or py_gasp + % + --last price + % capped at list or pg_gasp + % FROM poolprice pp LEFT OUTER JOIN pivot p ON @@ -393,38 +454,7 @@ GROUP BY 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 +SELECT * FROM fcp limit 1000 ----------------create a log entry-------------------- --,log AS ( -- INSERT INTO @@ -437,150 +467,99 @@ SELECT * FROM fcp limit 100 -- }$$::jsonb doc -- RETURNING * --) ---,poolprice AS ( +---------------build the iteration rows---------------- +--,ins AS ( --SELECT --- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product --- ,o.quota_rep_descr --- ,o.billto_group +-- o.fspr +-- ,o.plnt ---master data +-- ,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 --- ,order_season --- ,sum(units) units --- ,sum(value_loc) valloc --- ,sum(value_usd) valusd --- ,sum(o.value_loc)/sum(o.units) price --- ,jsonb_agg(DISTINCT iter) iters +-- ,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 -- rlarp.osm_pool o -- ,rlarp.itemmv i +-- ,adj a +-- ,log --WHERE -- i.item = o.part --- --AND o.units <> 0 +-- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) +-- 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---- -- AND o.order_date >= '2021-06-01' -- --only include baseline stuff--- --- 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' +-- AND iter <> 'upload price' --) ---, pooladj AS ( --- 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 ( +-------------aggregate the impact------------ +----SELECT * FROM ins limit 10000 ----SELECT ----- o.fspr ----- ,o.plnt ---master data ----- ,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 +---- order_season +---- ,sum(value_loc) val_loc +---- ,sum(value_usd) val_usd ----FROM ----- rlarp.osm_pool o ----- ,rlarp.itemmv i ----- ,adj a ----- ,log -----WHERE ----- i.item = o.part ----- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) ----- 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---- ----- 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; +---- 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;