From 683921dda0de247370cc0655b3d692643c09d790 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 20 Apr 2021 16:11:34 -0400 Subject: [PATCH 01/23] work on last price --- offline/last_price.sql | 571 +++++++++++++++++++++++++---------------- 1 file changed, 354 insertions(+), 217 deletions(-) diff --git a/offline/last_price.sql b/offline/last_price.sql index 74f8352..3b232f0 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -4,36 +4,36 @@ WITH ------------------goal price increases--------------------- incr AS ( SELECT * FROM (VALUES - ('110','PP','B',0.25), - ('110','PP','T',0.25), + ('110','PP','B',0.3), + ('110','PP','T',0.3), ('110','PP','L',0.4), ('110','PP','M',0.4), ('110','PP','P',0.4), ('110','PP','C',0.4), - ('210','PE','B',0.25), - ('210','PE','T',0.25), - ('210','PE','L',0.25), - ('210','PE','M',0.25), - ('210','PE','P',0.25), - ('210','PE','C',0.25), - ('310','PE','B',0.15), - ('310','PE','T',0.25), - ('310','PE','L',0.25), - ('310','PE','M',0.25), - ('310','PE','P',0.25), - ('310','PE','C',0.25), - ('310','PP','B',0.13), - ('310','PP','T',0.16), - ('310','PP','L',0.16), - ('310','PP','M',0.16), - ('310','PP','P',0.16), - ('310','PP','C',0.16), - ('310','PS','B',0.13), - ('310','PS','T',0.16), - ('310','PS','L',0.16), - ('310','PS','M',0.16), - ('310','PS','P',0.16), - ('310','PS','C',0.16), + ('210','PE','B',0.3), + ('210','PE','T',0.3), + ('210','PE','L',0.3), + ('210','PE','M',0.3), + ('210','PE','P',0.3), + ('210','PE','C',0.3), + ('310','PE','B',0.25), + ('310','PE','T',0.3), + ('310','PE','L',0.3), + ('310','PE','M',0.3), + ('310','PE','P',0.3), + ('310','PE','C',0.3), + ('310','PP','B',0.25), + ('310','PP','T',0.25), + ('310','PP','L',0.25), + ('310','PP','M',0.25), + ('310','PP','P',0.25), + ('310','PP','C',0.25), + ('310','PS','B',0.25), + ('310','PS','T',0.25), + ('310','PS','L',0.25), + ('310','PS','M',0.25), + ('310','PS','P',0.25), + ('310','PS','C',0.25), ('320','PE','B',0.25), ('320','PE','T',0.25), ('320','PE','L',0.25), @@ -46,21 +46,13 @@ incr AS ( ('320','PP','M',0.25), ('320','PP','P',0.25), ('320','PP','C',0.25), - ('910','PE','B',0.15), + ('910','PE','B',0.25), ('910','PE','D',0.25), ('910','PE','F',0.25), - ('910','PP','B',0.15), + ('910','PP','B',0.25), ('910','PP','D',0.25), ('910','PP','F',0.25), - ('910','PS','B',0.15), - ('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','B',0.25), ('910','PS','D',0.25), ('910','PS','F',0.25), ('610','','B',0.02), @@ -127,7 +119,8 @@ incr AS ( ,o.shipgrp ,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------------------ ,baseline AS ( SELECT @@ -144,47 +137,47 @@ incr AS ( ,assc ,chgrp ) -----------pivot the pricing out into columns per customer/product-------- -,pivot AS ( SELECT p.product - ,p.styc - ,p.glec + --,p.styc + --,p.glec ,p.majg ,p.assc ,p.colgrp - ,p.coltier - ,p.sizc - ,p.suffix + --,p.coltier + --,p.sizc + --,p.suffix ,p.chgrp ,p.account ,p.shipgrp - ,bl.py_gasp + --,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 + ,i.rate incr_rate ,CASE p.chgrp - WHEN 'D' THEN i.rate + WHEN 'D' THEN .5 ELSE CASE p.majg WHEN '610' THEN .02 ElSE CASE p.colgrp - WHEN 'B' THEN .1 - WHEN 'C' THEN .15 + 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 + --,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 p LEFT OUTER JOIN baseline bl ON @@ -197,77 +190,40 @@ FROM AND i.assc = p.assc AND i.coltier = p.coltier AND p.glec <> '1RE' - +WHERE + p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W' GROUP BY p.product - ,p.styc - ,p.glec + --,p.styc + --,p.glec ,p.majg ,p.assc - ,p.coltier + --,p.coltier ,p.colgrp - ,p.sizc - ,p.suffix + --,p.sizc + --,p.suffix ,p.chgrp ,p.account ,p.shipgrp - ,bl.py_gasp + --,bl.py_gasp ,i.rate -) -----------------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 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 * -) + + + +----------calculate pricing as it sits in the forecast-------------- ,poolprice AS ( SELECT i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product ,o.quota_rep_descr + ,substring(majg,1,3) majg + ,substring(chan,1,1) chgrp ,o.billto_group ,o.shipto_group ,order_season - ,sum(units) units - ,sum(value_loc) valloc - ,sum(value_usd) valusd - ,sum(o.value_loc)/sum(o.units) price + ,round(sum(units) ,2) fc_units + --,round(sum(value_loc) ,2) valloc + --,round(sum(value_usd) ,2) valusd + ,round(sum(o.value_loc)/sum(o.units),5) fc_price ,jsonb_agg(DISTINCT iter) iters FROM rlarp.osm_pool o @@ -282,125 +238,306 @@ WHERE GROUP BY i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) ,o.quota_rep_descr + ,substring(majg,1,3) + ,substring(chan,1,1) ,o.billto_group ,o.shipto_group ,order_season --AND iter <> 'upload price' +HAVING + sum(o.units) <> 0 ) -, 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 +----------pivot the pricing out into columns per customer/product-------- +,pivot AS ( +SELECT + p.product + --,p.styc + --,p.glec + ,p.majg + ,p.assc + ,p.colgrp + --,p.coltier + --,p.sizc + --,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 + p + LEFT OUTER JOIN baseline bl ON + bl.product = p.product + AND bl.majg = p.majg + AND bl.assc = p.assc + 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 ---SELECT * FROM poolprice WHERE product ~ 'TCA06600' AND shipto_group = 'BWI' AND quota_rep_descr = 'BRYAN HILL' LIMIT 1000 --------------build the iteration rows---------------- ---,ins AS ( +,test_unique AS ( +SELECT + p.* + ,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 +-- pp.product +-- ,pp.quota_rep_descr +-- ,pp.order_season +-- ,pp.billto_group +-- ,pp.shipto_group +-- ,p.py_gasp +-- ,p.cy_units +-- ,p.py_asp +-- ,p.last_price +-- ,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 --- 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 +-- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product +-- ,o.quota_rep_descr +-- ,o.billto_group -- ,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(units) units +-- ,sum(value_loc) valloc +-- ,sum(value_usd) valusd +-- ,sum(o.value_loc)/sum(o.units) price +-- ,jsonb_agg(DISTINCT iter) iters --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 +-- --AND o.units <> 0 -- ---only apply to 2022 orders---- -- AND o.order_date >= '2021-06-01' -- --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------------ -----SELECT * FROM ins limit 10000 +--, 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 ( ----SELECT ----- order_season ----- ,sum(value_loc) val_loc ----- ,sum(value_usd) val_usd +---- 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 ----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; +---- 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; From 704957a2fa71ee77d024d7eab85226c353222e6d Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 21 Apr 2021 17:24:05 -0400 Subject: [PATCH 02/23] update product notion to include color tier --- offline/last_price.sql | 232 +++++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 112 deletions(-) diff --git a/offline/last_price.sql b/offline/last_price.sql index 3b232f0..3665d31 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -64,6 +64,7 @@ incr AS ( ,p AS ( SELECT o.part + ,o.styc||'.'||o.coltier||substring(o.sizc,1,3) productt ,o.styc||'.'||o.colgrp||substring(o.sizc,1,3) product ,o.glec ,o.styc @@ -119,8 +120,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 ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' and chgrp = 'W' order by rn ASC ------------build global py asp------------------ ,baseline AS ( SELECT @@ -137,83 +137,81 @@ SELECT * FROM p WHERE account ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' an ,assc ,chgrp ) -SELECT - p.product - --,p.styc - --,p.glec - ,p.majg - ,p.assc - ,p.colgrp - --,p.coltier - --,p.sizc - --,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 - ,i.rate incr_rate - ,CASE p.chgrp - WHEN 'D' THEN .5 - 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 - p - LEFT OUTER JOIN baseline bl ON - bl.product = p.product - AND bl.majg = p.majg - AND bl.assc = p.assc - 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' -WHERE - p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W' -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 +-- p.product +-- --,p.styc +-- --,p.glec +-- ,p.majg +-- ,p.assc +-- ,p.colgrp +-- --,p.coltier +-- --,p.sizc +-- --,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 +-- ,i.rate incr_rate +-- ,CASE p.chgrp +-- WHEN 'D' THEN .5 +-- 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 +-- p +-- LEFT OUTER JOIN baseline bl ON +-- bl.product = p.product +-- AND bl.majg = p.majg +-- AND bl.assc = p.assc +-- 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' +--WHERE +-- p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W' +--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 ----------calculate pricing as it sits in the forecast-------------- ,poolprice AS ( SELECT - i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product + i.stlc||'.'||i.coltier||substring(i.sizc,1,3) productt + ,i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product ,o.quota_rep_descr ,substring(majg,1,3) majg ,substring(chan,1,1) chgrp @@ -236,7 +234,8 @@ WHERE --only include baseline stuff--- AND segm <> 'Retail' GROUP BY - i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) + i.stlc||'.'||i.coltier||substring(i.sizc,1,3) + ,i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) ,o.quota_rep_descr ,substring(majg,1,3) ,substring(chan,1,1) @@ -250,7 +249,8 @@ HAVING ----------pivot the pricing out into columns per customer/product-------- ,pivot AS ( SELECT - p.product + p.productt + ,p.product --,p.styc --,p.glec ,p.majg @@ -266,6 +266,7 @@ SELECT ,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(sum(val_usd) FILTER (WHERE oseas = 2021)/sum(units) FILTER (WHERE oseas = 2021),5) cy_asp ,round(avg(price) FILTER (WHERE rn = 1),5) last_price ,max(odate) FILTER (WHERE rn = 1) last_order ,CASE p.chgrp @@ -302,7 +303,8 @@ FROM AND i.coltier = p.coltier AND p.glec <> '1RE' GROUP BY - p.product + p.productt + ,p.product --,p.styc --,p.glec ,p.majg @@ -317,40 +319,46 @@ GROUP BY ,bl.py_gasp ,i.rate ) -,test_unique AS ( -SELECT - p.* - ,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 --- pp.product --- ,pp.quota_rep_descr --- ,pp.order_season --- ,pp.billto_group --- ,pp.shipto_group --- ,p.py_gasp --- ,p.cy_units --- ,p.py_asp --- ,p.last_price --- ,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 +--,test_unique AS ( +--SELECT +-- p.* +-- ,count(*) OVER (partition by productt, majg, chgrp, account, shipgrp) cnt +--FROM +-- pivot p --) ---SELECT * FROM fcp limit 100 +--SELECT * FROM test_unique where cnt > 1 +--SELECT * FROM pivot LIMIT 1000 +--------------join forecast price------------------- +,fcp AS ( + SELECT + pp.productt + ,pp.product + ,pp.majg + ,pp.quota_rep_descr + ,pp.order_season + ,pp.billto_group + ,pp.shipto_group + ,pp.fc_units + ,pp.fc_price + ,pp.iters + ,p.py_gasp + ,p.cy_units + ,p.py_asp + ,p.last_price + ,p.last_order + ,p.rate + --need to link in targets pricing and price list for cap purposes--------- + ,COALESCE(py_asp,py_gasp) * (1 + rate) fc_price + FROM + poolprice pp + LEFT OUTER JOIN pivot p ON + pp.productt = p.productt + AND pp.majg = p.majg + AND pp.chgrp = p.chgrp + AND pp.billto_group = p.account + AND pp.shipto_group = p.shipgrp +) +SELECT * FROM fcp limit 100 ----------------create the new price----------------- --,adj AS ( --SELECT From da8e1353f31149cae43885d09675d07d3dd55aa3 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 22 Apr 2021 17:46:56 -0400 Subject: [PATCH 03/23] notes on regional pricing and link in target pricing --- offline/last_price.sql | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/offline/last_price.sql b/offline/last_price.sql index 3665d31..d9b2d2d 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -60,6 +60,23 @@ incr AS ( ('610','','W',0.02) ) x(MAJG,ASSC,COLTIER,RATE) ) +,chx AS ( + SELECT * FROM ( VALUES + ('DIRECT','DIR','Direct'), + ('DISTRIB DROP SHIP','DRP','Drop'), + ('DISTRIBUTOR','WHS','Warehouse') + ) x(xchan, chan, tchan) +) +,ds AS ( + SELECT * FROM ( VALUES + ('B','X','BASE','Base',''), + ('B','L','BASE LABELED','Base','L'), + ('B','P','BASE PRINTED','Base','P'), + ('C','X','COLOR','Color',''), + ('C','L','COLOR LABELED','Color','L'), + ('C','P','COLOR PRINTED','Color','P') + ) x(colgrp, brand, dataseg, tcol, tbrand) +) ------------carve out pricing baseline data-------------------- ,p AS ( SELECT @@ -75,6 +92,7 @@ incr AS ( ,o.sizc ,i.suffix ,substring(o.chan,1,1) chgrp + ,o.chan ,o.account ,o.shipgrp ,o.fb_qty units @@ -280,6 +298,7 @@ SELECT END END END rate + ,ms.avg_price target --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- THEN 'unknown' @@ -302,6 +321,19 @@ FROM AND i.assc = p.assc AND i.coltier = p.coltier AND p.glec <> '1RE' + -----convert to target price channels--------- + LEFT OUTER JOIN chx ON + chx.chan = p.chan + -----convert to target price product level---- + LEFT OUTER JOIN ds ON + ds.colgrp = p.colgrp + AND ds.brand = substring(p.sizc,3,1) + LEFT OUTER JOIN pricequote.market_setavgprice ms ON + ms.mold = substring(p.product,1,8) + AND ms.chan = chx.xchan + AND ms.data_segment = ds.dataseg + AND ms.season = '2021' + AND ms.region = 'ALL' GROUP BY p.productt ,p.product @@ -317,6 +349,7 @@ GROUP BY ,p.account ,p.shipgrp ,bl.py_gasp + ,ms.avg_price ,i.rate ) --,test_unique AS ( @@ -346,8 +379,10 @@ GROUP BY ,p.py_asp ,p.last_price ,p.last_order + ,p.target ,p.rate --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 FROM poolprice pp From c92f945a71661996c7865531f551c72ff9dcdf6e Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 22 Apr 2021 17:47:29 -0400 Subject: [PATCH 04/23] join to materialized view --- build/snap_itemm_pool.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/snap_itemm_pool.sql b/build/snap_itemm_pool.sql index 92723cb..a4db87f 100644 --- a/build/snap_itemm_pool.sql +++ b/build/snap_itemm_pool.sql @@ -39,7 +39,7 @@ SELECT DISTINCT ,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance FROM rlarp.osm_pool o - LEFT OUTER JOIN rlarp.itemm i ON + LEFT OUTER JOIN rlarp.itemmv i ON i.item = o.part LEFT OUTER JOIN seg ON seg.glec = i.glec From 67e5466f725c6bd720fa69ce28a662167e6dd42e Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 23 Apr 2021 09:08:47 -0400 Subject: [PATCH 05/23] need to set director while snapping quota rep --- build/snap_cust_pool.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/build/snap_cust_pool.sql b/build/snap_cust_pool.sql index 937692b..106fc6a 100644 --- a/build/snap_cust_pool.sql +++ b/build/snap_cust_pool.sql @@ -50,6 +50,7 @@ UPDATE rlarp.osm_pool o SET quota_rep_descr = (regexp_match(r.repp,'.* - (.*)$'))[1] + ,director = r.director FROM rlarp.repc r WHERE From 78a9658e8028a75c7855448a0fa05065451e2fab Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 23 Apr 2021 09:41:38 -0400 Subject: [PATCH 06/23] need to include director comparison to hook for changes --- build/snap_cust_pool.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build/snap_cust_pool.sql b/build/snap_cust_pool.sql index 106fc6a..fcfa67a 100644 --- a/build/snap_cust_pool.sql +++ b/build/snap_cust_pool.sql @@ -55,6 +55,8 @@ FROM rlarp.repc r WHERE r.rcode = o.dsm - AND quota_rep_descr <> (regexp_match(r.repp,'.* - (.*)$'))[1]; - + AND ( + quota_rep_descr <> (regexp_match(r.repp,'.* - (.*)$'))[1] + OR o.director <> r.director + ); COMMIT; From 97f73e51079255480a774cdb36423d746f701a54 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 28 Apr 2021 11:14:43 -0400 Subject: [PATCH 07/23] notes on old update prior year baseline approach --- build/merge_actuals_exec.sql | 4 ++-- build/merge_actuals_pool.sql | 4 ++-- build/merge_actuals_prep.sql | 6 +++--- build/readme.md | 7 +++++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/build/merge_actuals_exec.sql b/build/merge_actuals_exec.sql index f263fae..891d662 100644 --- a/build/merge_actuals_exec.sql +++ b/build/merge_actuals_exec.sql @@ -1,3 +1,3 @@ -DELETE FROM rlarp.osmf_dev WHERE odate < '2020-06-01'; -INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev WHERE odate < '2020-06-01'; +DELETE FROM rlarp.osmf_dev WHERE odate < '2021-05-01'; +INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev WHERE odate < '2021-05-01'; diff --git a/build/merge_actuals_pool.sql b/build/merge_actuals_pool.sql index d57a96b..a37b194 100644 --- a/build/merge_actuals_pool.sql +++ b/build/merge_actuals_pool.sql @@ -1,6 +1,6 @@ BEGIN; -DELETE FROM rlarp.osm_pool WHERE order_date < '2020-06-01'; +DELETE FROM rlarp.osm_pool WHERE order_date < '2021-05-01'; INSERT INTO rlarp.osm_pool WITH @@ -135,7 +135,7 @@ FROM LEFT OUTER JOIN rlarp.itemm i ON i.item = o.part WHERE - odate < '2020-06-01'; + odate < '2021-05-01'; SELECT diff --git a/build/merge_actuals_prep.sql b/build/merge_actuals_prep.sql index dbc4343..a15580f 100644 --- a/build/merge_actuals_prep.sql +++ b/build/merge_actuals_prep.sql @@ -145,11 +145,11 @@ gld AS ( WHERE ( --base period orders booked.... - o.odate <@ daterange('2019-06-01','2020-06-01') + o.odate <@ daterange('2020-06-01','2021-05-01','[)') --...or any open orders currently booked before cutoff.... - OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2020-06-01') + OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2021-05-01') --...or anything that shipped in that period - OR o.fspr BETWEEN '2001' AND '2012' + OR o.fspr BETWEEN '2101' AND '2111' ) AND fs_line = '41010' AND calc_status <> 'CANCELED' diff --git a/build/readme.md b/build/readme.md index 9a4193d..3e3a2f9 100644 --- a/build/readme.md +++ b/build/readme.md @@ -11,3 +11,10 @@ Build A New Forecast * build the pool for the more limited UI data set: build_pool.sql * copy the pool to osmfs_dev from inclusion in the osm_stack and reporting: convert_pool_all.sql * refresh osm_stack_refresh() to integrate into reporting + +Integrate Actuals +------------------------------------------------------------------------------------------------------------------ + +* `merge_actuals_prep` -> push a fresh baseline into a temp table +* `merge_actuals_exec` -> take the baseline and push it into `osmf` +* `merge_actuals_exec` -> manualls splice in `osmf` into `pool` From ac3c50dbbae71c200fe02382647b8ba5ee13a0b6 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 28 Apr 2021 17:34:44 -0400 Subject: [PATCH 08/23] work on merging actuals --- build/build_merge.sql | 301 ++++++++++++++++++++++++++++++++++++++++ build/build_stage.sql | 313 ++++++++++++++++++++++++++++++++++++++++++ build/tots.sql | 2 +- 3 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 build/build_merge.sql create mode 100644 build/build_stage.sql diff --git a/build/build_merge.sql b/build/build_merge.sql new file mode 100644 index 0000000..844620f --- /dev/null +++ b/build/build_merge.sql @@ -0,0 +1,301 @@ +WITH +tdr AS ( + SELECT + DATERANGE('2020-05-01','2021-05-01','[)') drange + ,DATERANGE('2020-05-01' + ('1 year'::interval),'2021-05-01' + ('1 year'::interval),'[)') repl + ,'1 year'::interval AS incr + ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals plug'),('adj price'),('adj volume')) AS x(v)) iter +) +,remove AS ( + SELECT + -----------documents------------- + null::int "ddord#" + ,null::int "dditm#" + ,null::int "fgbol#" + ,null::int "fgent#" + ,null::int "diinv#" + ,null::int "dilin#" + ,null::int quoten + ,null::int quotel + ----------dates/status------------------ + ,o.odate dcodat + ,o.rdate ddqdat + ,null::date dcmdat + ,null::date fesdat + ,o.dhidat + ,null::text fesind + ,null::text dhpost + ,o.fspr + -----------measures-------------------- + ,null::numeric ddqtoi + ,null::numeric ddqtsi + ,null::numeric fgqshp + ,null::numeric diqtsh + ,null::numeric diext + ,null::numeric ditdis + ,null::jsonb discj + ,null::text dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,null::text custpo + ,null::text remit_to + ,null::text bill_class + ,o.bill_cust + ,null::text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,o.ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,o.dsm + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text chansub + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text bill_ctry + ,null::text bill_prov + ,null::text bill_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,o.part + ,null::text styc + ,null::text colc + ,null::text colgrp + ,null::text coltier + ,null::text colstat + ,null::text sizc + ,null::text pckg + ,null::text kit + ,null::text brnd + ,null::text majg + ,null::text ming + ,null::text majs + ,null::text mins + ,null::text gldco + ,null::text gldc + ,null::text glec + ,null::text harm + ,null::text clss + ,null::text brand + ,null::text assc + ,null::text ddunit + ,null::text unti + ,null::numeric lbs + ,null::numeric plt + ,null::text plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,-sum(o.fb_qty) fb_qty + ,-sum(o.fb_val_loc) fb_val_loc + ,-sum(o.fb_val_loc_dis) fb_val_loc_dis + ,-sum(o.fb_val_loc_qt) fb_val_loc_qt + ,-sum(o.fb_val_loc_pl) fb_val_loc_pl + ,-sum(o.fb_val_loc_tar) fb_val_loc_tar + ,-sum(o.fb_cst_loc) fb_cst_loc + ,-sum(o.fb_cst_loc_cur) fb_cst_loc_cur + ,-sum(o.fb_cst_loc_fut) fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + -----when null, greatest/least is just going to act like coalesce + ,o.sdate + ,o.sseas + ,o.version + ,o.iter + FROM + rlarp.osmf_dev o + CROSS JOIN tdr + WHERE + --collect all the rows in the base period and + --the destination slot for the new rows + o.odate <@ tdr.drange + OR o.odate <@ tdr.repl + GROUP BY + o.odate + ,o.rdate + ,o.dhidat + ,o.fspr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.bill_cust + ,o.ship_cust + ,o.dsm + ,o.part + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas +) +,stack AS ( +SELECT * FROM rlarp.osmf_stage +UNION ALL +SELECT * FROM remove +) +,merge_diff AS ( + SELECT + -----------documents------------- + null::int "ddord#" + ,null::int "dditm#" + ,null::int "fgbol#" + ,null::int "fgent#" + ,null::int "diinv#" + ,null::int "dilin#" + ,null::int quoten + ,null::int quotel + ----------dates/status------------------ + ,o.odate dcodat + ,o.rdate ddqdat + ,null::date dcmdat + ,null::date fesdat + ,o.dhidat + ,null::text fesind + ,null::text dhpost + ,o.fspr + -----------measures-------------------- + ,null::numeric ddqtoi + ,null::numeric ddqtsi + ,null::numeric fgqshp + ,null::numeric diqtsh + ,null::numeric diext + ,null::numeric ditdis + ,null::jsonb discj + ,null::text dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,null::text custpo + ,null::text remit_to + ,null::text bill_class + ,o.bill_cust + ,null::text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,o.ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,o.dsm + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text chansub + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text bill_ctry + ,null::text bill_prov + ,null::text bill_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,o.part + ,null::text styc + ,null::text colc + ,null::text colgrp + ,null::text coltier + ,null::text colstat + ,null::text sizc + ,null::text pckg + ,null::text kit + ,null::text brnd + ,null::text majg + ,null::text ming + ,null::text majs + ,null::text mins + ,null::text gldco + ,null::text gldc + ,null::text glec + ,null::text harm + ,null::text clss + ,null::text brand + ,null::text assc + ,null::text ddunit + ,null::text unti + ,null::numeric lbs + ,null::numeric plt + ,null::text plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,sum(o.fb_qty) fb_qty + ,sum(o.fb_val_loc) fb_val_loc + ,sum(o.fb_val_loc_dis) fb_val_loc_dis + ,sum(o.fb_val_loc_qt) fb_val_loc_qt + ,sum(o.fb_val_loc_pl) fb_val_loc_pl + ,sum(o.fb_val_loc_tar) fb_val_loc_tar + ,sum(o.fb_cst_loc) fb_cst_loc + ,sum(o.fb_cst_loc_cur) fb_cst_loc_cur + ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + -----when null, greatest/least is just going to act like coalesce + ,o.sdate + ,o.sseas + ,'actuals' + ,'merge' + FROM + stack + WHERE + --collect all the rows in the base period and + --the destination slot for the new rows + o.odate <@ tdr.drange + OR o.odate <@ tdr.repl + GROUP BY + o.odate + ,o.rdate + ,o.dhidat + ,o.fspr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.bill_cust + ,o.ship_cust + ,o.dsm + ,o.part + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas +) +SELECT COUNT(*) from merge_deff diff --git a/build/build_stage.sql b/build/build_stage.sql new file mode 100644 index 0000000..47c4db8 --- /dev/null +++ b/build/build_stage.sql @@ -0,0 +1,313 @@ +BEGIN; +--\timing +CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; +truncate table rlarp.osmf_stage; + +WITH +tdr AS ( + SELECT + DATERANGE('2020-05-01','2021-05-01','[)') drange + ,(SELECT jsonb_agg(iter) FROM (VALUES('copy'),('actuals'),('actuals plug'),('adj price'),('adj volume'))) iter +) +,gld AS ( + SELECT + N1COMP COMP + ,N1CCYY FSYR + ,KPMAXP PERDS + ,N1FSPP PERD + ,to_char(N1FSYP,'FM0000') FSPR + ,N1SD01 SDAT + ,N1ED01 EDAT + ,to_char(N1ED01,'yymm') CAPR + ,N1ED01 - N1SD01 +1 NDAYS + ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR + ,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR + FROM + LGDAT.GLDATREF + INNER JOIN LGDAT.GLDATE ON + KPCOMP = N1COMP AND + KPCCYY = N1CCYY + CROSS JOIN tdr + WHERE + N1COMP = 93 + --AND DIGITS(N1FSYP) = '1901' +) +--SELECT * FROM gld +,baseline AS ( + SELECT + -----------documents------------- + null::int "ddord#" + ,null::int "dditm#" + ,null::int "fgbol#" + ,null::int "fgent#" + ,null::int "diinv#" + ,null::int "dilin#" + ,null::int quoten + ,null::int quotel + ----------dates/status------------------ + ,o.odate dcodat + ,o.rdate ddqdat + ,null::date dcmdat + ,null::date fesdat + ,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat + ,null::text fesind + ,null::text dhpost + ,o.fspr + -----------measures-------------------- + ,null::numeric ddqtoi + ,null::numeric ddqtsi + ,null::numeric fgqshp + ,null::numeric diqtsh + ,null::numeric diext + ,null::numeric ditdis + ,null::jsonb discj + ,null::text dhincr + ,o.plnt + ,COALESCE(o.promo,'') promo + ,null::text return_reas + ,o.terms + ,null::text custpo + ,null::text remit_to + ,null::text bill_class + ,o.bill_cust + ,null::text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,o.ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,o.dsm + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text chansub + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text bill_ctry + ,null::text bill_prov + ,null::text bill_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,o.part + ,null::text styc + ,null::text colc + ,null::text colgrp + ,null::text coltier + ,null::text colstat + ,null::text sizc + ,null::text pckg + ,null::text kit + ,null::text brnd + ,null::text majg + ,null::text ming + ,null::text majs + ,null::text mins + ,null::text gldco + ,null::text gldc + ,null::text glec + ,null::text harm + ,null::text clss + ,null::text brand + ,null::text assc + ,null::text ddunit + ,null::text unti + ,null::numeric lbs + ,null::numeric plt + ,null::text plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,sum(o.fb_qty) fb_qty + ,sum(o.fb_val_loc) fb_val_loc + ,sum(o.fb_val_loc_dis) fb_val_loc_dis + ,sum(o.fb_val_loc_qt) fb_val_loc_qt + ,sum(o.fb_val_loc_pl) fb_val_loc_pl + ,sum(o.fb_val_loc_tar) fb_val_loc_tar + ,sum(o.fb_cst_loc) fb_cst_loc + ,sum(o.fb_cst_loc_cur) fb_cst_loc_cur + ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + -----when null, greatest/least is just going to act like coalesce + ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate + ,ss.ssyr sseas + ,'15mo' "version" + ,'actuals' iter + FROM + rlarp.osm_dev o + --snap the ship dates of the historic fiscal period + LEFT OUTER JOIN gld ON + gld.fspr = o.fspr + --get the shipping season for open orders based on the snapped date + LEFT OUTER JOIN gld ss ON + greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat + WHERE + ( + --base period orders booked.... + o.odate <@ (SELECT drange FROM tdr) + --...or any open orders currently booked before cutoff.... + OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < (SELECT UPPER(drange) FROM tdr)) + --...or anything that shipped in that period + OR (o.sdate <@ (SELECT drange FROM tdr) AND sseas IS NOT NULL) + ) + AND fs_line = '41010' + AND calc_status <> 'CANCELED' + AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER') + ---exclude integrated quotes---- + AND version = 'ACTUALS' + GROUP BY + o.fspr + ,o.plnt + ,COALESCE(o.promo,'') + ,o.terms + ,o.bill_cust + ,o.ship_cust + ,o.dsm + ,o.part + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,greatest(least(o.sdate,gld.edat),gld.sdat) + ,ss.ssyr +) +,incr AS ( +SELECT + o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.quoten + ,o.quotel + ,o.dcodat + interval '1 year' --incremented + ,o.ddqdat + interval '1 year' --incremented + ,o.dcmdat + ,o.fesdat + ,o.dhidat + interval '1 year' --incremented + ,o.fesind + ,o.dhpost + ,gld.fspr --incremented + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.diext + ,o.ditdis + ,o.discj + ,o.dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.dsm + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.chansub + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.bill_ctry + ,o.bill_prov + ,o.bill_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.styc + ,o.colc + ,o.colgrp + ,o.coltier + ,o.colstat + ,o.sizc + ,o.pckg + ,o.kit + ,o.brnd + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldco + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.ddunit + ,o.unti + ,o.lbs + ,o.plt + ,o.plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.fb_qty + ,o.fb_val_loc + ,o.fb_val_loc_dis + ,o.fb_val_loc_qt + ,o.fb_val_loc_pl + ,o.fb_val_loc_tar + ,o.fb_cst_loc + ,o.fb_cst_loc_cur + ,o.fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + interval '1 year' --incremented + ,o.oseas + 1 --incremented + ,o.rdate + interval '1 year' --incremented + ,o.rseas + 1 --incremented + ,o.sdate + interval '1 year' --incremented + ,o.sseas + 1 --incremented + ,'b22' "version" + ,'copy' iter +FROM + baseline o + LEFT OUTER JOIN gld ON + o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat +WHERE + o.odate + interval '1 year' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr) +) +INSERT INTO rlarp.osmf_stage +SELECT * FROM incr +UNION ALL +SELECT * FROM baseline; + +COMMIT; + +---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- +--UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; + +---identify goofy ship dates: causes disconnect with sales when splicing in a forecast that has this problem------------------- +--UPDATE rlarp.osmfs SET iter = 'bad date' WHERE adj_shipdate < adj_orderdate; diff --git a/build/tots.sql b/build/tots.sql index 4300e0c..5352f7f 100644 --- a/build/tots.sql +++ b/build/tots.sql @@ -5,7 +5,7 @@ SELECT iter, sum(fb_val_loc) value_loc FROM - rlarp.osmfs_dev o + rlarp.osmf_stage o GROUP BY oseas, to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon'), From 0a9675994505bfb18834f78e86ab82f899c2094e Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 29 Apr 2021 11:16:23 -0400 Subject: [PATCH 09/23] all forecast builds become an iteration overtop existing data --- build/build_merge.sql | 320 +--------------- .../{build_pool.sql => build_pool_schema.sql} | 0 build/build_stage.sql | 349 +++++++++++++++++- 3 files changed, 348 insertions(+), 321 deletions(-) rename build/{build_pool.sql => build_pool_schema.sql} (100%) diff --git a/build/build_merge.sql b/build/build_merge.sql index 844620f..56604bb 100644 --- a/build/build_merge.sql +++ b/build/build_merge.sql @@ -1,301 +1,19 @@ -WITH -tdr AS ( - SELECT - DATERANGE('2020-05-01','2021-05-01','[)') drange - ,DATERANGE('2020-05-01' + ('1 year'::interval),'2021-05-01' + ('1 year'::interval),'[)') repl - ,'1 year'::interval AS incr - ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals plug'),('adj price'),('adj volume')) AS x(v)) iter -) -,remove AS ( - SELECT - -----------documents------------- - null::int "ddord#" - ,null::int "dditm#" - ,null::int "fgbol#" - ,null::int "fgent#" - ,null::int "diinv#" - ,null::int "dilin#" - ,null::int quoten - ,null::int quotel - ----------dates/status------------------ - ,o.odate dcodat - ,o.rdate ddqdat - ,null::date dcmdat - ,null::date fesdat - ,o.dhidat - ,null::text fesind - ,null::text dhpost - ,o.fspr - -----------measures-------------------- - ,null::numeric ddqtoi - ,null::numeric ddqtsi - ,null::numeric fgqshp - ,null::numeric diqtsh - ,null::numeric diext - ,null::numeric ditdis - ,null::jsonb discj - ,null::text dhincr - ,o.plnt - ,o.promo - ,o.return_reas - ,o.terms - ,null::text custpo - ,null::text remit_to - ,null::text bill_class - ,o.bill_cust - ,null::text bill_rep - ,null::text bill_terr - ,null::text ship_class - ,o.ship_cust - ,null::text ship_rep - ,null::text ship_terr - ,o.dsm - ,null::text account - ,null::text shipgrp - ,null::text geo - ,null::text chan - ,null::text chansub - ,null::text orig_ctry - ,null::text orig_prov - ,null::text orig_post - ,null::text bill_ctry - ,null::text bill_prov - ,null::text bill_post - ,null::text dest_ctry - ,null::text dest_prov - ,null::text dest_post - ,o.part - ,null::text styc - ,null::text colc - ,null::text colgrp - ,null::text coltier - ,null::text colstat - ,null::text sizc - ,null::text pckg - ,null::text kit - ,null::text brnd - ,null::text majg - ,null::text ming - ,null::text majs - ,null::text mins - ,null::text gldco - ,null::text gldc - ,null::text glec - ,null::text harm - ,null::text clss - ,null::text brand - ,null::text assc - ,null::text ddunit - ,null::text unti - ,null::numeric lbs - ,null::numeric plt - ,null::text plcd - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,-sum(o.fb_qty) fb_qty - ,-sum(o.fb_val_loc) fb_val_loc - ,-sum(o.fb_val_loc_dis) fb_val_loc_dis - ,-sum(o.fb_val_loc_qt) fb_val_loc_qt - ,-sum(o.fb_val_loc_pl) fb_val_loc_pl - ,-sum(o.fb_val_loc_tar) fb_val_loc_tar - ,-sum(o.fb_cst_loc) fb_cst_loc - ,-sum(o.fb_cst_loc_cur) fb_cst_loc_cur - ,-sum(o.fb_cst_loc_fut) fb_cst_loc_fut - ,o.calc_status - ,o.flag - ,o.odate - ,o.oseas - ,o.rdate - ,o.rseas - -----when null, greatest/least is just going to act like coalesce - ,o.sdate - ,o.sseas - ,o.version - ,o.iter - FROM - rlarp.osmf_dev o - CROSS JOIN tdr - WHERE - --collect all the rows in the base period and - --the destination slot for the new rows - o.odate <@ tdr.drange - OR o.odate <@ tdr.repl - GROUP BY - o.odate - ,o.rdate - ,o.dhidat - ,o.fspr - ,o.plnt - ,o.promo - ,o.return_reas - ,o.terms - ,o.bill_cust - ,o.ship_cust - ,o.dsm - ,o.part - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.calc_status - ,o.flag - ,o.odate - ,o.oseas - ,o.rdate - ,o.rseas - ,o.sdate - ,o.sseas -) -,stack AS ( -SELECT * FROM rlarp.osmf_stage -UNION ALL -SELECT * FROM remove -) -,merge_diff AS ( - SELECT - -----------documents------------- - null::int "ddord#" - ,null::int "dditm#" - ,null::int "fgbol#" - ,null::int "fgent#" - ,null::int "diinv#" - ,null::int "dilin#" - ,null::int quoten - ,null::int quotel - ----------dates/status------------------ - ,o.odate dcodat - ,o.rdate ddqdat - ,null::date dcmdat - ,null::date fesdat - ,o.dhidat - ,null::text fesind - ,null::text dhpost - ,o.fspr - -----------measures-------------------- - ,null::numeric ddqtoi - ,null::numeric ddqtsi - ,null::numeric fgqshp - ,null::numeric diqtsh - ,null::numeric diext - ,null::numeric ditdis - ,null::jsonb discj - ,null::text dhincr - ,o.plnt - ,o.promo - ,o.return_reas - ,o.terms - ,null::text custpo - ,null::text remit_to - ,null::text bill_class - ,o.bill_cust - ,null::text bill_rep - ,null::text bill_terr - ,null::text ship_class - ,o.ship_cust - ,null::text ship_rep - ,null::text ship_terr - ,o.dsm - ,null::text account - ,null::text shipgrp - ,null::text geo - ,null::text chan - ,null::text chansub - ,null::text orig_ctry - ,null::text orig_prov - ,null::text orig_post - ,null::text bill_ctry - ,null::text bill_prov - ,null::text bill_post - ,null::text dest_ctry - ,null::text dest_prov - ,null::text dest_post - ,o.part - ,null::text styc - ,null::text colc - ,null::text colgrp - ,null::text coltier - ,null::text colstat - ,null::text sizc - ,null::text pckg - ,null::text kit - ,null::text brnd - ,null::text majg - ,null::text ming - ,null::text majs - ,null::text mins - ,null::text gldco - ,null::text gldc - ,null::text glec - ,null::text harm - ,null::text clss - ,null::text brand - ,null::text assc - ,null::text ddunit - ,null::text unti - ,null::numeric lbs - ,null::numeric plt - ,null::text plcd - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,sum(o.fb_qty) fb_qty - ,sum(o.fb_val_loc) fb_val_loc - ,sum(o.fb_val_loc_dis) fb_val_loc_dis - ,sum(o.fb_val_loc_qt) fb_val_loc_qt - ,sum(o.fb_val_loc_pl) fb_val_loc_pl - ,sum(o.fb_val_loc_tar) fb_val_loc_tar - ,sum(o.fb_cst_loc) fb_cst_loc - ,sum(o.fb_cst_loc_cur) fb_cst_loc_cur - ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut - ,o.calc_status - ,o.flag - ,o.odate - ,o.oseas - ,o.rdate - ,o.rseas - -----when null, greatest/least is just going to act like coalesce - ,o.sdate - ,o.sseas - ,'actuals' - ,'merge' - FROM - stack - WHERE - --collect all the rows in the base period and - --the destination slot for the new rows - o.odate <@ tdr.drange - OR o.odate <@ tdr.repl - GROUP BY - o.odate - ,o.rdate - ,o.dhidat - ,o.fspr - ,o.plnt - ,o.promo - ,o.return_reas - ,o.terms - ,o.bill_cust - ,o.ship_cust - ,o.dsm - ,o.part - ,o.fs_line - ,o.r_currency - ,o.r_rate - ,o.c_currency - ,o.c_rate - ,o.calc_status - ,o.flag - ,o.odate - ,o.oseas - ,o.rdate - ,o.rseas - ,o.sdate - ,o.sseas -) -SELECT COUNT(*) from merge_deff +SELECT + version + ,iter + ,oseas + ,g.sspr || ' ' || to_char(odate,'Mon') omon + ,COUNT(*) cnt + ,sum(fb_val_loc * r_rate) amt +FROM + rlarp.osmf_stage o + INNER JOIN rlarp.gld g ON + o.odate <@ g.drange +GROUP BY + version + ,iter + ,oseas + ,g.sspr || ' ' || to_char(odate,'Mon') +ORDER BY + oseas + ,omon diff --git a/build/build_pool.sql b/build/build_pool_schema.sql similarity index 100% rename from build/build_pool.sql rename to build/build_pool_schema.sql diff --git a/build/build_stage.sql b/build/build_stage.sql index 47c4db8..94f8a6b 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -1,15 +1,19 @@ BEGIN; --\timing -CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; -truncate table rlarp.osmf_stage; +--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; +TRUNCATE TABLE rlarp.osmf_stage; + +DROP TABLE IF EXISTS tdr; +CREATE TEMP TABLE tdr AS ( + SELECT + DATERANGE('2020-05-01','2021-05-01','[)') drange + ,DATERANGE(('2020-05-01'::date + '1 year'::interval)::date,('2021-05-01'::date + '1 year'::interval)::date,'[)') repl + ,'1 year'::interval AS incr + ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals plug')) AS x(v)) iter +); WITH -tdr AS ( - SELECT - DATERANGE('2020-05-01','2021-05-01','[)') drange - ,(SELECT jsonb_agg(iter) FROM (VALUES('copy'),('actuals'),('actuals plug'),('adj price'),('adj volume'))) iter -) -,gld AS ( +gld AS ( SELECT N1COMP COMP ,N1CCYY FSYR @@ -140,7 +144,7 @@ tdr AS ( -----when null, greatest/least is just going to act like coalesce ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate ,ss.ssyr sseas - ,'15mo' "version" + ,'actuals' "version" ,'actuals' iter FROM rlarp.osm_dev o @@ -197,11 +201,11 @@ SELECT ,o."dilin#" ,o.quoten ,o.quotel - ,o.dcodat + interval '1 year' --incremented - ,o.ddqdat + interval '1 year' --incremented + ,o.dcodat + interval '1 year' dcodat --incremented + ,o.ddqdat + interval '1 year' ddqdat --incremented ,o.dcmdat ,o.fesdat - ,o.dhidat + interval '1 year' --incremented + ,o.dhidat + interval '1 year' dhidat --incremented ,o.fesind ,o.dhpost ,gld.fspr --incremented @@ -284,12 +288,12 @@ SELECT ,o.fb_cst_loc_fut ,o.calc_status ,o.flag - ,o.odate + interval '1 year' --incremented - ,o.oseas + 1 --incremented - ,o.rdate + interval '1 year' --incremented - ,o.rseas + 1 --incremented - ,o.sdate + interval '1 year' --incremented - ,o.sseas + 1 --incremented + ,(o.odate + interval '1 year')::date odate --incremented + ,o.oseas + 1 oseas --incremented + ,(o.rdate + interval '1 year')::date rdate --incremented + ,o.rseas + 1 rseas --incremented + ,(o.sdate + interval '1 year')::date sdate --incremented + ,o.sseas + 1 sseas --incremented ,'b22' "version" ,'copy' iter FROM @@ -299,13 +303,318 @@ FROM WHERE o.odate + interval '1 year' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr) ) -INSERT INTO rlarp.osmf_stage +,stage AS ( SELECT * FROM incr UNION ALL -SELECT * FROM baseline; +SELECT * FROM baseline +) +,remove AS ( + SELECT + -----------documents------------- + null::int "ddord#" + ,null::int "dditm#" + ,null::int "fgbol#" + ,null::int "fgent#" + ,null::int "diinv#" + ,null::int "dilin#" + ,null::int quoten + ,null::int quotel + ----------dates/status------------------ + ,o.odate dcodat + ,o.rdate ddqdat + ,null::date dcmdat + ,null::date fesdat + ,o.dhidat + ,null::text fesind + ,null::text dhpost + ,o.fspr + -----------measures-------------------- + ,null::numeric ddqtoi + ,null::numeric ddqtsi + ,null::numeric fgqshp + ,null::numeric diqtsh + ,null::numeric diext + ,null::numeric ditdis + ,null::jsonb discj + ,null::text dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,null::text custpo + ,null::text remit_to + ,null::text bill_class + ,o.bill_cust + ,null::text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,o.ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,o.dsm + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text chansub + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text bill_ctry + ,null::text bill_prov + ,null::text bill_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,o.part + ,null::text styc + ,null::text colc + ,null::text colgrp + ,null::text coltier + ,null::text colstat + ,null::text sizc + ,null::text pckg + ,null::text kit + ,null::text brnd + ,null::text majg + ,null::text ming + ,null::text majs + ,null::text mins + ,null::text gldco + ,null::text gldc + ,null::text glec + ,null::text harm + ,null::text clss + ,null::text brand + ,null::text assc + ,null::text ddunit + ,null::text unti + ,null::numeric lbs + ,null::numeric plt + ,null::text plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,-sum(o.fb_qty) fb_qty + ,-sum(o.fb_val_loc) fb_val_loc + ,-sum(o.fb_val_loc_dis) fb_val_loc_dis + ,-sum(o.fb_val_loc_qt) fb_val_loc_qt + ,-sum(o.fb_val_loc_pl) fb_val_loc_pl + ,-sum(o.fb_val_loc_tar) fb_val_loc_tar + ,-sum(o.fb_cst_loc) fb_cst_loc + ,-sum(o.fb_cst_loc_cur) fb_cst_loc_cur + ,-sum(o.fb_cst_loc_fut) fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + -----when null, greatest/least is just going to act like coalesce + ,o.sdate + ,o.sseas + ,o.version + ,o.iter + FROM + rlarp.osmfs_dev o + CROSS JOIN tdr + WHERE + --collect all the rows in the base period and + --the destination slot for the new rows + ( + o.odate <@ tdr.drange + OR o.odate <@ tdr.repl + ) + ---only merge with targeted iterations + AND tdr.iter ? o.iter + GROUP BY + o.odate + ,o.rdate + ,o.dhidat + ,o.fspr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.bill_cust + ,o.ship_cust + ,o.dsm + ,o.part + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas + ,o.version + ,o.iter +) +,stack AS ( + SELECT * FROM stage + UNION ALL + SELECT * FROM remove +) +,merge_diff AS ( + SELECT + -----------documents------------- + null::int "ddord#" + ,null::int "dditm#" + ,null::int "fgbol#" + ,null::int "fgent#" + ,null::int "diinv#" + ,null::int "dilin#" + ,null::int quoten + ,null::int quotel + ----------dates/status------------------ + ,o.odate dcodat + ,o.rdate ddqdat + ,null::date dcmdat + ,null::date fesdat + ,o.dhidat + ,null::text fesind + ,null::text dhpost + ,o.fspr + -----------measures-------------------- + ,null::numeric ddqtoi + ,null::numeric ddqtsi + ,null::numeric fgqshp + ,null::numeric diqtsh + ,null::numeric diext + ,null::numeric ditdis + ,null::jsonb discj + ,null::text dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,null::text custpo + ,null::text remit_to + ,null::text bill_class + ,o.bill_cust + ,null::text bill_rep + ,null::text bill_terr + ,null::text ship_class + ,o.ship_cust + ,null::text ship_rep + ,null::text ship_terr + ,o.dsm + ,null::text account + ,null::text shipgrp + ,null::text geo + ,null::text chan + ,null::text chansub + ,null::text orig_ctry + ,null::text orig_prov + ,null::text orig_post + ,null::text bill_ctry + ,null::text bill_prov + ,null::text bill_post + ,null::text dest_ctry + ,null::text dest_prov + ,null::text dest_post + ,o.part + ,null::text styc + ,null::text colc + ,null::text colgrp + ,null::text coltier + ,null::text colstat + ,null::text sizc + ,null::text pckg + ,null::text kit + ,null::text brnd + ,null::text majg + ,null::text ming + ,null::text majs + ,null::text mins + ,null::text gldco + ,null::text gldc + ,null::text glec + ,null::text harm + ,null::text clss + ,null::text brand + ,null::text assc + ,null::text ddunit + ,null::text unti + ,null::numeric lbs + ,null::numeric plt + ,null::text plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,sum(o.fb_qty) fb_qty + ,sum(o.fb_val_loc) fb_val_loc + ,sum(o.fb_val_loc_dis) fb_val_loc_dis + ,sum(o.fb_val_loc_qt) fb_val_loc_qt + ,sum(o.fb_val_loc_pl) fb_val_loc_pl + ,sum(o.fb_val_loc_tar) fb_val_loc_tar + ,sum(o.fb_cst_loc) fb_cst_loc + ,sum(o.fb_cst_loc_cur) fb_cst_loc_cur + ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas + --,o.version + --,o.iter + ,'actuals' AS version + ,'merge' iter + FROM + stack o + WHERE + --collect all the rows in the base period and + --the destination slot for the new rows + o.odate <@ (SELECT tdr.drange FROM tdr) + OR o.odate <@ (SELECT tdr.repl FROM tdr) + GROUP BY + o.odate + ,o.rdate + ,o.dhidat + ,o.fspr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.bill_cust + ,o.ship_cust + ,o.dsm + ,o.part + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas + --,o.version + --,o.iter +) +INSERT INTO rlarp.osmf_stage SELECT * FROM merge_diff COMMIT; +END + ---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- --UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; From b4e2ca7919aae3b309b5ab1d5ad825e4dda6e7b9 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 29 Apr 2021 13:42:10 -0400 Subject: [PATCH 10/23] target osfm_dev as forecast staging area, no need to preserve; set fspr; stage pool instead of direct insert for debug --- build/build_pool.sql | 180 ++++++++++++++++++++++++++++++++++++++ build/build_stage.sql | 40 ++++++++- build/rebuild_forecast.sh | 13 ++- 3 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 build/build_pool.sql diff --git a/build/build_pool.sql b/build/build_pool.sql new file mode 100644 index 0000000..954a24b --- /dev/null +++ b/build/build_pool.sql @@ -0,0 +1,180 @@ +BEGIN; + +DROP TABLE IF EXISTS rlarp.osm_pool_stage; + +CREATE TABLE rlarp.osm_pool_stage AS ( +WITH +repc AS ( + SELECT + LTRIM(RTRIM(C.A9)) RCODE + ,C.A30 REPP + ,COALESCE(Q.DIR,'Other') DIRECTOR + FROM + LGDAT.CODE C + LEFT OUTER JOIN RLARP.QRH Q ON + Q.QR = LTRIM(RTRIM(C.A9)) + WHERE + C.A2 = 'MM' +) +,SEG AS ( + SELECT + GLEC + ,SEGM + FROM + ( + VALUES + ('1CU','Sustainable'), + ('1SU','Sustainable'), + ('1GR','Greenhouse'), + ('1NU','Nursery'), + ('1RE','Retail'), + ('2WI','Greenhouse'), + ('3BM','Other'), + ('3CO','Other'), + ('3PE','Other'), + ('3PP','Other'), + ('4CO','Other'), + ('4RA','Other'), + ('9MI','Other'), + ('9SA','Other'), + ('9TO','Other') + ) X(GLEC, SEGM) +) +----------------------------------------------------sales major codes---------------------------------------------------------------------------------------------------------------------------------- +,SJ AS ( + SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = '' +) +----------------------------------------------------sales minor codes---------------------------------------------------------------------------------------------------------------------------------- +,SI AS ( + SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' +) +,logload AS ( + INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','merge actuals','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING * +) +SELECT + fspr + ,plnt + ,promo + ,terms + ,bill_cust||' - '||bc.bvname bill_cust_descr + ,ship_cust||' - '||sc.bvname ship_cust_descr + ,dsm + ,coalesce(repc.repp,dsm) quota_rep_descr + ,repc.director + ,account billto_group + ,shipgrp shipto_group + ,chan + ,chansub + ,CASE seg.segm + --for 1RE coded product + WHEN 'Retail' THEN + CASE o.bill_class + WHEN 'RONL' THEN 'Online' + WHEN 'RNAT' THEN 'National' + WHEN 'RMAS' THEN 'National' + ELSE + ------ ship-to class --------------------- + CASE o.chan + WHEN 'GDIS' THEN 'Distribution' + WHEN 'NDIS' THEN 'Distribution' + WHEN 'RDIS' THEN 'Distribution' + WHEN 'GDRP' THEN 'Grower' + WHEN 'NDRP' THEN 'Grower' + WHEN 'RDRP' THEN 'Distribution' + WHEN 'GDIR' THEN 'Grower' + WHEN 'NDIR' THEN 'Grower' + -------this will probably need reviewed-------- + WHEN 'RDIR' THEN 'Distribution' + WHEN 'NDIR' THEN 'Grower' + WHEN 'GDIR' THEN 'Grower' + ELSE 'Distribution' + END + END + --for 1SU 1CU coded product + WHEN 'Sustainable' THEN + CASE SUBSTRING(o.coltier,1,1) + --anything with a bio color tier is bio on the channel + WHEN 'R' THEN 'Bio' + ELSE + CASE o.glec + --any 1SU that is not bio is fiber + WHEN '1SU' THEN 'Fiber' + --any 1CU that is not bio is commercial + WHEN '1CU' THEN 'Commercial' + ELSE o.chan + END + END + ELSE o.chan + END chan_retail + ,part + ,part||coalesce(' - '||i.descr,'') part_descr + ,stlcd part_group + ,brnd branding + ,o.majg||' - '||i.majgd majg_descr + ,o.ming||' - '||i.mingd ming_descr + ,o.majs||' - '||i.majsd majs_descr + ,o.mins||' - '||i.minsd mins_descr + ,seg.segm + ,CASE WHEN o.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance + ,fs_line + ,r_currency + ,r_rate + ,c_currency + ,c_rate + ,fb_qty units + ,fb_val_loc value_loc + ,fb_val_loc * r_rate value_usd + ,fb_cst_loc_cur cost_loc + ,fb_cst_loc_cur * c_rate cost_usd + ,calc_status + ,flag + ,o.odate order_date + ,to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon') order_month + ,oseas order_season + ,rdate request_date + ,to_char(CASE WHEN extract(month FROM o.rdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.rdate),'FM00')||' - '||to_char(o.rdate,'TMMon') request_month + ,rseas request_season + ,sdate ship_date + ,to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon') ship_month + ,sseas ship_season + ,version + ,iter + ,logload.id logid + ,logload.doc->>'tag' tag + ,logload.doc->>'message' "comment" + ,logload.doc->>'type' module +FROM + rlarp.osmf_dev o + CROSS JOIN logload + LEFT OUTER JOIN lgdat.cust bc ON + bc.bvcust = o.bill_cust + LEFT OUTER JOIN lgdat.cust sc ON + sc.bvcust = o.ship_cust + LEFT OUTER JOIN repc ON + repc.rcode = o.dsm + /* + LEFT OUTER JOIN lgdat.majg ON + bqgrp = o.majg + LEFT OUTER JOIN lgdat.mmgp ON + brmgrp = o.ming + AND BRGRP = o.majg + LEFT OUTER JOIN si ON + si.bsmjcd = o.majs + AND si.bsmncd = o.mins + LEFT OUTER JOIN sj ON + sj.bsmjcd = o.majs + */ + LEFT OUTER JOIN seg ON + seg.glec = o.glec + LEFT OUTER JOIN rlarp.itemm i ON + i.item = o.part + --LIMIT 100 +) WITH DATA; + +--CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); +--ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id); +--CREATE INDEX osm_pool_logid ON rlarp.osm_pool(logid); +--GRANT ALL ON TABLE rlarp.osm_log TO api; +--GRANT ALL ON TABLE rlarp.osm_pool TO api; + +COMMIT; diff --git a/build/build_stage.sql b/build/build_stage.sql index 94f8a6b..3e19e28 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -1,7 +1,7 @@ BEGIN; --\timing --CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; -TRUNCATE TABLE rlarp.osmf_stage; +TRUNCATE TABLE rlarp.osmf_dev; DROP TABLE IF EXISTS tdr; CREATE TEMP TABLE tdr AS ( @@ -608,8 +608,44 @@ SELECT * FROM baseline ,o.sseas --,o.version --,o.iter + HAVING + sum(o.fb_qty) <> 0 + AND sum(o.fb_val_loc) <> 0 + AND sum(o.fb_cst_loc) <> 0 ) -INSERT INTO rlarp.osmf_stage SELECT * FROM merge_diff +INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff; + + +UPDATE + rlarp.osmf_dev f +SET + fspr = gld.fspr +FROM + ( + SELECT + N1COMP COMP + ,N1CCYY FSYRq + ,KPMAXP PERDS + ,N1FSPP PERD + ,to_char(N1FSYP,'FM0000') FSPR + ,N1SD01 SDAT + ,N1ED01 EDAT + ,to_char(N1ED01,'yymm') CAPR + ,N1ED01 - N1SD01 +1 NDAYS + ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR + ,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR + FROM + LGDAT.GLDATREF + INNER JOIN LGDAT.GLDATE ON + KPCOMP = N1COMP AND + KPCCYY = N1CCYY + WHERE + N1COMP = 93 + --AND DIGITS(N1FSYP) = '1901' + ) gld +WHERE + f.sdate BETWEEN gld.sdat AND gld.edat + AND coalesce(f.fspr,'') <> gld.fspr; COMMIT; diff --git a/build/rebuild_forecast.sh b/build/rebuild_forecast.sh index 16fbcd7..66e6391 100644 --- a/build/rebuild_forecast.sh +++ b/build/rebuild_forecast.sh @@ -1,8 +1,5 @@ -$PG -f build_forecast.sql -$PG -f snap_itemm.sql -$PG -f snap_cost_current.sql -$PG -f snap_customer.sql -$PG -f snap_fx.sql -$PG -f build_pool.sql -$PG -f convert_pool_all.sql -$PG -c "CALL rlarp.osm_stack_refresh();" +$PGD -f ./build_stage.sql +$PGD -f ./snap_itemm.sql +$PGD -f ./snap_cost_current.sql +$PGD -f ./snap_customer.sql +$PGD -f ./build_pool.sql From 8de3ee57179d1957b51311c719c686d3da5f36d6 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 29 Apr 2021 16:12:54 -0400 Subject: [PATCH 11/23] fiscal period was blank cause rows not to net to -0- with merge, iteration name incorrect for baseline --- build/build_stage.sql | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/build/build_stage.sql b/build/build_stage.sql index 3e19e28..e9ea42a 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -9,7 +9,7 @@ CREATE TEMP TABLE tdr AS ( DATERANGE('2020-05-01','2021-05-01','[)') drange ,DATERANGE(('2020-05-01'::date + '1 year'::interval)::date,('2021-05-01'::date + '1 year'::interval)::date,'[)') repl ,'1 year'::interval AS incr - ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals plug')) AS x(v)) iter + ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter ); WITH @@ -56,7 +56,8 @@ gld AS ( ,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat ,null::text fesind ,null::text dhpost - ,o.fspr + ---for forecasting purposes there are no open orders, populate fspr---- + ,ss.fspr -----------measures-------------------- ,null::numeric ddqtoi ,null::numeric ddqtsi @@ -169,7 +170,7 @@ gld AS ( ---exclude integrated quotes---- AND version = 'ACTUALS' GROUP BY - o.fspr + ss.fspr ,o.plnt ,COALESCE(o.promo,'') ,o.terms @@ -581,9 +582,7 @@ SELECT * FROM baseline o.odate <@ (SELECT tdr.drange FROM tdr) OR o.odate <@ (SELECT tdr.repl FROM tdr) GROUP BY - o.odate - ,o.rdate - ,o.dhidat + o.dhidat ,o.fspr ,o.plnt ,o.promo From 36d80dd29685771ee191aeca06d731aa5f939ad4 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 29 Apr 2021 18:25:09 -0400 Subject: [PATCH 12/23] relable actuals later as copy so it is considered part of the baseline --- build/build_stage.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build_stage.sql b/build/build_stage.sql index e9ea42a..31edc5e 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -573,7 +573,7 @@ SELECT * FROM baseline --,o.version --,o.iter ,'actuals' AS version - ,'merge' iter + ,'copy' iter FROM stack o WHERE From e79c0d5bb4755eb5f145ca20db52b279da6e415c Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 30 Apr 2021 11:25:44 -0400 Subject: [PATCH 13/23] round before removing, keep any rows that have a qty, value, or cost --- build/build_stage.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/build/build_stage.sql b/build/build_stage.sql index 31edc5e..05cc6ee 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -553,13 +553,13 @@ SELECT * FROM baseline ,o.r_rate ,o.c_currency ,o.c_rate - ,sum(o.fb_qty) fb_qty - ,sum(o.fb_val_loc) fb_val_loc + ,round(sum(o.fb_qty),2) fb_qty + ,round(sum(o.fb_val_loc),2) fb_val_loc ,sum(o.fb_val_loc_dis) fb_val_loc_dis ,sum(o.fb_val_loc_qt) fb_val_loc_qt ,sum(o.fb_val_loc_pl) fb_val_loc_pl ,sum(o.fb_val_loc_tar) fb_val_loc_tar - ,sum(o.fb_cst_loc) fb_cst_loc + ,round(sum(o.fb_cst_loc),2) fb_cst_loc ,sum(o.fb_cst_loc_cur) fb_cst_loc_cur ,sum(o.fb_cst_loc_fut) fb_cst_loc_fut ,o.calc_status @@ -608,9 +608,9 @@ SELECT * FROM baseline --,o.version --,o.iter HAVING - sum(o.fb_qty) <> 0 - AND sum(o.fb_val_loc) <> 0 - AND sum(o.fb_cst_loc) <> 0 + round(sum(o.fb_qty),2) <> 0 + OR round(sum(o.fb_val_loc),2) <> 0 + OR round(sum(o.fb_cst_loc),2) <> 0 ) INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff; From f2b6686f3e309202f90c220f73fa430ccfe9f7dc Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 3 May 2021 09:09:21 -0400 Subject: [PATCH 14/23] insert directly into the pool and igonore net cost amount as a filter --- build/build_pool.sql | 2 ++ build/build_stage.sql | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/build/build_pool.sql b/build/build_pool.sql index 954a24b..e1f2685 100644 --- a/build/build_pool.sql +++ b/build/build_pool.sql @@ -171,6 +171,8 @@ FROM --LIMIT 100 ) WITH DATA; +INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage; + --CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); --ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id); --CREATE INDEX osm_pool_logid ON rlarp.osm_pool(logid); diff --git a/build/build_stage.sql b/build/build_stage.sql index 05cc6ee..db18bb6 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -610,7 +610,7 @@ SELECT * FROM baseline HAVING round(sum(o.fb_qty),2) <> 0 OR round(sum(o.fb_val_loc),2) <> 0 - OR round(sum(o.fb_cst_loc),2) <> 0 + --OR round(sum(o.fb_cst_loc),2) <> 0 ) INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff; From 032736f0e97d1fe094306bd02febfc301885f24b Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 3 May 2021 12:33:34 -0400 Subject: [PATCH 15/23] convert script to live db and do rounding on final build --- build/build_pool.sql | 12 ++++++------ build/rebuild_forecast.sh | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/build/build_pool.sql b/build/build_pool.sql index e1f2685..5a58add 100644 --- a/build/build_pool.sql +++ b/build/build_pool.sql @@ -121,11 +121,11 @@ SELECT ,r_rate ,c_currency ,c_rate - ,fb_qty units - ,fb_val_loc value_loc - ,fb_val_loc * r_rate value_usd - ,fb_cst_loc_cur cost_loc - ,fb_cst_loc_cur * c_rate cost_usd + ,round(fb_qty,2) units + ,round(fb_val_loc,2) value_loc + ,round(fb_val_loc * r_rate,2) value_usd + ,round(fb_cst_loc_cur,2) cost_loc + ,round(fb_cst_loc_cur * c_rate,2) cost_usd ,calc_status ,flag ,o.odate order_date @@ -171,7 +171,7 @@ FROM --LIMIT 100 ) WITH DATA; -INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage; +--INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage; --CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); --ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id); diff --git a/build/rebuild_forecast.sh b/build/rebuild_forecast.sh index 66e6391..3bb1b33 100644 --- a/build/rebuild_forecast.sh +++ b/build/rebuild_forecast.sh @@ -1,5 +1,5 @@ -$PGD -f ./build_stage.sql -$PGD -f ./snap_itemm.sql -$PGD -f ./snap_cost_current.sql -$PGD -f ./snap_customer.sql -$PGD -f ./build_pool.sql +$PG -f ./build_stage.sql +$PG -f ./snap_itemm.sql +$PG -f ./snap_cost_current.sql +$PG -f ./snap_customer.sql +$PG -f ./build_pool.sql From b7eb94202cae8093d73ac3476544a7a2148c13f4 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 7 May 2021 10:12:33 -0400 Subject: [PATCH 16/23] 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; From e376a9df2e1e00558fc9eae2a931b302f0c6a6aa Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 7 May 2021 14:32:18 -0400 Subject: [PATCH 17/23] many changes --- offline/last_price.sql | 452 ++++++++++++++++++++++++++++------------- 1 file changed, 309 insertions(+), 143 deletions(-) diff --git a/offline/last_price.sql b/offline/last_price.sql index b14b3c4..3b5a204 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -1,4 +1,4 @@ ---BEGIN; +BEGIN; WITH ------------------goal price increases--------------------- @@ -345,7 +345,7 @@ SELECT END END rate ,ms.avg_price target - ,JSONB_AGG(DISTINCT plist.jcpric/1000) jcpric + ,JSONB_AGG(DISTINCT plist.jcpric/1000) jcprice --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- THEN 'unknown' @@ -412,149 +412,315 @@ GROUP BY --SELECT * FROM test_unique where cnt > 1 --SELECT * FROM pivot LIMIT 1000 --------------join forecast price------------------- -,fcp AS ( - SELECT - pp.productt - ,pp.product - ,pp.majg - ,pp.quota_rep_descr - ,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 - ,p.last_price - ,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------ - ,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 - pp.productt = p.productt - AND pp.majg = p.majg - AND pp.chgrp = p.chgrp - AND pp.billto_group = p.account - AND pp.shipto_group = p.shipgrp -) -SELECT * FROM fcp limit 1000 +--,fcp AS ( +-- SELECT +-- pp.productt +-- ,pp.product +-- ,pp.majg +-- ,pp.quota_rep_descr +-- ,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 +-- ,p.last_price +-- ,p.last_order +-- ,p.target +-- ,p.rate +-- ,p.jcprice +-- --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------ +-- ,least( +-- least( +-- COALESCE(p.py_asp,p.last_price), +-- COALESCE(py_gasp,target) +-- ) * (1 + p.rate), +-- (jcprice->>0)::numeric +-- ) rev_price +-- ,greatest( +-- least( +-- least( +-- COALESCE(p.py_asp,p.last_price), +-- COALESCE(py_gasp,target) +-- ) * (1 + p.rate), +-- (jcprice->>0)::numeric +-- ) - fc_price +-- ,0 +-- ) fc_adj +-- --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 +-- pp.productt = p.productt +-- AND pp.majg = p.majg +-- AND pp.chgrp = p.chgrp +-- AND pp.billto_group = p.account +-- AND pp.shipto_group = p.shipgrp +-- WHERE +-- greatest( +-- least( +-- least( +-- COALESCE(p.py_asp,p.last_price), +-- COALESCE(py_gasp,target) +-- ) * (1 + p.rate), +-- (jcprice->>0)::numeric +-- ) - fc_price +-- ,0 +-- ) <> 0 +--) +--SELECT * FROM fcp 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 * ---) ----------------build the iteration rows---------------- ---,ins AS ( +,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 * +) +---------collapse iterations----------------------- +,collapse AS ( + 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 + ,i.stlc||'.'||i.coltier||substring(i.sizc,1,3) productt + ,o.part_group + ,o.branding + ,o.majg_descr + ,o.ming_descr + ,o.majs_descr + ,o.mins_descr + ,o.segm + ,o.substance + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,SUM(o.units) units + ,SUM(o.value_loc) value_loc + ,SUM(o.value_usd) value_usd + ,SUM(o.value_loc)/SUM(o.units) price_loc + ,SUM(o.cost_loc) cost_loc + ,SUM(o.cost_usd) 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 + FROM + rlarp.osm_pool o + --need to join to itemm to get the product from osm_pool + ,rlarp.itemmv i + WHERE + i.item = o.part + GROUP BY + 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 + ,i.stlc||'.'||i.coltier||substring(i.sizc,1,3) + ,o.part_group + ,o.branding + ,o.majg_descr + ,o.ming_descr + ,o.majs_descr + ,o.mins_descr + ,o.segm + ,o.substance + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,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 + HAVING + sum(o.units) <> 0 +) +-------------build the iteration rows---------------- +,ins AS ( +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 + --,o.units + ,0::numeric units + ,greatest( + round(least( + least( + COALESCE(a.py_asp,o.price_loc), + COALESCE(a.py_gasp,a.target) + ) * (1 + a.rate), + (a.jcprice->>0)::numeric + )*o.units - o.value_loc,2) + ,0) AS value_loc + ,greatest( + round((least( + least( + COALESCE(a.py_asp,o.price_loc), + COALESCE(a.py_gasp,a.target) + ) * (1 + a.rate), + (a.jcprice->>0)::numeric + )*o.units - o.value_loc) * r_rate,2) + ,0) AS value_usd + ----debug--- + --,least( + -- least( + -- COALESCE(a.py_asp,o.price_loc), + -- COALESCE(a.py_gasp,a.target) + -- ) * (1 + a.rate), + -- (a.jcprice->>0)::numeric + --) AS rev_price + --,o.units + --,o.value_loc + --,o.value_usd + --,o.price_loc + --,a.last_price + --,a.target + --,a.jcprice + --,a.rate + --,a.py_asp + --,a.py_gasp + ----------- + ,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--- + ,'b22' AS version + ,'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 + collapse o + --need to join to itemm to get the product from osm_pool + --,fcp a + ,pivot a + ,log +WHERE + -- a.productt = o.productt + --AND a.quota_rep_descr = o.quota_rep_descr + --AND a.majg = SUBSTRING(o.majg_descr,1,3) + --AND a.billto_group = o.billto_group + --AND a.shipto_group = o.shipto_group + --AND a.chgrp = substring(o.chan,1,1) + ---only apply to 2022 orders---- + ---join p + a.productt = o.productt + AND a.majg = SUBSTRING(o.majg_descr,1,3) + AND a.chgrp = substring(o.chan,1,1) + AND a.account = o.billto_group + AND a.shipgrp = o.shipto_group + AND o.order_date >= '2021-06-01' +) +--SELECT order_season, order_month, sum(value_usd) from ins group by order_season, order_month; +INSERT INTO rlarp.osm_pool SELECT * FROM ins; +-----------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; +-- ins +--GROUP BY +-- order_season; --,del AS ( -- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING * --) @@ -562,4 +728,4 @@ SELECT * FROM fcp limit 1000 -- rlarp.osm_pool --SELECT * FROM ins; -- ---COMMIT; +COMMIT; From e0d32cf4cf33227a1c14da0669cb09889fda8c59 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 10 May 2021 16:06:55 -0400 Subject: [PATCH 18/23] get rid of commented out stuff --- offline/last_price.sql | 153 +---------------------------------------- 1 file changed, 1 insertion(+), 152 deletions(-) diff --git a/offline/last_price.sql b/offline/last_price.sql index 3b5a204..b286466 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -200,76 +200,6 @@ incr AS ( ,chgrp ) --SELECT * FROM baseline WHERE product = 'AMK06000.CBXX' ---SELECT --- p.product --- --,p.styc --- --,p.glec --- ,p.majg --- ,p.assc --- ,p.colgrp --- --,p.coltier --- --,p.sizc --- --,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 --- ,i.rate incr_rate --- ,CASE p.chgrp --- WHEN 'D' THEN .5 --- 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 --- p --- LEFT OUTER JOIN baseline bl ON --- bl.product = p.product --- AND bl.majg = p.majg --- AND bl.assc = p.assc --- 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' ---WHERE --- p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W' ---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 ----------calculate pricing as it sits in the forecast-------------- ,poolprice AS ( SELECT @@ -410,72 +340,6 @@ GROUP BY -- pivot p --) --SELECT * FROM test_unique where cnt > 1 ---SELECT * FROM pivot LIMIT 1000 ---------------join forecast price------------------- ---,fcp AS ( --- SELECT --- pp.productt --- ,pp.product --- ,pp.majg --- ,pp.quota_rep_descr --- ,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 --- ,p.last_price --- ,p.last_order --- ,p.target --- ,p.rate --- ,p.jcprice --- --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------ --- ,least( --- least( --- COALESCE(p.py_asp,p.last_price), --- COALESCE(py_gasp,target) --- ) * (1 + p.rate), --- (jcprice->>0)::numeric --- ) rev_price --- ,greatest( --- least( --- least( --- COALESCE(p.py_asp,p.last_price), --- COALESCE(py_gasp,target) --- ) * (1 + p.rate), --- (jcprice->>0)::numeric --- ) - fc_price --- ,0 --- ) fc_adj --- --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 --- pp.productt = p.productt --- AND pp.majg = p.majg --- AND pp.chgrp = p.chgrp --- AND pp.billto_group = p.account --- AND pp.shipto_group = p.shipgrp --- WHERE --- greatest( --- least( --- least( --- COALESCE(p.py_asp,p.last_price), --- COALESCE(py_gasp,target) --- ) * (1 + p.rate), --- (jcprice->>0)::numeric --- ) - fc_price --- ,0 --- ) <> 0 ---) ---SELECT * FROM fcp LIMIT 1000 ----------------create a log entry-------------------- ,log AS ( INSERT INTO @@ -712,20 +576,5 @@ WHERE --SELECT order_season, order_month, sum(value_usd) from ins group by order_season, order_month; INSERT INTO rlarp.osm_pool SELECT * FROM ins; -----------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; From b4e592bbf1517766ab421eaef68ba9383d91bb85 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 10 May 2021 16:53:36 -0400 Subject: [PATCH 19/23] exclude retail from pricing upload --- offline/last_price.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/offline/last_price.sql b/offline/last_price.sql index b286466..8379d7a 100644 --- a/offline/last_price.sql +++ b/offline/last_price.sql @@ -572,6 +572,7 @@ WHERE AND a.account = o.billto_group AND a.shipgrp = o.shipto_group AND o.order_date >= '2021-06-01' + AND o.segm <> 'Retail' ) --SELECT order_season, order_month, sum(value_usd) from ins group by order_season, order_month; INSERT INTO rlarp.osm_pool SELECT * FROM ins; From 4560c829034263bdfa85ea3d8889a528268e62c3 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 12 May 2021 08:07:57 -0400 Subject: [PATCH 20/23] initial work on forcing forecast to match targets by account --- offline/account_plug.sql | 354 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 354 insertions(+) create mode 100644 offline/account_plug.sql diff --git a/offline/account_plug.sql b/offline/account_plug.sql new file mode 100644 index 0000000..43e1f38 --- /dev/null +++ b/offline/account_plug.sql @@ -0,0 +1,354 @@ +----primarliy used for retail forcast--- +WITH +targ AS ( + SELECT + account + ,targv::numeric targv + ,targm::numeric targm + ,targv::numeric - targm::numeric targc + FROM (VALUES + ('THE HOME DEPOT','17000000',0.180869108049054,'3074775'), + ('L&L NURSERY SUPPLY INC','2350000',0.563253626935247,'1323646'), + ('WAL-MART','0',0.253163216091017,'0'), + ('FRED MEYER','2000000',0.428001050124866,'856002'), + ('AMAZON.COM','1640000',0.628251012584489,'1030332'), + ('BFG SUPPLY','1500000',0.557715153718382,'836573'), + ('MENARDS','1638401',0.371183129119907,'608147'), + ('ARETT SALES','1241808',0.534946705934419,'664301'), + ('VM INNOVATIONS-NE','1000000',0.550614145444113,'550614'), + ('CANADIAN TIRE CORP LTD','1000000',7.1071607800234802,'71072'), + ('ALLIANCE INTERNATIONAL LLC','500000',0.41320498613211,'206602'), + ('OCEAN STATE JOBBERS','300000',0.519592592494997,'155878'), + ('GRIFFIN','258240',0.459350079411253,'118623'), + ('DO IT BEST','297067',0.598819925741005,'177889'), + ('BWI','249061',0.471267420367251,'117374'), + ('BRADLEY CALDWELL INC','226211',0.594840340158987,'134560'), + ('PHILLIPS PET FOOD & SUPPLIES','194414',0.585623594407928,'113853'), + ('ACE HARDWARE','195043',0.658624729218799,'128460'), + ('EDDIS WHOLESALE GDN SUPPLIES','156305',0.492445750074503,'76972'), + ('GARD N WISE','154657',0.538554281692573,'83291'), + ('FOSTERS INC','154278',0.544753621190595,'84043'), + ('GARDEN CENTRE GROUP CO-OP','148400',0.536654631402229,'79640'), + ('TRUE VALUE','125272',0.533535971254213,'66837'), + ('RONA INC (PICK UP)ACE CANADA','125154',0.392502596997768,'49123'), + ('HB DAVIS SEED CO','103990',0.584667552206486,'60800'), + ('BOMGAARS SUPPLY INC. RETAIL','96550',0.571735536677834,'55201'), + ('CANAC MARQUIS GRENIER','84336',0.311660052989262,'26284'), + ('LOWES','82640',0.404702398184799,'33445'), + ('JENSEN DISTRIBUTION','79189',0.600674562921357,'47567'), + ('EARL MAY SEED','77321',0.583078139217855,'45084'), + ('INTERMOUNTAIN FARMERS ASSN','71161',0.363909518632671,'25896'), + ('PEAVEY INDUSTRIES LP RET','63755',0.597135536225184,'38070'), + ('SRC CORPORATION','62610',0.544747977551784,'34107'), + ('HORIZON DISTRIBUTION INC','52200',0.573814286557659,'29953'), + ('HALIFAX SEED CO INC','47859',0.488214843600939,'23365'), + ('JAVIC STEIN GARDEN CENTER RET','47332',0.630242306115199,'29831'), + ('TERIS SUPPLY SERVICES','46454',0.47092608297219,'21876'), + ('LITTLE MOUNTAIN GREENHOUSES','0',0.618549785089707,'0'), + ('WYATT QUARLES','45961',0.43093386297766,'19806'), + ('KROGER ACCOUNTING SERVICES','0',0.44059506522642,'0'), + ('AURORA WHOLESALERS, LLC','39005',0.05,'1950'), + ('CIMARRON LUMBER CO','41255',0.710738991111474,'29321'), + ('SKH WHOLESALE DIST','39498',0.557039866089005,'22002'), + ('GREENHOUSE MEGASTORE','38481',0.576006787390623,'22165'), + ('MEYER SEED CO.OF BALTIMORE INC','36907',0.575920515687234,'21255'), + ('GREEN ISLAND DISTRIBUTORS INC.','36335',0.456804427057109,'16598'), + ('ALASKA GDN & PET SUPPLY RET','34416',0.51350131612571,'17673'), + ('CY GROWERS SUPPLIES LTD','33516',0.515228915496354,'17268'), + ('PL ROHRER & BRO INC RET','32682',0.5728291738311,'18721'), + ('CISCO COMPANIES (THE)','31294',0.541898179358204,'16958'), + ('EXACTA SALES','29985',0.566851139029652,'16997'), + ('CARLIN HORTICULTURAL SUPP','29766',0.602236367122854,'17926'), + ('LEON KOROL COMPANY','27634',0.743284803218813,'-20540'), + ('FAMILY TREE NURSERY','26732',0.533201536839096,'14253'), + ('GREENHOUSE & GARDEN SUPPLY INC','28432',0.569077841320063,'16180'), + ('MIKES GARDEN CENTER','24804',0.35,'8682'), + ('HORTMARK','24148',0.590534795378758,'14260'), + ('FLORIDA HARDWARE COMPANY','22952',0.584540312012481,'13416'), + ('EARL''S FARM & GARDEN INC','20856',0.560015239964639,'11680'), + ('MIZE FARM & GARDEN SUPPLY','20317',0.545315931222579,'11079'), + ('HUMMERT INTERNATIONAL','19214',0.551774005627846,'10602'), + ('BAYERS GARDEN SHOP','18177',0.618732293603234,'11247'), + ('FEDERATED CO-OPERATIVES LTD','16347',0.57320462397634,'9370'), + ('VANDENBERG BULB CO INC','15895',0.536805989854345,'8533'), + ('RS GROWERS SUPPLY','15676',0.554424403728287,'8691'), + ('PLANTERS SEED & SPICE RET','15395',0.553402994429341,'8520'), + ('UNITED GARDEN CENTER','15303',0.530437452248006,'8117'), + ('NORTH 40 OUTFITTERS','13975',0.650681606349669,'9093'), + ('ERVA TOOL & DIE CO','13102',0.77194498381877,'10114'), + ('NITRO-PHOS FERTILIZERS','12772',0.579101021208437,'7397'), + ('OTTEN BROS','12390',0.529120693814904,'6556'), + ('GROSOUTH','12229',0.472902730308864,'5783'), + ('WAYFAIR LLC','10894',0.629842861478861,'6861'), + ('PRINCE CORPORATION','10627',0.60961110919305,'6478'), + ('BLISH-MIZE CO','10502',0.58855406150138,'6181'), + ('UNITED GARDEN CENTERS RET','10323',0.569618377072865,'5880'), + ('KC SCHAEFER SUPPLY','10176',0.556059424402277,'5658'), + ('LIFOAM INDUSTRIES LLC','9922',0.144817968696581,'1437'), + ('CARL BREHOB & SON','9909',0.490703914367658,'4862'), + ('GPF CORPORATION','8497',0.577518582216107,'4907'), + ('APACHE SEEDS LTD','7524',0.640989162803091,'4823'), + ('PRO GROWER SUPPLY','7160',0.626847725532073,'4488'), + ('ALTON GREENHOUSES','6282',0.72757677788011,'4571'), + ('VG SUPPLY CO INC','6170',0.535115688020424,'3302'), + ('PATRICK MORIN','5550',0.754110342865499,'4185'), + ('PROFESSIONAL GARDENER CO','5534',0.683089614852672,'3780'), + ('PREMIUM HORTICULTURAL SUPPLY','5260',0.561352920789087,'2953'), + ('SCHROTH WHOLESALE SUPPLY CO.','4886',0.581467907541787,'2841'), + ('WALDO & ASSOCIATES','4848',0.549115597293131,'2662'), + ('SERRES STE-ELIE','4554',0.486746530391766,'2217'), + ('JEAN H HENLE INC RET','4390',0.59124007783322,'2596'), + ('GOLDCREST DIST','4203',0.631010290556901,'2652'), + ('KEN VAN WINGERDEN GREENHOUSES','3934',0.691649231813846,'2721'), + ('PASTANCH LLC','3558',0.646557455289712,'2300'), + ('TESSMAN SEED','3490',0.526463930114026,'1837'), + ('GEMMELLS GARDEN CENTRE INC','3356',0.732614797457948,'2459'), + ('BELLE INVESTMENTS INC','2969',0.640282280523858,'1901'), + ('BLUE STAR WHOLESALE','2870',0.460127920377161,'1321'), + ('GLEN ECHO NURSERIES INC','2856',0.475371840926624,'1358'), + ('Distribution','2856',0.35,'1000'), + ('EZ GRO GARDEN','2770',0.35,'970'), + ('ENVIROGREENERY PLANTS LLC','2759',0.560239996772843,'1546'), + ('GERTENS GREENHOUSE','2669',0.35,'934'), + ('KROGER (KRG LLC)','2363',0.454740287124271,'1074'), + ('CINNABAR VALLEY','1873',0.646249752802887,'1210'), + ('EDWARDS GREENHOUSE INC','1842',0.639898469505178,'1179'), + ('DIRECT DISTRIBUTORS INC','1732',0.658284195044688,'1140'), + ('YODERS PRODUCE','1295',0.39639861227028,'514'), + ('ABC NURSERY LANDSCAPEMAIN','556',0.548465631971323,'305'), + ('DUNN SALES & MARKETING, INC.','385',0.510810215763981,'197'), + ('GARDENERS SUPPLY CO','375',0.700292682926829,'263'), + ('FARMERS COOP','355',0.378703902613677,'135'), + ('CROP PROD SERVICES INC','351',0.568089371980676,'199'), + ('ARNOLDS GREENHOUSES','309',0.803142487392462,'249'), + ('GEORGE''S FLOWERS','287',0.460093805309734,'132'), + ('HJS WHOLESALE LTD','162',0.723774463631607,'117'), + ('HOLMBERG FARMS INC','101',0.711558080808081,'72'), + ('KINNEY BONDED','93',0.520260909090909,'49'), + ('IRRIGATION PLUS INC','35',0.608262111104002,'21'), + ('GLENKIRK ASSOCIATES INC','0',CAST(NULL AS NUMERIC),'0'), + ('MILITARY PRODUCE GROUP LLC','0',CAST(NULL AS NUMERIC),'0'), + ('WONDER SOIL','0',CAST(NULL AS NUMERIC),'0'), + ('CHUCK BECK','0',CAST(NULL AS NUMERIC),'0'), + ('MENARDS INC STORE 3256 RET','0',CAST(NULL AS NUMERIC),'0'), + ('GREENSTAR PLANT-MANCHESTER','0',CAST(NULL AS NUMERIC),'0'), + ('MAIN LINE POTTERY','0',CAST(NULL AS NUMERIC),'0'), + ('PSR SALES INC','0',CAST(NULL AS NUMERIC),'0'), + ('CLEVELAND FLORAL PRODUCTS INC','0',CAST(NULL AS NUMERIC),'0'), + ('RACHEL BOWMAN','0',CAST(NULL AS NUMERIC),'0'), + ('ENG SVCS & PROD','0',CAST(NULL AS NUMERIC),'0'), + ('RITCHIE FEED & SEED','0',CAST(NULL AS NUMERIC),'0'), + ('PARK SEED (DBA JPPA)','0',CAST(NULL AS NUMERIC),'0'), + ('ART KNAPP','0',CAST(NULL AS NUMERIC),'0'), + ('VARIETY WHOLESALE (9001)','0',CAST(NULL AS NUMERIC),'0'), + ('ROY SIEMENS','0',CAST(NULL AS NUMERIC),'0'), + ('MARC DESROSIERS','0',CAST(NULL AS NUMERIC),'0'), + ('JIM REGER','0',CAST(NULL AS NUMERIC),'0'), + ('MARK WILKINSON','0',CAST(NULL AS NUMERIC),'0'), + ('SAN MARCOS GROWERS','0',CAST(NULL AS NUMERIC),'0'), + ('GRANBYS GREENHOUSE','0',CAST(NULL AS NUMERIC),'0'), + ('GIANT TIGERE STORES LIMITED','0',CAST(NULL AS NUMERIC),'0'), + ('INTEGRITY SALES & DISTRIBUTION','0',CAST(NULL AS NUMERIC),'0'), + ('GLOECKNER & CO. INC.','0',CAST(NULL AS NUMERIC),'0'), + ('DORAN MARABLE','0',CAST(NULL AS NUMERIC),'0'), + ('SEVEN OAKS PLANT SHOP INC','0',CAST(NULL AS NUMERIC),'0'), + ('BURPEE GARDEN PRODUCTS RET','0',CAST(NULL AS NUMERIC),'0'), + ('SHERRILL INC','0',CAST(NULL AS NUMERIC),'0'), + ('PARKWAY GARDENS','0',CAST(NULL AS NUMERIC),'0'), + ('GS DISTRIBUTION','0',CAST(NULL AS NUMERIC),'0'), + ('GALES GARDEN CENTER','0',CAST(NULL AS NUMERIC),'0'), + ('DAVE BODO','0',CAST(NULL AS NUMERIC),'0'), + ('FLOWERLAND GARDEN CTR OF CLEVE','0',CAST(NULL AS NUMERIC),'0'), + ('STACK-A-POTS','0',CAST(NULL AS NUMERIC),'0'), + ('BEAUTIFUL LAND PRODUCTS','0',CAST(NULL AS NUMERIC),'0'), + ('STRADERS','0',CAST(NULL AS NUMERIC),'0'), + ('WESTLAND GREENHOUSES','0',CAST(NULL AS NUMERIC),'0'), + ('SUNNYSIDE GREENHOUSES LTD. RET','0',CAST(NULL AS NUMERIC),'0'), + ('DM COLOR EXPRESS','0',CAST(NULL AS NUMERIC),'0'), + ('TERIS SVSC D''''APPROVISIONNEMNT','0',CAST(NULL AS NUMERIC),'0'), + ('YONAS GREENHOUSES','0',CAST(NULL AS NUMERIC),'0'), + ('FLORALIES JOUVENCE','0',CAST(NULL AS NUMERIC),'0'), + ('ALECIA LATINI','0',CAST(NULL AS NUMERIC),'0'), + ('AL PAR PEAT CO','0',CAST(NULL AS NUMERIC),'0'), + ('IVAN VANDERDEEN','0',CAST(NULL AS NUMERIC),'0'), + ('TIDBURY CREEK FARMS','0',CAST(NULL AS NUMERIC),'0'), + ('AGRO PRODUCTS LIMITED','0',CAST(NULL AS NUMERIC),'0'), + ('ALDI','0',CAST(NULL AS NUMERIC),'0'), + ('MISC ACCT - (US )','0',CAST(NULL AS NUMERIC),'0'), + ('TIMBERS MARKETING LLC','0',CAST(NULL AS NUMERIC),'0'), + ('ALLEN STERLING AND LOTHROP','0',CAST(NULL AS NUMERIC),'0'), + ('TINA PETTIGREW','0',CAST(NULL AS NUMERIC),'0'), + ('TONY LANDINO','0',CAST(NULL AS NUMERIC),'0'), + ('TRICIA PRICE','0',CAST(NULL AS NUMERIC),'0'), + ('PEAK DISTRIBUTION LLC','0',CAST(NULL AS NUMERIC),'0'), + ('DILLON SEED & SUPPLY','0',CAST(NULL AS NUMERIC),'0'), + ('MENARDS INC STORE 3510 RET','0',CAST(NULL AS NUMERIC),'0'), + ('PLANT BEST','0',CAST(NULL AS NUMERIC),'0'), + ('DENVER WHOLESALE FLORISTS CO','0',CAST(NULL AS NUMERIC),'0'), + ('TSC STORES LIMITED','0',CAST(NULL AS NUMERIC),'0'), + ('CENTRAL GARDEN & PET','0',CAST(NULL AS NUMERIC),'0'), + ('ESERRO','0',CAST(NULL AS NUMERIC),'0'), + ('AMA PLASTICS','0',CAST(NULL AS NUMERIC),'0'), + ('LITTLE MOUNTAIN GREENHOUSES U','0',CAST(NULL AS NUMERIC),'0'), + ('PATAPSCO VALLEY SALES','0',CAST(NULL AS NUMERIC),'0'), + ('UNITED HARDWARE','0',CAST(NULL AS NUMERIC),'0'), + ('COLOUR PARADISE GRHS LTD','0',CAST(NULL AS NUMERIC),'0'), + ('GARDEN RIDGE CORP','0',CAST(NULL AS NUMERIC),'0'), + ('VARIETY DISTRIBUTORS INC','0',CAST(NULL AS NUMERIC),'0'), + ('BLUE WATER BALTIMORE','0',CAST(NULL AS NUMERIC),'0'), + ('LEE VALLEY TOOLS','0',CAST(NULL AS NUMERIC),'0') + ) x(account,targv,marginp,targm) +) +,factor AS ( +SELECT + account + ,targv + ,targm + ,targc + ,sum(value_usd) value_usd + ,sum(units) units + ,sum(cost_usd) cost_usd + ,round(CASE WHEN sum(value_usd) = 0 THEN 0 ELSE targv::numeric/sum(value_usd) END,5) vfactor + ,round(CASE WHEN sum(cost_usd) = 0 THEN 0 ELSE targc::numeric/sum(cost_usd) END,5) cfactor +FROM + targ t + LEFT OUTER JOIN rlarp.osm_pool o ON + o.billto_group = t.account + AND o.segm = 'Retail' + AND o.order_season = 2022 +GROUP BY + account + ,targv + ,targm + ,targc +) +--SELECT * FROM agg_curr +----------------create a log entry-------------------- +--,log AS ( +-- INSERT INTO +-- rlarp.osm_log(doc) +-- SELECT +-- $${ +-- "message":"adjust select retail customers to targets sales and margin", +-- "tag":"retail", +-- "type":"build" +-- }$$::jsonb doc +-- RETURNING * +--) +-------build adjustment rows collapsed for all iterations---------- +,dv AS ( + 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 + ,round(sum(o.units*s.cfactor - o.units ),2) units + ,round(sum(o.value_loc*s.vfactor - o.value_loc ),2) value_loc + ,round(sum(o.value_usd*s.vfactor - o.value_usd ),2) value_usd + ,round(sum(o.cost_loc*s.cfactor - o.cost_loc ),2) cost_loc + ,round(sum(o.cost_usd*s.cfactor - o.cost_usd ),2) 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 + ,'b22' aS version + ,'upload volume' iter + --,log.id + --,coalescE(log.doc->>'tag','') "tag" + --,log.doc->>'message' "comment" + --,log.doc->>'type' module + FROM + rlarp.osm_pool o + INNER JOIN factor s ON + s.account = o.billto_group + --cross joIN log + WHERE + order_season = 2022 + AND segm = 'Retail' + GROUP BY + 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 + ,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 + --,log.id + --,coalescE(log.doc->>'tag','') "tag" + --,log.doc->>'message' "comment" + --,log.doc->>'type' module +) +SELECT + billto_group + ,sum(value_usd) val + ,sum(cost_usd) AS cost + ,sum(units) units +FROM + dv +GROUP BY + billto_group From 4c926b63e6e64dc8a621ced5c4f5d096d87e4c5d Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 12 May 2021 11:42:09 -0400 Subject: [PATCH 21/23] include log pieces for initial use --- offline/account_plug.sql | 448 ++++++++++++++++++++------------------- 1 file changed, 227 insertions(+), 221 deletions(-) diff --git a/offline/account_plug.sql b/offline/account_plug.sql index 43e1f38..52ad207 100644 --- a/offline/account_plug.sql +++ b/offline/account_plug.sql @@ -1,203 +1,206 @@ -----primarliy used for retail forcast--- +BEGIN; + WITH targ AS ( SELECT account + ,segment ,targv::numeric targv ,targm::numeric targm ,targv::numeric - targm::numeric targc FROM (VALUES - ('THE HOME DEPOT','17000000',0.180869108049054,'3074775'), - ('L&L NURSERY SUPPLY INC','2350000',0.563253626935247,'1323646'), - ('WAL-MART','0',0.253163216091017,'0'), - ('FRED MEYER','2000000',0.428001050124866,'856002'), - ('AMAZON.COM','1640000',0.628251012584489,'1030332'), - ('BFG SUPPLY','1500000',0.557715153718382,'836573'), - ('MENARDS','1638401',0.371183129119907,'608147'), - ('ARETT SALES','1241808',0.534946705934419,'664301'), - ('VM INNOVATIONS-NE','1000000',0.550614145444113,'550614'), - ('CANADIAN TIRE CORP LTD','1000000',7.1071607800234802,'71072'), - ('ALLIANCE INTERNATIONAL LLC','500000',0.41320498613211,'206602'), - ('OCEAN STATE JOBBERS','300000',0.519592592494997,'155878'), - ('GRIFFIN','258240',0.459350079411253,'118623'), - ('DO IT BEST','297067',0.598819925741005,'177889'), - ('BWI','249061',0.471267420367251,'117374'), - ('BRADLEY CALDWELL INC','226211',0.594840340158987,'134560'), - ('PHILLIPS PET FOOD & SUPPLIES','194414',0.585623594407928,'113853'), - ('ACE HARDWARE','195043',0.658624729218799,'128460'), - ('EDDIS WHOLESALE GDN SUPPLIES','156305',0.492445750074503,'76972'), - ('GARD N WISE','154657',0.538554281692573,'83291'), - ('FOSTERS INC','154278',0.544753621190595,'84043'), - ('GARDEN CENTRE GROUP CO-OP','148400',0.536654631402229,'79640'), - ('TRUE VALUE','125272',0.533535971254213,'66837'), - ('RONA INC (PICK UP)ACE CANADA','125154',0.392502596997768,'49123'), - ('HB DAVIS SEED CO','103990',0.584667552206486,'60800'), - ('BOMGAARS SUPPLY INC. RETAIL','96550',0.571735536677834,'55201'), - ('CANAC MARQUIS GRENIER','84336',0.311660052989262,'26284'), - ('LOWES','82640',0.404702398184799,'33445'), - ('JENSEN DISTRIBUTION','79189',0.600674562921357,'47567'), - ('EARL MAY SEED','77321',0.583078139217855,'45084'), - ('INTERMOUNTAIN FARMERS ASSN','71161',0.363909518632671,'25896'), - ('PEAVEY INDUSTRIES LP RET','63755',0.597135536225184,'38070'), - ('SRC CORPORATION','62610',0.544747977551784,'34107'), - ('HORIZON DISTRIBUTION INC','52200',0.573814286557659,'29953'), - ('HALIFAX SEED CO INC','47859',0.488214843600939,'23365'), - ('JAVIC STEIN GARDEN CENTER RET','47332',0.630242306115199,'29831'), - ('TERIS SUPPLY SERVICES','46454',0.47092608297219,'21876'), - ('LITTLE MOUNTAIN GREENHOUSES','0',0.618549785089707,'0'), - ('WYATT QUARLES','45961',0.43093386297766,'19806'), - ('KROGER ACCOUNTING SERVICES','0',0.44059506522642,'0'), - ('AURORA WHOLESALERS, LLC','39005',0.05,'1950'), - ('CIMARRON LUMBER CO','41255',0.710738991111474,'29321'), - ('SKH WHOLESALE DIST','39498',0.557039866089005,'22002'), - ('GREENHOUSE MEGASTORE','38481',0.576006787390623,'22165'), - ('MEYER SEED CO.OF BALTIMORE INC','36907',0.575920515687234,'21255'), - ('GREEN ISLAND DISTRIBUTORS INC.','36335',0.456804427057109,'16598'), - ('ALASKA GDN & PET SUPPLY RET','34416',0.51350131612571,'17673'), - ('CY GROWERS SUPPLIES LTD','33516',0.515228915496354,'17268'), - ('PL ROHRER & BRO INC RET','32682',0.5728291738311,'18721'), - ('CISCO COMPANIES (THE)','31294',0.541898179358204,'16958'), - ('EXACTA SALES','29985',0.566851139029652,'16997'), - ('CARLIN HORTICULTURAL SUPP','29766',0.602236367122854,'17926'), - ('LEON KOROL COMPANY','27634',0.743284803218813,'-20540'), - ('FAMILY TREE NURSERY','26732',0.533201536839096,'14253'), - ('GREENHOUSE & GARDEN SUPPLY INC','28432',0.569077841320063,'16180'), - ('MIKES GARDEN CENTER','24804',0.35,'8682'), - ('HORTMARK','24148',0.590534795378758,'14260'), - ('FLORIDA HARDWARE COMPANY','22952',0.584540312012481,'13416'), - ('EARL''S FARM & GARDEN INC','20856',0.560015239964639,'11680'), - ('MIZE FARM & GARDEN SUPPLY','20317',0.545315931222579,'11079'), - ('HUMMERT INTERNATIONAL','19214',0.551774005627846,'10602'), - ('BAYERS GARDEN SHOP','18177',0.618732293603234,'11247'), - ('FEDERATED CO-OPERATIVES LTD','16347',0.57320462397634,'9370'), - ('VANDENBERG BULB CO INC','15895',0.536805989854345,'8533'), - ('RS GROWERS SUPPLY','15676',0.554424403728287,'8691'), - ('PLANTERS SEED & SPICE RET','15395',0.553402994429341,'8520'), - ('UNITED GARDEN CENTER','15303',0.530437452248006,'8117'), - ('NORTH 40 OUTFITTERS','13975',0.650681606349669,'9093'), - ('ERVA TOOL & DIE CO','13102',0.77194498381877,'10114'), - ('NITRO-PHOS FERTILIZERS','12772',0.579101021208437,'7397'), - ('OTTEN BROS','12390',0.529120693814904,'6556'), - ('GROSOUTH','12229',0.472902730308864,'5783'), - ('WAYFAIR LLC','10894',0.629842861478861,'6861'), - ('PRINCE CORPORATION','10627',0.60961110919305,'6478'), - ('BLISH-MIZE CO','10502',0.58855406150138,'6181'), - ('UNITED GARDEN CENTERS RET','10323',0.569618377072865,'5880'), - ('KC SCHAEFER SUPPLY','10176',0.556059424402277,'5658'), - ('LIFOAM INDUSTRIES LLC','9922',0.144817968696581,'1437'), - ('CARL BREHOB & SON','9909',0.490703914367658,'4862'), - ('GPF CORPORATION','8497',0.577518582216107,'4907'), - ('APACHE SEEDS LTD','7524',0.640989162803091,'4823'), - ('PRO GROWER SUPPLY','7160',0.626847725532073,'4488'), - ('ALTON GREENHOUSES','6282',0.72757677788011,'4571'), - ('VG SUPPLY CO INC','6170',0.535115688020424,'3302'), - ('PATRICK MORIN','5550',0.754110342865499,'4185'), - ('PROFESSIONAL GARDENER CO','5534',0.683089614852672,'3780'), - ('PREMIUM HORTICULTURAL SUPPLY','5260',0.561352920789087,'2953'), - ('SCHROTH WHOLESALE SUPPLY CO.','4886',0.581467907541787,'2841'), - ('WALDO & ASSOCIATES','4848',0.549115597293131,'2662'), - ('SERRES STE-ELIE','4554',0.486746530391766,'2217'), - ('JEAN H HENLE INC RET','4390',0.59124007783322,'2596'), - ('GOLDCREST DIST','4203',0.631010290556901,'2652'), - ('KEN VAN WINGERDEN GREENHOUSES','3934',0.691649231813846,'2721'), - ('PASTANCH LLC','3558',0.646557455289712,'2300'), - ('TESSMAN SEED','3490',0.526463930114026,'1837'), - ('GEMMELLS GARDEN CENTRE INC','3356',0.732614797457948,'2459'), - ('BELLE INVESTMENTS INC','2969',0.640282280523858,'1901'), - ('BLUE STAR WHOLESALE','2870',0.460127920377161,'1321'), - ('GLEN ECHO NURSERIES INC','2856',0.475371840926624,'1358'), - ('Distribution','2856',0.35,'1000'), - ('EZ GRO GARDEN','2770',0.35,'970'), - ('ENVIROGREENERY PLANTS LLC','2759',0.560239996772843,'1546'), - ('GERTENS GREENHOUSE','2669',0.35,'934'), - ('KROGER (KRG LLC)','2363',0.454740287124271,'1074'), - ('CINNABAR VALLEY','1873',0.646249752802887,'1210'), - ('EDWARDS GREENHOUSE INC','1842',0.639898469505178,'1179'), - ('DIRECT DISTRIBUTORS INC','1732',0.658284195044688,'1140'), - ('YODERS PRODUCE','1295',0.39639861227028,'514'), - ('ABC NURSERY LANDSCAPEMAIN','556',0.548465631971323,'305'), - ('DUNN SALES & MARKETING, INC.','385',0.510810215763981,'197'), - ('GARDENERS SUPPLY CO','375',0.700292682926829,'263'), - ('FARMERS COOP','355',0.378703902613677,'135'), - ('CROP PROD SERVICES INC','351',0.568089371980676,'199'), - ('ARNOLDS GREENHOUSES','309',0.803142487392462,'249'), - ('GEORGE''S FLOWERS','287',0.460093805309734,'132'), - ('HJS WHOLESALE LTD','162',0.723774463631607,'117'), - ('HOLMBERG FARMS INC','101',0.711558080808081,'72'), - ('KINNEY BONDED','93',0.520260909090909,'49'), - ('IRRIGATION PLUS INC','35',0.608262111104002,'21'), - ('GLENKIRK ASSOCIATES INC','0',CAST(NULL AS NUMERIC),'0'), - ('MILITARY PRODUCE GROUP LLC','0',CAST(NULL AS NUMERIC),'0'), - ('WONDER SOIL','0',CAST(NULL AS NUMERIC),'0'), - ('CHUCK BECK','0',CAST(NULL AS NUMERIC),'0'), - ('MENARDS INC STORE 3256 RET','0',CAST(NULL AS NUMERIC),'0'), - ('GREENSTAR PLANT-MANCHESTER','0',CAST(NULL AS NUMERIC),'0'), - ('MAIN LINE POTTERY','0',CAST(NULL AS NUMERIC),'0'), - ('PSR SALES INC','0',CAST(NULL AS NUMERIC),'0'), - ('CLEVELAND FLORAL PRODUCTS INC','0',CAST(NULL AS NUMERIC),'0'), - ('RACHEL BOWMAN','0',CAST(NULL AS NUMERIC),'0'), - ('ENG SVCS & PROD','0',CAST(NULL AS NUMERIC),'0'), - ('RITCHIE FEED & SEED','0',CAST(NULL AS NUMERIC),'0'), - ('PARK SEED (DBA JPPA)','0',CAST(NULL AS NUMERIC),'0'), - ('ART KNAPP','0',CAST(NULL AS NUMERIC),'0'), - ('VARIETY WHOLESALE (9001)','0',CAST(NULL AS NUMERIC),'0'), - ('ROY SIEMENS','0',CAST(NULL AS NUMERIC),'0'), - ('MARC DESROSIERS','0',CAST(NULL AS NUMERIC),'0'), - ('JIM REGER','0',CAST(NULL AS NUMERIC),'0'), - ('MARK WILKINSON','0',CAST(NULL AS NUMERIC),'0'), - ('SAN MARCOS GROWERS','0',CAST(NULL AS NUMERIC),'0'), - ('GRANBYS GREENHOUSE','0',CAST(NULL AS NUMERIC),'0'), - ('GIANT TIGERE STORES LIMITED','0',CAST(NULL AS NUMERIC),'0'), - ('INTEGRITY SALES & DISTRIBUTION','0',CAST(NULL AS NUMERIC),'0'), - ('GLOECKNER & CO. INC.','0',CAST(NULL AS NUMERIC),'0'), - ('DORAN MARABLE','0',CAST(NULL AS NUMERIC),'0'), - ('SEVEN OAKS PLANT SHOP INC','0',CAST(NULL AS NUMERIC),'0'), - ('BURPEE GARDEN PRODUCTS RET','0',CAST(NULL AS NUMERIC),'0'), - ('SHERRILL INC','0',CAST(NULL AS NUMERIC),'0'), - ('PARKWAY GARDENS','0',CAST(NULL AS NUMERIC),'0'), - ('GS DISTRIBUTION','0',CAST(NULL AS NUMERIC),'0'), - ('GALES GARDEN CENTER','0',CAST(NULL AS NUMERIC),'0'), - ('DAVE BODO','0',CAST(NULL AS NUMERIC),'0'), - ('FLOWERLAND GARDEN CTR OF CLEVE','0',CAST(NULL AS NUMERIC),'0'), - ('STACK-A-POTS','0',CAST(NULL AS NUMERIC),'0'), - ('BEAUTIFUL LAND PRODUCTS','0',CAST(NULL AS NUMERIC),'0'), - ('STRADERS','0',CAST(NULL AS NUMERIC),'0'), - ('WESTLAND GREENHOUSES','0',CAST(NULL AS NUMERIC),'0'), - ('SUNNYSIDE GREENHOUSES LTD. RET','0',CAST(NULL AS NUMERIC),'0'), - ('DM COLOR EXPRESS','0',CAST(NULL AS NUMERIC),'0'), - ('TERIS SVSC D''''APPROVISIONNEMNT','0',CAST(NULL AS NUMERIC),'0'), - ('YONAS GREENHOUSES','0',CAST(NULL AS NUMERIC),'0'), - ('FLORALIES JOUVENCE','0',CAST(NULL AS NUMERIC),'0'), - ('ALECIA LATINI','0',CAST(NULL AS NUMERIC),'0'), - ('AL PAR PEAT CO','0',CAST(NULL AS NUMERIC),'0'), - ('IVAN VANDERDEEN','0',CAST(NULL AS NUMERIC),'0'), - ('TIDBURY CREEK FARMS','0',CAST(NULL AS NUMERIC),'0'), - ('AGRO PRODUCTS LIMITED','0',CAST(NULL AS NUMERIC),'0'), - ('ALDI','0',CAST(NULL AS NUMERIC),'0'), - ('MISC ACCT - (US )','0',CAST(NULL AS NUMERIC),'0'), - ('TIMBERS MARKETING LLC','0',CAST(NULL AS NUMERIC),'0'), - ('ALLEN STERLING AND LOTHROP','0',CAST(NULL AS NUMERIC),'0'), - ('TINA PETTIGREW','0',CAST(NULL AS NUMERIC),'0'), - ('TONY LANDINO','0',CAST(NULL AS NUMERIC),'0'), - ('TRICIA PRICE','0',CAST(NULL AS NUMERIC),'0'), - ('PEAK DISTRIBUTION LLC','0',CAST(NULL AS NUMERIC),'0'), - ('DILLON SEED & SUPPLY','0',CAST(NULL AS NUMERIC),'0'), - ('MENARDS INC STORE 3510 RET','0',CAST(NULL AS NUMERIC),'0'), - ('PLANT BEST','0',CAST(NULL AS NUMERIC),'0'), - ('DENVER WHOLESALE FLORISTS CO','0',CAST(NULL AS NUMERIC),'0'), - ('TSC STORES LIMITED','0',CAST(NULL AS NUMERIC),'0'), - ('CENTRAL GARDEN & PET','0',CAST(NULL AS NUMERIC),'0'), - ('ESERRO','0',CAST(NULL AS NUMERIC),'0'), - ('AMA PLASTICS','0',CAST(NULL AS NUMERIC),'0'), - ('LITTLE MOUNTAIN GREENHOUSES U','0',CAST(NULL AS NUMERIC),'0'), - ('PATAPSCO VALLEY SALES','0',CAST(NULL AS NUMERIC),'0'), - ('UNITED HARDWARE','0',CAST(NULL AS NUMERIC),'0'), - ('COLOUR PARADISE GRHS LTD','0',CAST(NULL AS NUMERIC),'0'), - ('GARDEN RIDGE CORP','0',CAST(NULL AS NUMERIC),'0'), - ('VARIETY DISTRIBUTORS INC','0',CAST(NULL AS NUMERIC),'0'), - ('BLUE WATER BALTIMORE','0',CAST(NULL AS NUMERIC),'0'), - ('LEE VALLEY TOOLS','0',CAST(NULL AS NUMERIC),'0') - ) x(account,targv,marginp,targm) + ('THE HOME DEPOT','17000000','3074775','Retail'), + ('L&L NURSERY SUPPLY INC','2350000','1323646','Retail'), + ('WAL-MART','0','0','Retail'), + ('FRED MEYER','2000000','856002','Retail'), + ('AMAZON.COM','1640000','1030332','Retail'), + ('BFG SUPPLY','1500000','836573','Retail'), + ('MENARDS','1638401','608147','Retail'), + ('ARETT SALES','1241808','664301','Retail'), + ('VM INNOVATIONS-NE','1000000','550614','Retail'), + ('CANADIAN TIRE CORP LTD','680606','47642','Retail'), + ('CANADIAN TIRE CORP LTD','319394','160394','Sustainable'), + ('ALLIANCE INTERNATIONAL LLC','500000','206602','Retail'), + ('OCEAN STATE JOBBERS','300000','155878','Retail'), + ('GRIFFIN','258240','118623','Retail'), + ('DO IT BEST','297067','177889','Retail'), + ('BWI','249061','117374','Retail'), + ('BRADLEY CALDWELL INC','226211','134560','Retail'), + ('PHILLIPS PET FOOD & SUPPLIES','194414','113853','Retail'), + ('ACE HARDWARE','195043','128460','Retail'), + ('EDDIS WHOLESALE GDN SUPPLIES','156305','76972','Retail'), + ('GARD N WISE','154657','83291','Retail'), + ('FOSTERS INC','154278','84043','Retail'), + ('GARDEN CENTRE GROUP CO-OP','148400','79640','Retail'), + ('TRUE VALUE','125272','66837','Retail'), + ('RONA INC (PICK UP)ACE CANADA','125154','49123','Retail'), + ('HB DAVIS SEED CO','103990','60800','Retail'), + ('BOMGAARS SUPPLY INC. RETAIL','96550','55201','Retail'), + ('CANAC MARQUIS GRENIER','84336','26284','Retail'), + ('LOWES','82640','33445','Retail'), + ('JENSEN DISTRIBUTION','79189','47567','Retail'), + ('EARL MAY SEED','77321','45084','Retail'), + ('INTERMOUNTAIN FARMERS ASSN','71161','25896','Retail'), + ('PEAVEY INDUSTRIES LP RET','63755','38070','Retail'), + ('SRC CORPORATION','62610','34107','Retail'), + ('HORIZON DISTRIBUTION INC','52200','29953','Retail'), + ('HALIFAX SEED CO INC','47859','23365','Retail'), + ('JAVIC STEIN GARDEN CENTER RET','47332','29831','Retail'), + ('TERIS SUPPLY SERVICES','46454','21876','Retail'), + ('LITTLE MOUNTAIN GREENHOUSES','0','0','Retail'), + ('WYATT QUARLES','45961','19806','Retail'), + ('KROGER ACCOUNTING SERVICES','0','0','Retail'), + ('AURORA WHOLESALERS, LLC','39005','1950','Retail'), + ('CIMARRON LUMBER CO','41255','29321','Retail'), + ('SKH WHOLESALE DIST','39498','22002','Retail'), + ('GREENHOUSE MEGASTORE','38481','22165','Retail'), + ('MEYER SEED CO.OF BALTIMORE INC','36907','21255','Retail'), + ('GREEN ISLAND DISTRIBUTORS INC.','36335','16598','Retail'), + ('ALASKA GDN & PET SUPPLY RET','34416','17673','Retail'), + ('CY GROWERS SUPPLIES LTD','33516','17268','Retail'), + ('PL ROHRER & BRO INC RET','32682','18721','Retail'), + ('CISCO COMPANIES (THE)','31294','16958','Retail'), + ('EXACTA SALES','29985','16997','Retail'), + ('CARLIN HORTICULTURAL SUPP','29766','17926','Retail'), + ('LEON KOROL COMPANY','27634','-20540','Retail'), + ('FAMILY TREE NURSERY','26732','14253','Retail'), + ('GREENHOUSE & GARDEN SUPPLY INC','28432','16180','Retail'), + ('MIKES GARDEN CENTER','24804','8682','Retail'), + ('HORTMARK','24148','14260','Retail'), + ('FLORIDA HARDWARE COMPANY','22952','13416','Retail'), + ('EARL''S FARM & GARDEN INC','20856','11680','Retail'), + ('MIZE FARM & GARDEN SUPPLY','20317','11079','Retail'), + ('HUMMERT INTERNATIONAL','19214','10602','Retail'), + ('BAYERS GARDEN SHOP','18177','11247','Retail'), + ('FEDERATED CO-OPERATIVES LTD','16347','9370','Retail'), + ('VANDENBERG BULB CO INC','15895','8533','Retail'), + ('RS GROWERS SUPPLY','15676','8691','Retail'), + ('PLANTERS SEED & SPICE RET','15395','8520','Retail'), + ('UNITED GARDEN CENTER','15303','8117','Retail'), + ('NORTH 40 OUTFITTERS','13975','9093','Retail'), + ('ERVA TOOL & DIE CO','13102','10114','Retail'), + ('NITRO-PHOS FERTILIZERS','12772','7397','Retail'), + ('OTTEN BROS','12390','6556','Retail'), + ('GROSOUTH','12229','5783','Retail'), + ('WAYFAIR LLC','10894','6861','Retail'), + ('PRINCE CORPORATION','10627','6478','Retail'), + ('BLISH-MIZE CO','10502','6181','Retail'), + ('UNITED GARDEN CENTERS RET','10323','5880','Retail'), + ('KC SCHAEFER SUPPLY','10176','5658','Retail'), + ('LIFOAM INDUSTRIES LLC','9922','1437','Retail'), + ('CARL BREHOB & SON','9909','4862','Retail'), + ('GPF CORPORATION','8497','4907','Retail'), + ('APACHE SEEDS LTD','7524','4823','Retail'), + ('PRO GROWER SUPPLY','7160','4488','Retail'), + ('ALTON GREENHOUSES','6282','4571','Retail'), + ('VG SUPPLY CO INC','6170','3302','Retail'), + ('PATRICK MORIN','5550','4185','Retail'), + ('PROFESSIONAL GARDENER CO','5534','3780','Retail'), + ('PREMIUM HORTICULTURAL SUPPLY','5260','2953','Retail'), + ('SCHROTH WHOLESALE SUPPLY CO.','4886','2841','Retail'), + ('WALDO & ASSOCIATES','4848','2662','Retail'), + ('SERRES STE-ELIE','4554','2217','Retail'), + ('JEAN H HENLE INC RET','4390','2596','Retail'), + ('GOLDCREST DIST','4203','2652','Retail'), + ('KEN VAN WINGERDEN GREENHOUSES','3934','2721','Retail'), + ('PASTANCH LLC','3558','2300','Retail'), + ('TESSMAN SEED','3490','1837','Retail'), + ('GEMMELLS GARDEN CENTRE INC','3356','2459','Retail'), + ('BELLE INVESTMENTS INC','2969','1901','Retail'), + ('BLUE STAR WHOLESALE','2870','1321','Retail'), + ('GLEN ECHO NURSERIES INC','2856','1358','Retail'), + ('Distribution','2856','1000','Retail'), + ('EZ GRO GARDEN','2770','970','Retail'), + ('ENVIROGREENERY PLANTS LLC','2759','1546','Retail'), + ('GERTENS GREENHOUSE','2669','934','Retail'), + ('KROGER (KRG LLC)','2363','1074','Retail'), + ('CINNABAR VALLEY','1873','1210','Retail'), + ('EDWARDS GREENHOUSE INC','1842','1179','Retail'), + ('DIRECT DISTRIBUTORS INC','1732','1140','Retail'), + ('YODERS PRODUCE','1295','514','Retail'), + ('ABC NURSERY LANDSCAPEMAIN','556','305','Retail'), + ('DUNN SALES & MARKETING, INC.','385','197','Retail'), + ('GARDENERS SUPPLY CO','375','263','Retail'), + ('FARMERS COOP','355','135','Retail'), + ('CROP PROD SERVICES INC','351','199','Retail'), + ('ARNOLDS GREENHOUSES','309','249','Retail'), + ('GEORGE''S FLOWERS','287','132','Retail'), + ('HJS WHOLESALE LTD','162','117','Retail'), + ('HOLMBERG FARMS INC','101','72','Retail'), + ('KINNEY BONDED','93','49','Retail'), + ('IRRIGATION PLUS INC','35','21','Retail'), + ('GLENKIRK ASSOCIATES INC','0','0','Retail'), + ('MILITARY PRODUCE GROUP LLC','0','0','Retail'), + ('WONDER SOIL','0','0','Retail'), + ('CHUCK BECK','0','0','Retail'), + ('MENARDS INC STORE 3256 RET','0','0','Retail'), + ('GREENSTAR PLANT-MANCHESTER','0','0','Retail'), + ('MAIN LINE POTTERY','0','0','Retail'), + ('PSR SALES INC','0','0','Retail'), + ('CLEVELAND FLORAL PRODUCTS INC','0','0','Retail'), + ('RACHEL BOWMAN','0','0','Retail'), + ('ENG SVCS & PROD','0','0','Retail'), + ('RITCHIE FEED & SEED','0','0','Retail'), + ('PARK SEED (DBA JPPA)','0','0','Retail'), + ('ART KNAPP','0','0','Retail'), + ('VARIETY WHOLESALE (9001)','0','0','Retail'), + ('ROY SIEMENS','0','0','Retail'), + ('MARC DESROSIERS','0','0','Retail'), + ('JIM REGER','0','0','Retail'), + ('MARK WILKINSON','0','0','Retail'), + ('SAN MARCOS GROWERS','0','0','Retail'), + ('GRANBYS GREENHOUSE','0','0','Retail'), + ('GIANT TIGERE STORES LIMITED','0','0','Retail'), + ('INTEGRITY SALES & DISTRIBUTION','0','0','Retail'), + ('GLOECKNER & CO. INC.','0','0','Retail'), + ('DORAN MARABLE','0','0','Retail'), + ('SEVEN OAKS PLANT SHOP INC','0','0','Retail'), + ('BURPEE GARDEN PRODUCTS RET','0','0','Retail'), + ('SHERRILL INC','0','0','Retail'), + ('PARKWAY GARDENS','0','0','Retail'), + ('GS DISTRIBUTION','0','0','Retail'), + ('GALES GARDEN CENTER','0','0','Retail'), + ('DAVE BODO','0','0','Retail'), + ('FLOWERLAND GARDEN CTR OF CLEVE','0','0','Retail'), + ('STACK-A-POTS','0','0','Retail'), + ('BEAUTIFUL LAND PRODUCTS','0','0','Retail'), + ('STRADERS','0','0','Retail'), + ('WESTLAND GREENHOUSES','0','0','Retail'), + ('SUNNYSIDE GREENHOUSES LTD. RET','0','0','Retail'), + ('DM COLOR EXPRESS','0','0','Retail'), + ('TERIS SVSC D''''APPROVISIONNEMNT','0','0','Retail'), + ('YONAS GREENHOUSES','0','0','Retail'), + ('FLORALIES JOUVENCE','0','0','Retail'), + ('ALECIA LATINI','0','0','Retail'), + ('AL PAR PEAT CO','0','0','Retail'), + ('IVAN VANDERDEEN','0','0','Retail'), + ('TIDBURY CREEK FARMS','0','0','Retail'), + ('AGRO PRODUCTS LIMITED','0','0','Retail'), + ('ALDI','0','0','Retail'), + ('MISC ACCT - (US )','0','0','Retail'), + ('TIMBERS MARKETING LLC','0','0','Retail'), + ('ALLEN STERLING AND LOTHROP','0','0','Retail'), + ('TINA PETTIGREW','0','0','Retail'), + ('TONY LANDINO','0','0','Retail'), + ('TRICIA PRICE','0','0','Retail'), + ('PEAK DISTRIBUTION LLC','0','0','Retail'), + ('DILLON SEED & SUPPLY','0','0','Retail'), + ('MENARDS INC STORE 3510 RET','0','0','Retail'), + ('PLANT BEST','0','0','Retail'), + ('DENVER WHOLESALE FLORISTS CO','0','0','Retail'), + ('TSC STORES LIMITED','0','0','Retail'), + ('CENTRAL GARDEN & PET','0','0','Retail'), + ('ESERRO','0','0','Retail'), + ('AMA PLASTICS','0','0','Retail'), + ('LITTLE MOUNTAIN GREENHOUSES U','0','0','Retail'), + ('PATAPSCO VALLEY SALES','0','0','Retail'), + ('UNITED HARDWARE','0','0','Retail'), + ('COLOUR PARADISE GRHS LTD','0','0','Retail'), + ('GARDEN RIDGE CORP','0','0','Retail'), + ('VARIETY DISTRIBUTORS INC','0','0','Retail'), + ('BLUE WATER BALTIMORE','0','0','Retail'), + ('LEE VALLEY TOOLS','0','0','Retail') + ) x(account,targv,targm,segment) ) ,factor AS ( SELECT @@ -224,17 +227,17 @@ GROUP BY ) --SELECT * FROM agg_curr ----------------create a log entry-------------------- ---,log AS ( --- INSERT INTO --- rlarp.osm_log(doc) --- SELECT --- $${ --- "message":"adjust select retail customers to targets sales and margin", --- "tag":"retail", --- "type":"build" --- }$$::jsonb doc --- RETURNING * ---) +,log AS ( + INSERT INTO + rlarp.osm_log(doc) + SELECT + $${ + "message":"adjust select retail customers to targets sales and margin", + "tag":"retail", + "type":"build" + }$$::jsonb doc + RETURNING * +) -------build adjustment rows collapsed for all iterations---------- ,dv AS ( SELECT @@ -285,15 +288,15 @@ GROUP BY ,o.ship_season ,'b22' aS version ,'upload volume' iter - --,log.id - --,coalescE(log.doc->>'tag','') "tag" - --,log.doc->>'message' "comment" - --,log.doc->>'type' module + ,log.id + ,coalescE(log.doc->>'tag','') AS "tag" + ,log.doc->>'message' AS "comment" + ,log.doc->>'type' module FROM rlarp.osm_pool o INNER JOIN factor s ON s.account = o.billto_group - --cross joIN log + CROSS JOIN log WHERE order_season = 2022 AND segm = 'Retail' @@ -338,17 +341,20 @@ GROUP BY ,o.ship_date --history date mix ,o.ship_month ,o.ship_season - --,log.id - --,coalescE(log.doc->>'tag','') "tag" - --,log.doc->>'message' "comment" - --,log.doc->>'type' module + ,log.id + ,coalescE(log.doc->>'tag','') + ,log.doc->>'message' + ,log.doc->>'type' ) -SELECT - billto_group - ,sum(value_usd) val - ,sum(cost_usd) AS cost - ,sum(units) units -FROM - dv -GROUP BY - billto_group +INSERT INTO rlarp.osm_pool SELECT * FROM dv; +--SELECT +-- billto_group +-- ,sum(value_usd) val +-- ,sum(cost_usd) AS cost +-- ,sum(units) units +--FROM +-- dv +--GROUP BY +-- billto_group + +COMMIT; From 1faa832c5bdfa876c247fdd51ac12a049cbb3515 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 12 May 2021 11:46:02 -0400 Subject: [PATCH 22/23] use inbound segment to hook with exisiting baseline data --- offline/account_plug.sql | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/offline/account_plug.sql b/offline/account_plug.sql index 52ad207..d964266 100644 --- a/offline/account_plug.sql +++ b/offline/account_plug.sql @@ -205,6 +205,7 @@ targ AS ( ,factor AS ( SELECT account + ,segment ,targv ,targm ,targc @@ -217,10 +218,11 @@ FROM targ t LEFT OUTER JOIN rlarp.osm_pool o ON o.billto_group = t.account - AND o.segm = 'Retail' + AND o.segm = t.segment AND o.order_season = 2022 GROUP BY account + ,segment ,targv ,targm ,targc @@ -232,8 +234,8 @@ GROUP BY rlarp.osm_log(doc) SELECT $${ - "message":"adjust select retail customers to targets sales and margin", - "tag":"retail", + "message":"force accounts to match target total value and margin", + "tag":"retail plug", "type":"build" }$$::jsonb doc RETURNING * @@ -298,8 +300,8 @@ GROUP BY s.account = o.billto_group CROSS JOIN log WHERE - order_season = 2022 - AND segm = 'Retail' + o.order_season = 2022 + AND o.segm = s.segment GROUP BY o.fspr ,o.plnt ---master data From 4f33136a1525f59d0b9818c26065babceef46aaf Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 13 May 2021 13:54:42 -0400 Subject: [PATCH 23/23] style code was not getting set as part of the item master snap --- build/convert_pool_all.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build/convert_pool_all.sql b/build/convert_pool_all.sql index 95df7af..dc8fa78 100644 --- a/build/convert_pool_all.sql +++ b/build/convert_pool_all.sql @@ -118,7 +118,8 @@ FROM UPDATE RLARP.OSMFS_DEV O SET - COLC = M.COLC + STYC = M.STLC + ,COLC = M.COLC ,COLGRP = M.COLGRP ,COLTIER = M.COLTIER ,COLSTAT = M.COLSTAT