Compare commits
3 Commits
be9ca7b848
...
28c14ece67
Author | SHA1 | Date | |
---|---|---|---|
28c14ece67 | |||
f8bf1518f9 | |||
b831dfc35e |
@ -17,10 +17,12 @@ WITH
|
|||||||
---these dates will have to be syncronized with the forecast build-----------
|
---these dates will have to be syncronized with the forecast build-----------
|
||||||
tdr AS (
|
tdr AS (
|
||||||
SELECT
|
SELECT
|
||||||
-----------actuals into baseline-------------------------------------
|
-----------intented forecast range-----------------------------------
|
||||||
DATERANGE('2020-06-01','2021-07-12','[]') arange
|
DATERANGE('2021-06-01','2022-06-01','[)') prange
|
||||||
-----------plan into baseline----------------------------------------
|
-----------explicitly call out overlap period------------------------
|
||||||
,DATERANGE('2021-06-01','2022-06-01','[)') prange
|
-----------anythign in this period has to net out to match actuals---
|
||||||
|
,DATERANGE('2021-06-01','2021-07-12','[]') overlap
|
||||||
|
,jsonb_build_array('plan','diff') iter
|
||||||
)
|
)
|
||||||
,booked AS materialized(
|
,booked AS materialized(
|
||||||
SELECT
|
SELECT
|
||||||
@ -32,8 +34,7 @@ tdr AS (
|
|||||||
rlarp.osm_pool
|
rlarp.osm_pool
|
||||||
WHERE
|
WHERE
|
||||||
--find actual orders in the overlap range (where orders fall in both target ranges)
|
--find actual orders in the overlap range (where orders fall in both target ranges)
|
||||||
order_date <@ (SELECT arange FROM tdr)
|
order_date <@ (SELECT overlap FROM tdr)
|
||||||
AND order_date <@ (SELECT prange FROM tdr)
|
|
||||||
--anything that falls in this range is supposed to already match actuals, so no need to filter for actuals
|
--anything that falls in this range is supposed to already match actuals, so no need to filter for actuals
|
||||||
GROUP BY
|
GROUP BY
|
||||||
billto_group
|
billto_group
|
||||||
@ -61,7 +62,7 @@ tdr AS (
|
|||||||
--budget orders have already been trued up to match actual, so should not be included in the this isolation
|
--budget orders have already been trued up to match actual, so should not be included in the this isolation
|
||||||
--find actual orders in the overlap range (where orders fall in both target ranges)
|
--find actual orders in the overlap range (where orders fall in both target ranges)
|
||||||
p.order_date <@ (SELECT prange FROM tdr)
|
p.order_date <@ (SELECT prange FROM tdr)
|
||||||
AND p.iter IN ('plan','diff')
|
AND (SELECT iter FROM tdr) ? p.iter
|
||||||
GROUP BY
|
GROUP BY
|
||||||
p.billto_group
|
p.billto_group
|
||||||
,p.shipto_group
|
,p.shipto_group
|
||||||
@ -78,7 +79,7 @@ tdr AS (
|
|||||||
,p.order_date
|
,p.order_date
|
||||||
,p.ship_date
|
,p.ship_date
|
||||||
,round(p.plan_units,2) plan_units
|
,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) FILTER (WHERE order_date >= (SELECT upper(overlap) 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(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(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(COALESCE(p.diff_units,0) + coalesce(p.plan_units,0),2) net_units
|
||||||
@ -114,8 +115,8 @@ tdr AS (
|
|||||||
rlarp.osm_log(doc)
|
rlarp.osm_log(doc)
|
||||||
SELECT
|
SELECT
|
||||||
$${
|
$${
|
||||||
"message":"force accounts to match target total value and margin",
|
"message":"remove future orders to the extent the new forecast totals more than the original budget",
|
||||||
"tag":"retail plug",
|
"tag":"1+11",
|
||||||
"type":"build"
|
"type":"build"
|
||||||
}$$::jsonb doc
|
}$$::jsonb doc
|
||||||
RETURNING *
|
RETURNING *
|
||||||
@ -169,10 +170,10 @@ tdr AS (
|
|||||||
,o.ship_season
|
,o.ship_season
|
||||||
,'1+11' AS version
|
,'1+11' AS version
|
||||||
,'pf' iter
|
,'pf' iter
|
||||||
--,log.id
|
,log.id
|
||||||
--,coalescE(log.doc->>'tag','') AS "tag"
|
,coalescE(log.doc->>'tag','') AS "tag"
|
||||||
--,log.doc->>'message' AS "comment"
|
,log.doc->>'message' AS "comment"
|
||||||
--,log.doc->>'type' module
|
,log.doc->>'type' module
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool o
|
rlarp.osm_pool o
|
||||||
INNER JOIN eval e ON
|
INNER JOIN eval e ON
|
||||||
@ -181,7 +182,7 @@ tdr AS (
|
|||||||
e.part_group = o.part_group AND
|
e.part_group = o.part_group AND
|
||||||
e.order_date = o.order_date AND
|
e.order_date = o.order_date AND
|
||||||
e.ship_date = o.ship_date
|
e.ship_date = o.ship_date
|
||||||
--CROSS JOIN log
|
CROSS JOIN log
|
||||||
WHERE
|
WHERE
|
||||||
e.flag
|
e.flag
|
||||||
GROUP BY
|
GROUP BY
|
||||||
@ -225,10 +226,10 @@ tdr AS (
|
|||||||
,o.ship_date
|
,o.ship_date
|
||||||
,o.ship_month
|
,o.ship_month
|
||||||
,o.ship_season
|
,o.ship_season
|
||||||
--,log.id
|
,log.id
|
||||||
--,coalescE(log.doc->>'tag','') AS "tag"
|
,coalescE(log.doc->>'tag','')
|
||||||
--,log.doc->>'message' AS "comment"
|
,log.doc->>'message'
|
||||||
--,log.doc->>'type' module
|
,log.doc->>'type'
|
||||||
-----some bad forecast swings, if one is removed it's wacky
|
-----some bad forecast swings, if one is removed it's wacky
|
||||||
-----cap at -1,000,000
|
-----cap at -1,000,000
|
||||||
HAVING
|
HAVING
|
||||||
|
@ -16,13 +16,13 @@ DROP TABLE IF EXISTS tdr;
|
|||||||
CREATE TEMP TABLE tdr AS (
|
CREATE TEMP TABLE tdr AS (
|
||||||
SELECT
|
SELECT
|
||||||
-----------actuals into baseline-------------------------------------
|
-----------actuals into baseline-------------------------------------
|
||||||
DATERANGE('2020-06-01','2021-07-12','[]') arange
|
DATERANGE('2020-06-01','2021-07-15','[]') arange
|
||||||
-----------plan into baseline----------------------------------------
|
-----------plan into baseline----------------------------------------
|
||||||
,DATERANGE('2021-06-01','2022-06-01','[)') prange
|
,DATERANGE('2020-06-01','2022-06-01','[)') prange
|
||||||
-----------forecast into baseline------------------------------------
|
-----------forecast into baseline------------------------------------
|
||||||
,DATERANGE('2022-06-01','2022-06-01','[)') frange -- this range effectively excludes
|
,DATERANGE('2022-06-01','2022-06-01','[)') frange -- this range effectively excludes
|
||||||
-----------baseline selection for increment--------------------------
|
-----------baseline selection for increment--------------------------
|
||||||
,DATERANGE('2021-07-08','2021-07-08','[)') selection
|
,DATERANGE('2021-07-15','2021-07-15','[)') selection
|
||||||
-----------selection increment size----------------------------------
|
-----------selection increment size----------------------------------
|
||||||
,'0 year'::interval AS incr
|
,'0 year'::interval AS incr
|
||||||
-----------iterations to merge with----------------------------------
|
-----------iterations to merge with----------------------------------
|
||||||
|
@ -113,7 +113,9 @@ SELECT
|
|||||||
iter,
|
iter,
|
||||||
tag
|
tag
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool;
|
rlarp.osm_pool
|
||||||
|
WHERE
|
||||||
|
version <> 'actuals'
|
||||||
|
|
||||||
-------need to set item master values before other things-----------
|
-------need to set item master values before other things-----------
|
||||||
UPDATE
|
UPDATE
|
||||||
|
@ -6,10 +6,10 @@ target AS (select target_volume vincr, target_price pincr)
|
|||||||
,testv AS (
|
,testv AS (
|
||||||
SELECT
|
SELECT
|
||||||
sum(units) tot
|
sum(units) tot
|
||||||
,sum(units) FILTER (WHERE iter = 'copy') base
|
,sum(units) FILTER (WHERE iter IN ('copy','plan','diff')) base
|
||||||
,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart
|
,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart
|
||||||
,sum(value_loc *r_rate) totsales
|
,sum(value_loc *r_rate) totsales
|
||||||
,sum(value_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
|
,sum(value_loc *r_rate) FILTER (WHERE iter IN ('plan','diff','copy')) basesales
|
||||||
,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales
|
,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool
|
rlarp.osm_pool
|
||||||
@ -114,7 +114,7 @@ target AS (select target_volume vincr, target_price pincr)
|
|||||||
-----------------additional params-------------------
|
-----------------additional params-------------------
|
||||||
AND CASE (SELECT flag FROM flagv)
|
AND CASE (SELECT flag FROM flagv)
|
||||||
WHEN 'scale all' THEN true
|
WHEN 'scale all' THEN true
|
||||||
WHEN 'scale copy' THEN iter = 'copy'
|
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
|
||||||
WHEN 'scale new part' THEN module = 'new basket'
|
WHEN 'scale new part' THEN module = 'new basket'
|
||||||
END
|
END
|
||||||
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
||||||
@ -236,7 +236,7 @@ SELECT
|
|||||||
-----------------additional params-------------------
|
-----------------additional params-------------------
|
||||||
AND CASE (SELECT flag FROM flagv)
|
AND CASE (SELECT flag FROM flagv)
|
||||||
WHEN 'scale all' THEN true
|
WHEN 'scale all' THEN true
|
||||||
WHEN 'scale copy' THEN iter = 'copy'
|
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
|
||||||
WHEN 'scale new part' THEN module = 'new basket'
|
WHEN 'scale new part' THEN module = 'new basket'
|
||||||
END
|
END
|
||||||
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
||||||
|
@ -10,10 +10,10 @@ SELECT
|
|||||||
,doc->>'stamp' stamp
|
,doc->>'stamp' stamp
|
||||||
,logid
|
,logid
|
||||||
,tag
|
,tag
|
||||||
,sum(value_usd) FILTER (WHERE iter = 'copy') sales
|
,sum(value_usd) FILTER (WHERE iter IN ('plan','diff','copy')) sales
|
||||||
,sum(cost_usd) FILTER (WHERE iter = 'copy') scogs
|
,sum(cost_usd) FILTER (WHERE iter IN ('plan','diff','copy')) scogs
|
||||||
,sum(value_usd) FILTER (WHERE iter <> 'copy') sales_adj
|
,sum(value_usd) FILTER (WHERE iter <> NOT IN ('plan','diff','copy')) sales_adj
|
||||||
,sum(cost_usd) FILTER (WHERE iter <> 'copy') scogs_adj
|
,sum(cost_usd) FILTER (WHERE iter <> NOT IN ('plan','diff','copy')) scogs_adj
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool
|
rlarp.osm_pool
|
||||||
LEFT OUTER JOIN rlarp.osm_log ON
|
LEFT OUTER JOIN rlarp.osm_log ON
|
||||||
|
@ -3,7 +3,7 @@ target AS (select incr_qty qincr)
|
|||||||
,testv AS (
|
,testv AS (
|
||||||
SELECT
|
SELECT
|
||||||
sum(units) tot
|
sum(units) tot
|
||||||
,sum(units) FILTER (WHERE iter = 'copy') base
|
,sum(units) FILTER (WHERE iter IN ('copy','plan','diff')) base
|
||||||
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool p
|
rlarp.osm_pool p
|
||||||
@ -91,13 +91,13 @@ target AS (select incr_qty qincr)
|
|||||||
-----------------additional params-------------------
|
-----------------additional params-------------------
|
||||||
AND CASE (SELECT flag FROM flagv)
|
AND CASE (SELECT flag FROM flagv)
|
||||||
WHEN 'scale all' THEN true
|
WHEN 'scale all' THEN true
|
||||||
WHEN 'scale copy' THEN iter = 'copy'
|
WHEN 'scale copy' THEN iter IN ('copy','plan','diff')
|
||||||
WHEN 'scale new part' THEN module = 'new basket'
|
WHEN 'scale new part' THEN module = 'new basket'
|
||||||
END
|
END
|
||||||
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
||||||
AND order_date <= ship_date
|
AND order_date <= ship_date
|
||||||
GROUP BY
|
GROUP BY
|
||||||
fspr
|
fspr
|
||||||
,plnt ---master data
|
,plnt ---master data
|
||||||
,promo --history date mix
|
,promo --history date mix
|
||||||
,terms
|
,terms
|
||||||
|
@ -3,7 +3,7 @@ target AS (select target_vol vincr, target_prc pincr)
|
|||||||
,testv AS (
|
,testv AS (
|
||||||
SELECT
|
SELECT
|
||||||
sum(units) tot
|
sum(units) tot
|
||||||
,sum(units) FILTER (WHERE iter = 'copy') base
|
,sum(units) FILTER (WHERE iter IN ('plan','diff','copy')) base
|
||||||
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool
|
rlarp.osm_pool
|
||||||
@ -91,7 +91,7 @@ target AS (select target_vol vincr, target_prc pincr)
|
|||||||
-----------------additional params-------------------
|
-----------------additional params-------------------
|
||||||
AND CASE (SELECT flag FROM flagv)
|
AND CASE (SELECT flag FROM flagv)
|
||||||
WHEN 'scale all' THEN true
|
WHEN 'scale all' THEN true
|
||||||
WHEN 'scale copy' THEN iter = 'copy'
|
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
|
||||||
WHEN 'scale new part' THEN module = 'new basket'
|
WHEN 'scale new part' THEN module = 'new basket'
|
||||||
END
|
END
|
||||||
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
||||||
|
@ -3,7 +3,7 @@ target AS (select target_vol vincr, target_prc pincr)
|
|||||||
,testv AS (
|
,testv AS (
|
||||||
SELECT
|
SELECT
|
||||||
sum(units) tot
|
sum(units) tot
|
||||||
,sum(units) FILTER (WHERE iter = 'copy') base
|
,sum(units) FILTER (WHERE iter IN ('plan','diff','copy')) base
|
||||||
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
,sum(units) FILTER (WHERE module = 'new basket') newpart
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_pool
|
rlarp.osm_pool
|
||||||
@ -92,7 +92,7 @@ target AS (select target_vol vincr, target_prc pincr)
|
|||||||
-----------------additional params-------------------
|
-----------------additional params-------------------
|
||||||
AND CASE (SELECT flag FROM flagv)
|
AND CASE (SELECT flag FROM flagv)
|
||||||
WHEN 'scale all' THEN true
|
WHEN 'scale all' THEN true
|
||||||
WHEN 'scale copy' THEN iter = 'copy'
|
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
|
||||||
WHEN 'scale new part' THEN module = 'new basket'
|
WHEN 'scale new part' THEN module = 'new basket'
|
||||||
END
|
END
|
||||||
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
|
||||||
|
@ -95,12 +95,12 @@ GROUP BY
|
|||||||
order_month
|
order_month
|
||||||
,seq
|
,seq
|
||||||
,SUM(units) FILTER (WHERE order_season = 2021) "2021 qty"
|
,SUM(units) FILTER (WHERE order_season = 2021) "2021 qty"
|
||||||
,SUM(units) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship')) "2022 base qty"
|
,SUM(units) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship','plan')) "2022 base qty"
|
||||||
,SUM(units) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship')) "2022 adj qty"
|
,SUM(units) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship','diff','pf')) "2022 adj qty"
|
||||||
,SUM(units) FILTER (WHERE order_season = 2022) "2022 tot qty"
|
,SUM(units) FILTER (WHERE order_season = 2022) "2022 tot qty"
|
||||||
,SUM(value_usd) FILTER (WHERE order_season = 2021) "2021 value_usd"
|
,SUM(value_usd) FILTER (WHERE order_season = 2021) "2021 value_usd"
|
||||||
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship')) "2022 base value_usd"
|
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship','plan')) "2022 base value_usd"
|
||||||
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship')) "2022 adj value_usd"
|
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship','diff','pf')) "2022 adj value_usd"
|
||||||
,SUM(value_usd) FILTER (WHERE order_season = 2022) "2022 tot value_usd"
|
,SUM(value_usd) FILTER (WHERE order_season = 2022) "2022 tot value_usd"
|
||||||
FROM
|
FROM
|
||||||
months
|
months
|
||||||
|
Loading…
Reference in New Issue
Block a user