diff --git a/build/build_stage.sql b/build/build_stage.sql index f14d8c3..fc3a9cd 100644 --- a/build/build_stage.sql +++ b/build/build_stage.sql @@ -4,14 +4,23 @@ BEGIN; TRUNCATE TABLE rlarp.osmf_dev; DROP TABLE IF EXISTS tdr; + CREATE TEMP TABLE tdr AS ( SELECT - DATERANGE('2020-06-01','2021-06-01','[)') drange - ,DATERANGE(('2020-06-01'::date + '1 year'::interval)::date,('2021-06-01'::date + '1 year'::interval)::date,'[)') repl - ,'1 year'::interval AS incr + -----------actuals--------------------------------------------------- + DATERANGE('2020-06-01','2021-07-01','[)') adrange + -----------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 * from tdr + WITH gld AS ( SELECT @@ -158,11 +167,11 @@ gld AS ( WHERE ( --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 (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 (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 calc_status <> 'CANCELED' @@ -192,6 +201,7 @@ gld AS ( ,greatest(least(o.sdate,gld.edat),gld.sdat) ,ss.ssyr ) +--SELECT * FROM baseline ,incr AS ( SELECT o."ddord#" @@ -202,14 +212,14 @@ SELECT ,o."dilin#" ,o.quoten ,o.quotel - ,o.dcodat + interval '1 year' dcodat --incremented - ,o.ddqdat + interval '1 year' ddqdat --incremented + ,o.dcodat + (SELECT aincr FROM tdr) dcodat --incremented + ,o.ddqdat + (SELECT aincr FROM tdr) ddqdat --incremented ,o.dcmdat ,o.fesdat - ,o.dhidat + interval '1 year' dhidat --incremented + ,o.dhidat + (SELECT aincr FROM tdr) dhidat --incremented ,o.fesind ,o.dhpost - ,gld.fspr --incremented + ,sg.fspr --incremented ,o.ddqtoi ,o.ddqtsi ,o.fgqshp @@ -289,21 +299,28 @@ SELECT ,o.fb_cst_loc_fut ,o.calc_status ,o.flag - ,(o.odate + interval '1 year')::date odate --incremented - ,o.oseas + 1 oseas --incremented - ,(o.rdate + interval '1 year')::date rdate --incremented - ,o.rseas + 1 rseas --incremented - ,(o.sdate + interval '1 year')::date sdate --incremented - ,o.sseas + 1 sseas --incremented + ,(o.odate + (SELECT aincr FROM tdr))::date odate --incremented + ,og.ssyr oseas --incremented + ,(o.rdate + (SELECT aincr FROM tdr))::date rdate --incremented + ,rg.ssyr rseas --incremented + ,(o.sdate + (SELECT aincr FROM tdr))::date sdate --incremented + ,sg.ssyr sseas --incremented ,'b22' "version" ,'copy' iter FROM baseline o - LEFT OUTER JOIN gld ON - o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat + -----join to date tables based on revised dating-------------- + 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 - 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 ( SELECT * FROM incr UNION ALL @@ -419,14 +436,14 @@ SELECT * FROM baseline ,o.version ,o.iter FROM - rlarp.osmfs_dev o + rlarp.osmf_dev o CROSS JOIN tdr WHERE --collect all the rows in the base period and --the destination slot for the new rows ( - o.odate <@ tdr.drange - OR o.odate <@ tdr.repl + o.odate <@ tdr.adrange + OR o.odate <@ tdr.arepl ) ---only merge with targeted iterations AND tdr.iter ? o.iter @@ -579,8 +596,10 @@ SELECT * FROM baseline WHERE --collect all the rows in the base period and --the destination slot for the new rows - o.odate <@ (SELECT tdr.drange FROM tdr) - OR o.odate <@ (SELECT tdr.repl FROM tdr) + o.odate <@ (SELECT tdr.adrange 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 o.dhidat ,o.fspr @@ -607,9 +626,9 @@ SELECT * FROM baseline ,o.sseas --,o.version --,o.iter - HAVING - round(sum(o.fb_qty),2) <> 0 - OR round(sum(o.fb_val_loc),2) <> 0 + --HAVING + -- round(sum(o.fb_qty),2) <> 0 + -- OR round(sum(o.fb_val_loc),2) <> 0 --OR round(sum(o.fb_cst_loc),2) <> 0 ) INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff; @@ -648,8 +667,6 @@ WHERE COMMIT; -END - ---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- --UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; diff --git a/build/rebuild_forecast.sh b/build/rebuild_forecast.sh index 3bb1b33..1164536 100644 --- a/build/rebuild_forecast.sh +++ b/build/rebuild_forecast.sh @@ -1,5 +1,6 @@ -$PG -f ./build_stage.sql -$PG -f ./snap_itemm.sql -$PG -f ./snap_cost_current.sql -$PG -f ./snap_customer.sql -$PG -f ./build_pool.sql +#!/bin/bash +$PG -f ./build_stage.sql; +$PG -f ./snap_itemm.sql; +$PG -f ./snap_cost_current.sql; +$PG -f ./snap_customer.sql; +$PG -f ./build_pool.sql; diff --git a/build/tots.sql b/build/tots.sql index 5352f7f..b7c3b07 100644 --- a/build/tots.sql +++ b/build/tots.sql @@ -5,7 +5,7 @@ SELECT iter, sum(fb_val_loc) value_loc FROM - rlarp.osmf_stage o + rlarp.osmf_dev o GROUP BY 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'),