work on 1+11 forecast

This commit is contained in:
Paul Trowbridge 2021-07-06 09:33:58 -04:00
parent 8373b5753a
commit f8a96d6df2
3 changed files with 53 additions and 35 deletions

View File

@ -4,14 +4,23 @@ BEGIN;
TRUNCATE TABLE rlarp.osmf_dev; TRUNCATE TABLE rlarp.osmf_dev;
DROP TABLE IF EXISTS tdr; DROP TABLE IF EXISTS tdr;
CREATE TEMP TABLE tdr AS ( CREATE TEMP TABLE tdr AS (
SELECT SELECT
DATERANGE('2020-06-01','2021-06-01','[)') drange -----------actuals---------------------------------------------------
,DATERANGE(('2020-06-01'::date + '1 year'::interval)::date,('2021-06-01'::date + '1 year'::interval)::date,'[)') repl DATERANGE('2020-06-01','2021-07-01','[)') adrange
,'1 year'::interval AS incr -----------actuals to target for incremnt----------------------------
,DATERANGE('2021-08-01','2022-06-01','[)') arepl
,'0 year'::interval AS aincr
-----------budget to target for increment----------------------------
,DATERANGE('2021-07-01','2022-06-01','[)') prepl
,'0 year'::interval AS pincr
-----------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
); );
--select * from tdr
WITH WITH
gld AS ( gld AS (
SELECT SELECT
@ -158,11 +167,11 @@ gld AS (
WHERE WHERE
( (
--base period orders booked.... --base period orders booked....
o.odate <@ (SELECT drange FROM tdr) o.odate <@ (SELECT adrange FROM tdr)
--...or any open orders currently booked before cutoff.... --...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < (SELECT UPPER(drange) FROM tdr)) OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(adrange) FROM tdr))
--...or anything that shipped in that period --...or anything that shipped in that period
OR (o.sdate <@ (SELECT drange FROM tdr) AND sseas IS NOT NULL) OR (o.sdate <@ (SELECT adrange FROM tdr) AND sseas IS NOT NULL)
) )
AND fs_line = '41010' AND fs_line = '41010'
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
@ -192,6 +201,7 @@ gld AS (
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
) )
--SELECT * FROM baseline
,incr AS ( ,incr AS (
SELECT SELECT
o."ddord#" o."ddord#"
@ -202,14 +212,14 @@ SELECT
,o."dilin#" ,o."dilin#"
,o.quoten ,o.quoten
,o.quotel ,o.quotel
,o.dcodat + interval '1 year' dcodat --incremented ,o.dcodat + (SELECT aincr FROM tdr) dcodat --incremented
,o.ddqdat + interval '1 year' ddqdat --incremented ,o.ddqdat + (SELECT aincr FROM tdr) ddqdat --incremented
,o.dcmdat ,o.dcmdat
,o.fesdat ,o.fesdat
,o.dhidat + interval '1 year' dhidat --incremented ,o.dhidat + (SELECT aincr FROM tdr) dhidat --incremented
,o.fesind ,o.fesind
,o.dhpost ,o.dhpost
,gld.fspr --incremented ,sg.fspr --incremented
,o.ddqtoi ,o.ddqtoi
,o.ddqtsi ,o.ddqtsi
,o.fgqshp ,o.fgqshp
@ -289,21 +299,28 @@ SELECT
,o.fb_cst_loc_fut ,o.fb_cst_loc_fut
,o.calc_status ,o.calc_status
,o.flag ,o.flag
,(o.odate + interval '1 year')::date odate --incremented ,(o.odate + (SELECT aincr FROM tdr))::date odate --incremented
,o.oseas + 1 oseas --incremented ,og.ssyr oseas --incremented
,(o.rdate + interval '1 year')::date rdate --incremented ,(o.rdate + (SELECT aincr FROM tdr))::date rdate --incremented
,o.rseas + 1 rseas --incremented ,rg.ssyr rseas --incremented
,(o.sdate + interval '1 year')::date sdate --incremented ,(o.sdate + (SELECT aincr FROM tdr))::date sdate --incremented
,o.sseas + 1 sseas --incremented ,sg.ssyr sseas --incremented
,'b22' "version" ,'b22' "version"
,'copy' iter ,'copy' iter
FROM FROM
baseline o baseline o
LEFT OUTER JOIN gld ON -----join to date tables based on revised dating--------------
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat LEFT OUTER JOIN rlarp.gld sg ON
sg.drange @> (o.sdate + (SELECT aincr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld og ON
og.drange @> (o.odate + (SELECT aincr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld rg ON
rg.drange @> (o.odate + (SELECT aincr FROM tdr))::date
WHERE WHERE
o.odate + interval '1 year' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr) -------only return orders where the forecast orderdate is in the target forecast range-----------
(o.odate + (SELECT aincr FROM tdr)) >= (SELECT LOWER(arepl) + aincr FROM tdr)
) )
--SELECT count(*) FROM incr
,stage AS ( ,stage AS (
SELECT * FROM incr SELECT * FROM incr
UNION ALL UNION ALL
@ -419,14 +436,14 @@ SELECT * FROM baseline
,o.version ,o.version
,o.iter ,o.iter
FROM FROM
rlarp.osmfs_dev o rlarp.osmf_dev o
CROSS JOIN tdr CROSS JOIN tdr
WHERE WHERE
--collect all the rows in the base period and --collect all the rows in the base period and
--the destination slot for the new rows --the destination slot for the new rows
( (
o.odate <@ tdr.drange o.odate <@ tdr.adrange
OR o.odate <@ tdr.repl OR o.odate <@ tdr.arepl
) )
---only merge with targeted iterations ---only merge with targeted iterations
AND tdr.iter ? o.iter AND tdr.iter ? o.iter
@ -579,8 +596,10 @@ SELECT * FROM baseline
WHERE WHERE
--collect all the rows in the base period and --collect all the rows in the base period and
--the destination slot for the new rows --the destination slot for the new rows
o.odate <@ (SELECT tdr.drange FROM tdr) o.odate <@ (SELECT tdr.adrange FROM tdr)
OR o.odate <@ (SELECT tdr.repl FROM tdr) OR o.sdate <@ (SELECT tdr.adrange FROM tdr)
OR o.odate <@ (SELECT tdr.arepl FROM tdr)
OR o.sdate <@ (SELECT tdr.arepl FROM tdr)
GROUP BY GROUP BY
o.dhidat o.dhidat
,o.fspr ,o.fspr
@ -607,9 +626,9 @@ SELECT * FROM baseline
,o.sseas ,o.sseas
--,o.version --,o.version
--,o.iter --,o.iter
HAVING --HAVING
round(sum(o.fb_qty),2) <> 0 -- round(sum(o.fb_qty),2) <> 0
OR round(sum(o.fb_val_loc),2) <> 0 -- OR round(sum(o.fb_val_loc),2) <> 0
--OR round(sum(o.fb_cst_loc),2) <> 0 --OR round(sum(o.fb_cst_loc),2) <> 0
) )
INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff; INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff;
@ -648,8 +667,6 @@ WHERE
COMMIT; COMMIT;
END
---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- ---identify short ships: causes disconnect with actual sales-------------------------------------------------------------------
--UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; --UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER';

View File

@ -1,5 +1,6 @@
$PG -f ./build_stage.sql #!/bin/bash
$PG -f ./snap_itemm.sql $PG -f ./build_stage.sql;
$PG -f ./snap_cost_current.sql $PG -f ./snap_itemm.sql;
$PG -f ./snap_customer.sql $PG -f ./snap_cost_current.sql;
$PG -f ./build_pool.sql $PG -f ./snap_customer.sql;
$PG -f ./build_pool.sql;

View File

@ -5,7 +5,7 @@ SELECT
iter, iter,
sum(fb_val_loc) value_loc sum(fb_val_loc) value_loc
FROM FROM
rlarp.osmf_stage o rlarp.osmf_dev o
GROUP BY GROUP BY
oseas, oseas,
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'), 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'),