many changes

This commit is contained in:
Paul Trowbridge 2021-05-07 14:32:18 -04:00
parent b7eb94202c
commit e376a9df2e

View File

@ -1,4 +1,4 @@
--BEGIN; BEGIN;
WITH WITH
------------------goal price increases--------------------- ------------------goal price increases---------------------
@ -345,7 +345,7 @@ SELECT
END END
END rate END rate
,ms.avg_price target ,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 --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
-- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'unknown' -- THEN 'unknown'
@ -412,149 +412,315 @@ GROUP BY
--SELECT * FROM test_unique where cnt > 1 --SELECT * FROM test_unique where cnt > 1
--SELECT * FROM pivot LIMIT 1000 --SELECT * FROM pivot LIMIT 1000
--------------join forecast price------------------- --------------join forecast price-------------------
,fcp AS ( --,fcp AS (
SELECT -- SELECT
pp.productt -- pp.productt
,pp.product -- ,pp.product
,pp.majg -- ,pp.majg
,pp.quota_rep_descr -- ,pp.quota_rep_descr
,pp.order_season -- ,pp.order_season
,pp.billto_group -- ,pp.billto_group
,pp.shipto_group -- ,pp.shipto_group
,pp.chgrp -- ,pp.chgrp
,pp.fc_units -- ,pp.fc_units
,pp.fc_price -- ,pp.fc_price
,pp.iters -- ,pp.iters
--,p.item -- --,p.item
,p.py_gasp -- ,p.py_gasp
,p.cy_units -- ,p.cy_units
,p.py_asp -- ,p.py_asp
,p.last_price -- ,p.last_price
,p.last_order -- ,p.last_order
,p.target -- ,p.target
,p.rate -- ,p.rate
,p.jcpric -- ,p.jcprice
--need to link in targets pricing and price list for cap purposes--------- -- --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------ -- --also need to link regional price lists so we don't blow past those------
,least( -- ,least(
min( -- least(
COALESCE(p.py_asp,p.last_price), -- COALESCE(p.py_asp,p.last_price),
COALESCE(py_gasp,target) -- COALESCE(py_gasp,target)
) * (1 + p.rate), -- ) * (1 + p.rate),
jcprice->>0 -- (jcprice->>0)::numeric
) fc_price -- ) rev_price
--last season price + % capped at list or py_gasp + % -- ,greatest(
--last price + % capped at list or pg_gasp + % -- least(
FROM -- least(
poolprice pp -- COALESCE(p.py_asp,p.last_price),
LEFT OUTER JOIN pivot p ON -- COALESCE(py_gasp,target)
pp.productt = p.productt -- ) * (1 + p.rate),
AND pp.majg = p.majg -- (jcprice->>0)::numeric
AND pp.chgrp = p.chgrp -- ) - fc_price
AND pp.billto_group = p.account -- ,0
AND pp.shipto_group = p.shipgrp -- ) fc_adj
) -- --last season price + % capped at list or py_gasp + %
SELECT * FROM fcp limit 1000 -- --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-------------------- ----------------create a log entry--------------------
--,log AS ( ,log AS (
-- INSERT INTO INSERT INTO
-- rlarp.osm_log(doc) rlarp.osm_log(doc)
-- SELECT SELECT
-- $${ $${
-- "message":"application of last price and target increases to all forecast orders", "message":"application of last price and target increases to all forecast orders",
-- "tag":"last price", "tag":"last price",
-- "type":"build" "type":"build"
-- }$$::jsonb doc }$$::jsonb doc
-- RETURNING * RETURNING *
--) )
---------------build the iteration rows---------------- ---------collapse iterations-----------------------
--,ins AS ( ,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 --SELECT
-- o.fspr -- order_season
-- ,o.plnt ---master data -- ,sum(value_loc) val_loc
-- ,o.promo --history date mix -- ,sum(value_usd) val_usd
-- ,o.terms
-- ,o.bill_cust_descr --history cust mix
-- ,o.ship_cust_descr --history cust mix
-- ,o.dsm
-- ,o.quota_rep_descr --master data
-- ,o.director
-- ,o.billto_group --master data
-- ,o.shipto_group
-- ,o.chan --master data
-- ,o.chansub
-- ,o.chan_retail
-- ,o.part
-- ,o.part_descr
-- ,o.part_group
-- ,o.branding
-- ,o.majg_descr
-- ,o.ming_descr
-- ,o.majs_descr
-- ,o.mins_descr
-- ,o.segm
-- ,o.substance
-- ,o.fs_line --master data
-- ,o.r_currency --history cust mix
-- ,o.r_rate --master data
-- ,o.c_currency --master data
-- ,o.c_rate --master data
-- ,0::numeric units
-- ,ROUND(o.units * (a.price_increment/o.r_rate),2) value_loc
-- ,ROUND(o.units * a.price_increment,2) value_usd
-- ,0::numeric cost_loc
-- ,0::numeric cost_usd
-- ,o.calc_status --0
-- ,o.flag --0
-- ,o.order_date --history date mix
-- ,o.order_month
-- ,o.order_season
-- ,o.request_date --history date mix
-- ,o.request_month
-- ,o.request_season
-- ,o.ship_date --history date mix
-- ,o.ship_month
-- ,o.ship_season
-- ,o.version
-- ---this iteration has to be listed in the master template file in order to be effectively included---
-- ,'upload price' iter
-- ,log.id
-- ,COALESCE(log.doc->>'tag','') "tag"
-- ,log.doc->>'message' "comment"
-- ,log.doc->>'type' module
-- -----------debug columns---------
-- --,value_usd/units price
-- --,a.py_gasp
-- --,a.rate
-- --,a.last_price
-- --,a.price_increment
--FROM --FROM
-- rlarp.osm_pool o -- ins
-- ,rlarp.itemmv i --GROUP BY
-- ,adj a -- order_season;
-- ,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 ( --,del AS (
-- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING * -- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING *
--) --)
@ -562,4 +728,4 @@ SELECT * FROM fcp limit 1000
-- rlarp.osm_pool -- rlarp.osm_pool
--SELECT * FROM ins; --SELECT * FROM ins;
-- --
--COMMIT; COMMIT;