update product notion to include color tier

This commit is contained in:
Paul Trowbridge 2021-04-21 17:24:05 -04:00
parent 683921dda0
commit 704957a2fa

View File

@ -64,6 +64,7 @@ incr AS (
,p AS ( ,p AS (
SELECT SELECT
o.part o.part
,o.styc||'.'||o.coltier||substring(o.sizc,1,3) productt
,o.styc||'.'||o.colgrp||substring(o.sizc,1,3) product ,o.styc||'.'||o.colgrp||substring(o.sizc,1,3) product
,o.glec ,o.glec
,o.styc ,o.styc
@ -119,8 +120,7 @@ incr AS (
,o.shipgrp ,o.shipgrp
,o.odate DESC ,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------------------ ------------build global py asp------------------
,baseline AS ( ,baseline AS (
SELECT SELECT
@ -137,83 +137,81 @@ SELECT * FROM p WHERE account ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' an
,assc ,assc
,chgrp ,chgrp
) )
SELECT --SELECT
p.product -- p.product
--,p.styc -- --,p.styc
--,p.glec -- --,p.glec
,p.majg -- ,p.majg
,p.assc -- ,p.assc
,p.colgrp -- ,p.colgrp
--,p.coltier -- --,p.coltier
--,p.sizc -- --,p.sizc
--,p.suffix -- --,p.suffix
,p.chgrp -- ,p.chgrp
,p.account -- ,p.account
,p.shipgrp -- ,p.shipgrp
--,bl.py_gasp -- --,bl.py_gasp
,sum(units) FILTER (WHERE oseas = 2020) py_units -- ,sum(units) FILTER (WHERE oseas = 2020) py_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units -- ,sum(units) FILTER (WHERE oseas = 2021) cy_units
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp -- ,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(avg(price) FILTER (WHERE rn = 1),5) last_price -- ,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,max(odate) FILTER (WHERE rn = 1) last_order -- ,max(odate) FILTER (WHERE rn = 1) last_order
,i.rate incr_rate -- ,i.rate incr_rate
,CASE p.chgrp -- ,CASE p.chgrp
WHEN 'D' THEN .5 -- WHEN 'D' THEN .5
ELSE CASE p.majg -- ELSE CASE p.majg
WHEN '610' THEN .02 -- WHEN '610' THEN .02
ElSE CASE p.colgrp -- ElSE CASE p.colgrp
WHEN 'B' THEN .15 -- WHEN 'B' THEN .15
WHEN 'C' THEN .20 -- WHEN 'C' THEN .20
ELSE 1 -- 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 -- END
-- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'lost'
-- ELSE 'repeat'
-- END -- END
--END flag -- END rate
FROM -- --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
p -- -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
LEFT OUTER JOIN baseline bl ON -- -- THEN 'unknown'
bl.product = p.product -- -- ELSE 'new'
AND bl.majg = p.majg -- -- END
AND bl.assc = p.assc -- -- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
AND bl.chgrp = p.chgrp -- -- THEN 'lost'
LEFT OUTER JOIN incr i ON -- -- ELSE 'repeat'
i.majg = p.majg -- -- END
AND i.assc = p.assc -- --END flag
AND i.coltier = p.coltier --FROM
AND p.glec <> '1RE' -- p
WHERE -- LEFT OUTER JOIN baseline bl ON
p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W' -- bl.product = p.product
GROUP BY -- AND bl.majg = p.majg
p.product -- AND bl.assc = p.assc
--,p.styc -- AND bl.chgrp = p.chgrp
--,p.glec -- LEFT OUTER JOIN incr i ON
,p.majg -- i.majg = p.majg
,p.assc -- AND i.assc = p.assc
--,p.coltier -- AND i.coltier = p.coltier
,p.colgrp -- AND p.glec <> '1RE'
--,p.sizc --WHERE
--,p.suffix -- p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W'
,p.chgrp --GROUP BY
,p.account -- p.product
,p.shipgrp -- --,p.styc
--,bl.py_gasp -- --,p.glec
,i.rate -- ,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-------------- ----------calculate pricing as it sits in the forecast--------------
,poolprice AS ( ,poolprice AS (
SELECT 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 ,o.quota_rep_descr
,substring(majg,1,3) majg ,substring(majg,1,3) majg
,substring(chan,1,1) chgrp ,substring(chan,1,1) chgrp
@ -236,7 +234,8 @@ WHERE
--only include baseline stuff--- --only include baseline stuff---
AND segm <> 'Retail' AND segm <> 'Retail'
GROUP BY 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 ,o.quota_rep_descr
,substring(majg,1,3) ,substring(majg,1,3)
,substring(chan,1,1) ,substring(chan,1,1)
@ -250,7 +249,8 @@ HAVING
----------pivot the pricing out into columns per customer/product-------- ----------pivot the pricing out into columns per customer/product--------
,pivot AS ( ,pivot AS (
SELECT SELECT
p.product p.productt
,p.product
--,p.styc --,p.styc
--,p.glec --,p.glec
,p.majg ,p.majg
@ -266,6 +266,7 @@ SELECT
,sum(units) FILTER (WHERE oseas = 2020) py_units ,sum(units) FILTER (WHERE oseas = 2020) py_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units ,sum(units) FILTER (WHERE oseas = 2021) cy_units
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp ,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(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 ,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,max(odate) FILTER (WHERE rn = 1) last_order ,max(odate) FILTER (WHERE rn = 1) last_order
,CASE p.chgrp ,CASE p.chgrp
@ -302,7 +303,8 @@ FROM
AND i.coltier = p.coltier AND i.coltier = p.coltier
AND p.glec <> '1RE' AND p.glec <> '1RE'
GROUP BY GROUP BY
p.product p.productt
,p.product
--,p.styc --,p.styc
--,p.glec --,p.glec
,p.majg ,p.majg
@ -317,40 +319,46 @@ GROUP BY
,bl.py_gasp ,bl.py_gasp
,i.rate ,i.rate
) )
,test_unique 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 --SELECT
-- pp.product -- p.*
-- ,pp.quota_rep_descr -- ,count(*) OVER (partition by productt, majg, chgrp, account, shipgrp) cnt
-- ,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 --FROM
-- poolprice pp -- pivot p
-- 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 --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----------------- ----------------create the new price-----------------
--,adj AS ( --,adj AS (
--SELECT --SELECT