back to original stage file

This commit is contained in:
Paul Trowbridge 2021-07-09 16:18:01 -04:00
parent 7f8ed574c5
commit b9c6c64764

View File

@ -1,27 +1,17 @@
BEGIN;
--\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data;
--will not match actuals exactly due to short-ships
TRUNCATE TABLE rlarp.osmf_dev;
DROP TABLE IF EXISTS tdr;
CREATE TEMP TABLE tdr AS (
SELECT
-----------actuals---------------------------------------------------
DATERANGE('2020-06-01','2021-07-07','[]') adrange
-----------actuals to target for incremnt----------------------------
,DATERANGE('2022-06-01','2022-06-01','[)') arepl
,'0 year'::interval AS aincr
-----------budget to target for increment----------------------------
,DATERANGE('2021-07-08','2022-06-01','[)') prepl
,'0 year'::interval AS pincr
-----------iterations to merge with----------------------------------
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
,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter
);
--select * from tdr
WITH
gld AS (
SELECT
@ -168,17 +158,14 @@ gld AS (
WHERE
(
--base period orders booked....
o.odate <@ (SELECT adrange FROM tdr)
o.odate <@ (SELECT drange FROM tdr)
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(adrange) FROM tdr))
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < (SELECT UPPER(drange) FROM tdr))
--...or anything that shipped in that period
OR (o.sdate <@ (SELECT adrange FROM tdr) AND sseas IS NOT NULL)
OR (o.sdate <@ (SELECT drange FROM tdr) AND sseas IS NOT NULL)
)
---exclude R&A's
AND fs_line = '41010'
---exclude canceled orders
AND calc_status <> 'CANCELED'
---exclude short ships
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
---exclude integrated quotes----
AND version = 'ACTUALS'
@ -205,7 +192,6 @@ gld AS (
,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr
)
--SELECT * FROM baseline
,incr AS (
SELECT
o."ddord#"
@ -216,14 +202,14 @@ SELECT
,o."dilin#"
,o.quoten
,o.quotel
,o.dcodat + (SELECT aincr FROM tdr) dcodat --incremented
,o.ddqdat + (SELECT aincr FROM tdr) ddqdat --incremented
,o.dcodat + interval '1 year' dcodat --incremented
,o.ddqdat + interval '1 year' ddqdat --incremented
,o.dcmdat
,o.fesdat
,o.dhidat + (SELECT aincr FROM tdr) dhidat --incremented
,o.dhidat + interval '1 year' dhidat --incremented
,o.fesind
,o.dhpost
,sg.fspr --incremented
,gld.fspr --incremented
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
@ -303,28 +289,21 @@ SELECT
,o.fb_cst_loc_fut
,o.calc_status
,o.flag
,(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
,(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
,'b22' "version"
,'copy' iter
FROM
baseline o
-----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
LEFT OUTER JOIN gld ON
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
WHERE
-------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)
o.odate + interval '1 year' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr)
)
--SELECT count(*) FROM incr
,stage AS (
SELECT * FROM incr
UNION ALL
@ -440,14 +419,14 @@ SELECT * FROM baseline
,o.version
,o.iter
FROM
rlarp.osmf_dev o
rlarp.osmfs_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.adrange
OR o.odate <@ tdr.arepl
o.odate <@ tdr.drange
OR o.odate <@ tdr.repl
)
---only merge with targeted iterations
AND tdr.iter ? o.iter
@ -600,10 +579,8 @@ SELECT * FROM baseline
WHERE
--collect all the rows in the base period and
--the destination slot for the new rows
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)
o.odate <@ (SELECT tdr.drange FROM tdr)
OR o.odate <@ (SELECT tdr.repl FROM tdr)
GROUP BY
o.dhidat
,o.fspr
@ -630,9 +607,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;
@ -671,6 +648,8 @@ 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';