Compare commits

..

No commits in common. "6e7f45a01001191e8f6a652d5b0b05c1e653a4d4" and "8e8cd605b2cc7c30958e2706286bee72fa05d9c6" have entirely different histories.

4 changed files with 11 additions and 211 deletions

View File

@ -22,7 +22,6 @@ repc AS (
WHERE
C.A2 = 'MM'
)
--select * from repc
,SEG AS (
SELECT
GLEC
@ -47,22 +46,17 @@ repc AS (
('9TO','Other')
) X(GLEC, SEGM)
)
--select * from seg
----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
)
--select * from sj
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
--select * from si
,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','1+11','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
)
--select * from logload
,loadset AS (
SELECT
fspr
,plnt
@ -180,8 +174,7 @@ FROM
seg.glec = o.glec
LEFT OUTER JOIN rlarp.itemm i ON
i.item = o.part
)
SELECT * FROM loadset
--LIMIT 100
) WITH DATA;

View File

@ -77,191 +77,15 @@ tdr AS (
,p.part_group
,p.order_date
,p.ship_date
,round(p.plan_units,2) plan_units
,round(SUM(p.plan_units) FILTER (WHERE order_date >= (SELECT upper(arange) FROM tdr)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date),2) plan_rolling
,round(SUM(p.plan_units) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ),2) plan_units_tot
--,round(SUM(COALESCE(p.plan_units,0) + COALESCE(p.diff_units,0)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date),2) net_units_agg
,round(COALESCE(p.diff_units,0) + coalesce(p.plan_units,0),2) net_units
,round(SUM(COALESCE(p.plan_units,0) + COALESCE(p.diff_units,0)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group),2) net_units_tot
--,round(p.booked,2) booked
,p.plan_units
,SUM(p.plan_units) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date) plan_units_agg
,p.diff_units
,SUM(p.diff_units) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date) diff_units_agg
,p.booked
FROM
planned p
)
---flag any rows where
,eval AS (
SELECT
p.billto_group
,p.shipto_group
,p.part_group
,p.order_date
,p.ship_date
,p.plan_units
,p.plan_rolling
,p.plan_units_tot
--,p.net_units_agg
,p.net_units_tot
,p.net_units
--,p.booked
,plan_rolling <= (p.net_units_tot - p.plan_units_tot + plan_units) flag
--,CASE WHEN (p.plan_units_agg - p.plan_units) > p.booked AND p.plan_units_agg <= p.booked * 2 THEN true ELSE false END remove
FROM
plan_ranked p
)
--select * from eval limit 10000
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"force accounts to match target total value and margin",
"tag":"retail plug",
"type":"build"
}$$::jsonb doc
RETURNING *
)
,remove AS (
SELECT
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,round(-sum(o.units ),2) units
,round(-sum(o.value_loc),2) value_loc
,round(-sum(o.value_usd),2) value_usd
,round(-sum(o.cost_loc ),2) cost_loc
,round(-sum(o.cost_usd ),2) cost_usd
,o.calc_status
,o.flag
,o.order_date
,o.order_month
,o.order_season
,o.request_date
,o.request_month
,o.request_season
,o.ship_date
,o.ship_month
,o.ship_season
,'1+11' AS version
,'pf' iter
--,log.id
--,coalescE(log.doc->>'tag','') AS "tag"
--,log.doc->>'message' AS "comment"
--,log.doc->>'type' module
FROM
rlarp.osm_pool o
INNER JOIN eval e ON
e.billto_group = o.billto_group AND
e.shipto_group = o.shipto_group AND
e.part_group = o.part_group AND
e.order_date = o.order_date AND
e.ship_date = o.ship_date
--CROSS JOIN log
WHERE
e.flag
GROUP BY
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.order_date
,o.order_month
,o.order_season
,o.request_date
,o.request_month
,o.request_season
,o.ship_date
,o.ship_month
,o.ship_season
--,log.id
--,coalescE(log.doc->>'tag','') AS "tag"
--,log.doc->>'message' AS "comment"
--,log.doc->>'type' module
-----some bad forecast swings, if one is removed it's wacky
-----cap at -1,000,000
HAVING
round(-sum(o.value_loc),2) > -1000000
)
INSERT INTO rlarp.osm_pool SELECT * FROM remove;
-------sorted pull-forward scenarios------
--SELECT
-- billto_group,
-- shipto_group,
-- part_group,
-- order_date,
-- ship_date,
-- SUM(value_usd)
--FROM
-- remove
--GROUP BY
-- billto_group,
-- shipto_group,
-- part_group,
-- order_date,
-- ship_date
--ORDER BY
-- sum(value_usd) asc
--LIMIT 1000
-----pull-forward by month-----
--SELECT
-- order_season,
-- order_month,
-- SUM(value_usd)
--FROM
-- remove
--GROUP BY
-- order_season
-- order_month
SELECT * FROM plan_ranked LIMIT 1000;
--ROLLBACK;

View File

@ -3,15 +3,13 @@ SELECT
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'plan') plan2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'plan') plan2022
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'diff') diff2021
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'diff') diff2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'pf') pf2022
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'pf') pf2022
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'diff') diff2022
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'actuals') act2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'actuals') act2022
FROM
rlarp.osm_pool
WHERE
order_season IN (2021,2022)
--AND quota_rep_descr = 'COLIN MAXWELL'
AND quota_rep_descr = 'COLIN MAXWELL'
GROUP BY
order_month

View File

@ -1,15 +0,0 @@
SELECT
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
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'plan'),0) plan2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'plan'),0) plan2022
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'diff'),0) diff2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'diff'),0) diff2022
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'actuals'),0) act2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'actuals'),0) act2022
FROM
rlarp.osmfs_dev o
WHERE
oseas IN (2021,2022)
--AND quota_rep_descr = 'COLIN MAXWELL'
GROUP BY
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')