Compare commits

..

No commits in common. "752aeffa061b6004fe8699afb8d29d05ec749b18" and "b0234c2f89b672e711c8d11b24c1eee2823d3ae6" have entirely different histories.

2 changed files with 12 additions and 27 deletions

View File

@ -21,10 +21,9 @@ tdr AS (
DATERANGE('2021-06-01','2022-06-01','[)') prange DATERANGE('2021-06-01','2022-06-01','[)') prange
-----------explicitly call out overlap period------------------------ -----------explicitly call out overlap period------------------------
-----------anythign in this period has to net out to match actuals--- -----------anythign in this period has to net out to match actuals---
,DATERANGE('2021-06-01','2021-07-31','[]') overlap ,DATERANGE('2021-06-01','2021-07-29','[]') overlap
,jsonb_build_array('plan','diff') iter ,jsonb_build_array('plan','diff') iter
) )
------actual orders according to whatever is in the forecast, has to be defined as 'plan' and 'diff'--------
,booked AS materialized( ,booked AS materialized(
SELECT SELECT
billto_group billto_group
@ -42,8 +41,6 @@ tdr AS (
,shipto_group ,shipto_group
,part_group ,part_group
) )
--SELECT * FROM booked
------------plan units according to the 'plan' iteration of the current forecast----------------------------
,planned AS ( ,planned AS (
SELECT SELECT
p.billto_group p.billto_group
@ -74,8 +71,6 @@ tdr AS (
,p.ship_date ,p.ship_date
,b.units ,b.units
) )
--SELECT * FROM planned LIMIT 100
-------flag rows by preparing a rolling total in this step------------------
,plan_ranked AS ( ,plan_ranked AS (
SELECT SELECT
p.billto_group p.billto_group
@ -93,8 +88,7 @@ tdr AS (
FROM FROM
planned p planned p
) )
--SELECT * FROM plan_ranked ---flag any rows where
---flag future budget where up to the extent that actuals are larger than budget, but not more------
,eval AS ( ,eval AS (
SELECT SELECT
p.billto_group p.billto_group
@ -109,20 +103,12 @@ tdr AS (
,p.net_units_tot ,p.net_units_tot
,p.net_units ,p.net_units
--,p.booked --,p.booked
-----total current units less plan total units is the excess ,plan_rolling <= (p.net_units_tot - p.plan_units_tot + plan_units) flag
-----if the current row contributes to the excess, flag it
-----the last row will likely pull out too much, it will need split probably
,(plan_rolling - plan_units) <= (p.net_units_tot - p.plan_units_tot) flag
,-(p.net_units_tot - p.plan_units_tot) max_possible_reduction
,-CASE
WHEN plan_rolling <= (p.net_units_tot - p.plan_units_tot) THEN plan_units
WHEN plan_rolling > (p.net_units_tot - p.plan_units_tot) AND (plan_rolling - plan_units) <= (p.net_units_tot - p.plan_units_tot) THEN (p.net_units_tot - p.plan_units_tot) - (plan_rolling - plan_units)
END qty
--,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 --,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 FROM
plan_ranked p plan_ranked p
) )
--SELECT * FROM eval LIMIT 10000 --select * from eval limit 10000
----------------create a log entry-------------------- ----------------create a log entry--------------------
,log AS ( ,log AS (
INSERT INTO INSERT INTO
@ -166,12 +152,11 @@ tdr AS (
,o.r_rate ,o.r_rate
,o.c_currency ,o.c_currency
,o.c_rate ,o.c_rate
-----only keep the percentage of the current row determined by the last step-------- ,round(-sum(o.units ),2) units
,round(-sum(o.units ),2)*(-e.qty/e.plan_units) units ,round(-sum(o.value_loc),2) value_loc
,round(-sum(o.value_loc),2)*(-e.qty/e.plan_units) value_loc ,round(-sum(o.value_usd),2) value_usd
,round(-sum(o.value_usd),2)*(-e.qty/e.plan_units) value_usd ,round(-sum(o.cost_loc ),2) cost_loc
,round(-sum(o.cost_loc ),2)*(-e.qty/e.plan_units) cost_loc ,round(-sum(o.cost_usd ),2) cost_usd
,round(-sum(o.cost_usd ),2)*(-e.qty/e.plan_units) cost_usd
,o.calc_status ,o.calc_status
,o.flag ,o.flag
,o.order_date ,o.order_date

View File

@ -16,19 +16,19 @@ 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-31','[]') arange DATERANGE('2020-06-01','2021-07-29','[]') arange
-----------plan into baseline---------------------------------------- -----------plan into baseline----------------------------------------
,DATERANGE('2020-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-31','2021-07-31','[)') selection ,DATERANGE('2021-07-29','2021-07-29','[)') 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----------------------------------
,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter
-----------existing baseline overlap--------------------------------- -----------existing baseline overlap---------------------------------
,DATERANGE('2000-06-01','2021-07-31') overlap ,DATERANGE('2000-06-01','2021-07-29') overlap
); );
--select * from tdr --select * from tdr