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