Compare commits

...

4 Commits

4 changed files with 211 additions and 11 deletions

View File

@ -22,6 +22,7 @@ repc AS (
WHERE WHERE
C.A2 = 'MM' C.A2 = 'MM'
) )
--select * from repc
,SEG AS ( ,SEG AS (
SELECT SELECT
GLEC GLEC
@ -46,17 +47,22 @@ repc AS (
('9TO','Other') ('9TO','Other')
) X(GLEC, SEGM) ) X(GLEC, SEGM)
) )
--select * from seg
----------------------------------------------------sales major codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS ( ,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
) )
--select * from sj
----------------------------------------------------sales minor codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS ( ,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
) )
--select * from si
,logload AS ( ,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 * 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 SELECT
fspr fspr
,plnt ,plnt
@ -174,7 +180,8 @@ FROM
seg.glec = o.glec seg.glec = o.glec
LEFT OUTER JOIN rlarp.itemm i ON LEFT OUTER JOIN rlarp.itemm i ON
i.item = o.part i.item = o.part
--LIMIT 100 )
SELECT * FROM loadset
) WITH DATA; ) WITH DATA;

View File

@ -77,15 +77,191 @@ tdr AS (
,p.part_group ,p.part_group
,p.order_date ,p.order_date
,p.ship_date ,p.ship_date
,p.plan_units ,round(p.plan_units,2) 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 ,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
,p.diff_units ,round(SUM(p.plan_units) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ),2) plan_units_tot
,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 --,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
,p.booked ,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
FROM FROM
planned p planned p
) )
SELECT * FROM plan_ranked LIMIT 1000; ---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
--ROLLBACK; --ROLLBACK;

15
inquirey/osmf_months.sql Normal file
View File

@ -0,0 +1,15 @@
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')

View File

@ -3,13 +3,15 @@ SELECT
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'plan') plan2021 ,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 = 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 = 'diff') diff2022 ,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 = 2021 AND iter = 'actuals') act2021 ,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'actuals') act2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'actuals') act2022 ,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'actuals') act2022
FROM FROM
rlarp.osm_pool rlarp.osm_pool
WHERE WHERE
order_season IN (2021,2022) order_season IN (2021,2022)
AND quota_rep_descr = 'COLIN MAXWELL' --AND quota_rep_descr = 'COLIN MAXWELL'
GROUP BY GROUP BY
order_month order_month