rename files to reflect intent

This commit is contained in:
Paul Trowbridge 2021-07-12 11:32:52 -04:00
parent c5b9d39b5a
commit 391e6bf09a
2 changed files with 446 additions and 446 deletions

View File

@ -1,41 +1,17 @@
/*
baseline - selections from actual, plan, and forecast
incr - increment selection from baseline
incoming - baseline & incr stacked
negative - apply negative values to existing baseline to create a diff
diff - stack incoming and the negative
collapse_diff - group the diff to create single diff rows per item
*/
BEGIN; BEGIN;
--\timing --\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; --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; 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
-----------actuals into baseline------------------------------------- DATERANGE('2020-06-01','2021-06-01','[)') drange
DATERANGE('2020-06-01','2021-07-07','[]') arange ,DATERANGE(('2020-06-01'::date + '1 year'::interval)::date,('2021-06-01'::date + '1 year'::interval)::date,'[)') repl
-----------plan into baseline---------------------------------------- ,'1 year'::interval AS incr
,DATERANGE('2021-07-08','2022-06-01','[)') prange
-----------forecast into baseline------------------------------------
,DATERANGE('2022-06-01','2022-06-01','[)') frange
-----------baseline selection for increment--------------------------
,DATERANGE('2021-07-08','2021-07-08','[)') selection
-----------selection increment size----------------------------------
,'0 year'::interval AS incr
-----------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---------------------------------
,DATERANGE('2000-06-01','2021-07-08') overlap
); );
--select * from tdr
WITH WITH
gld AS ( gld AS (
SELECT SELECT
@ -62,9 +38,6 @@ gld AS (
) )
--SELECT * FROM gld --SELECT * FROM gld
,baseline AS ( ,baseline AS (
-------------------------------------------------------------------------------------
-----------------------------------actuals-------------------------------------------
-------------------------------------------------------------------------------------
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -172,8 +145,8 @@ gld AS (
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas ,ss.ssyr sseas
,o."version" ,'actuals' "version"
,o.iter ,'actuals' iter
FROM FROM
rlarp.osm_dev o rlarp.osm_dev o
--snap the ship dates of the historic fiscal period --snap the ship dates of the historic fiscal period
@ -185,17 +158,14 @@ gld AS (
WHERE WHERE
( (
--base period orders booked.... --base period orders booked....
o.odate <@ (SELECT arange FROM tdr) o.odate <@ (SELECT drange 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(arange) 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 anything that shipped in that period
OR (o.sdate <@ (SELECT arange 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' AND fs_line = '41010'
---exclude canceled orders
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
---exclude short ships
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER') AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
---exclude integrated quotes---- ---exclude integrated quotes----
AND version = 'ACTUALS' AND version = 'ACTUALS'
@ -221,334 +191,7 @@ gld AS (
,o.rseas ,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
,o."version"
,o.iter
UNION ALL
-------------------------------------------------------------------------------------
-----------------------------------plan----------------------------------------------
-------------------------------------------------------------------------------------
SELECT
-----------documents-------------
null::int "ddord#"
,null::int "dditm#"
,null::int "fgbol#"
,null::int "fgent#"
,null::int "diinv#"
,null::int "dilin#"
,null::int quoten
,null::int quotel
----------dates/status------------------
,o.odate dcodat
,o.rdate ddqdat
,null::date dcmdat
,null::date fesdat
,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat
,null::text fesind
,null::text dhpost
---for forecasting purposes there are no open orders, populate fspr----
,ss.fspr
-----------measures--------------------
,null::numeric ddqtoi
,null::numeric ddqtsi
,null::numeric fgqshp
,null::numeric diqtsh
,null::numeric diext
,null::numeric ditdis
,null::jsonb discj
,null::text dhincr
,o.plnt
,COALESCE(o.promo,'') promo
,null::text return_reas
,o.terms
,null::text custpo
,null::text remit_to
,null::text bill_class
,o.bill_cust
,null::text bill_rep
,null::text bill_terr
,null::text ship_class
,o.ship_cust
,null::text ship_rep
,null::text ship_terr
,o.dsm
,null::text account
,null::text shipgrp
,null::text geo
,null::text chan
,null::text chansub
,null::text orig_ctry
,null::text orig_prov
,null::text orig_post
,null::text bill_ctry
,null::text bill_prov
,null::text bill_post
,null::text dest_ctry
,null::text dest_prov
,null::text dest_post
,o.part
,null::text styc
,null::text colc
,null::text colgrp
,null::text coltier
,null::text colstat
,null::text sizc
,null::text pckg
,null::text kit
,null::text brnd
,null::text majg
,null::text ming
,null::text majs
,null::text mins
,null::text gldco
,null::text gldc
,null::text glec
,null::text harm
,null::text clss
,null::text brand
,null::text assc
,null::text ddunit
,null::text unti
,null::numeric lbs
,null::numeric plt
,null::text plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,sum(o.fb_qty) fb_qty
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_dis) fb_val_loc_dis
,sum(o.fb_val_loc_qt) fb_val_loc_qt
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,sum(o.fb_val_loc_tar) fb_val_loc_tar
,sum(o.fb_cst_loc) fb_cst_loc
,sum(o.fb_cst_loc_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
-----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas
,o."version"
,o.iter
FROM
rlarp.osmp_dev o
--snap the ship dates of the historic fiscal period
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
--get the shipping season for open orders based on the snapped date
LEFT OUTER JOIN gld ss ON
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
--base period orders booked....
o.odate <@ (SELECT prange FROM tdr)
--...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(prange) FROM tdr))
--...or anything that shipped in that period
--OR (o.sdate <@ (SELECT prange 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 = 'b22'
GROUP BY
ss.fspr
,o.plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,o.ship_cust
,o.dsm
,o.part
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr
,o."version"
,o.iter
UNION ALL
-------------------------------------------------------------------------------------
-----------------------------------forecast------------------------------------------
-------------------------------------------------------------------------------------
SELECT
-----------documents-------------
null::int "ddord#"
,null::int "dditm#"
,null::int "fgbol#"
,null::int "fgent#"
,null::int "diinv#"
,null::int "dilin#"
,null::int quoten
,null::int quotel
----------dates/status------------------
,o.odate dcodat
,o.rdate ddqdat
,null::date dcmdat
,null::date fesdat
,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat
,null::text fesind
,null::text dhpost
---for forecasting purposes there are no open orders, populate fspr----
,ss.fspr
-----------measures--------------------
,null::numeric ddqtoi
,null::numeric ddqtsi
,null::numeric fgqshp
,null::numeric diqtsh
,null::numeric diext
,null::numeric ditdis
,null::jsonb discj
,null::text dhincr
,o.plnt
,COALESCE(o.promo,'') promo
,null::text return_reas
,o.terms
,null::text custpo
,null::text remit_to
,null::text bill_class
,o.bill_cust
,null::text bill_rep
,null::text bill_terr
,null::text ship_class
,o.ship_cust
,null::text ship_rep
,null::text ship_terr
,o.dsm
,null::text account
,null::text shipgrp
,null::text geo
,null::text chan
,null::text chansub
,null::text orig_ctry
,null::text orig_prov
,null::text orig_post
,null::text bill_ctry
,null::text bill_prov
,null::text bill_post
,null::text dest_ctry
,null::text dest_prov
,null::text dest_post
,o.part
,null::text styc
,null::text colc
,null::text colgrp
,null::text coltier
,null::text colstat
,null::text sizc
,null::text pckg
,null::text kit
,null::text brnd
,null::text majg
,null::text ming
,null::text majs
,null::text mins
,null::text gldco
,null::text gldc
,null::text glec
,null::text harm
,null::text clss
,null::text brand
,null::text assc
,null::text ddunit
,null::text unti
,null::numeric lbs
,null::numeric plt
,null::text plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,sum(o.fb_qty) fb_qty
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_dis) fb_val_loc_dis
,sum(o.fb_val_loc_qt) fb_val_loc_qt
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,sum(o.fb_val_loc_tar) fb_val_loc_tar
,sum(o.fb_cst_loc) fb_cst_loc
,sum(o.fb_cst_loc_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
-----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas
,o."version"
,o.iter
FROM
rlarp.osmf_dev o
--snap the ship dates of the historic fiscal period
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
--get the shipping season for open orders based on the snapped date
LEFT OUTER JOIN gld ss ON
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
--base period orders booked....
o.odate <@ (SELECT frange FROM tdr)
--...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(prange) FROM tdr))
--...or anything that shipped in that period
--OR (o.sdate <@ (SELECT prange 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'
GROUP BY
ss.fspr
,o.plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,o.ship_cust
,o.dsm
,o.part
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr
,o."version"
,o.iter
)
--SELECT * FROM baseline where version = 'b22'
,incr AS ( ,incr AS (
SELECT SELECT
o."ddord#" o."ddord#"
@ -559,14 +202,14 @@ SELECT
,o."dilin#" ,o."dilin#"
,o.quoten ,o.quoten
,o.quotel ,o.quotel
,o.dcodat + (SELECT incr FROM tdr) dcodat --incremented ,o.dcodat + interval '1 year' dcodat --incremented
,o.ddqdat + (SELECT incr FROM tdr) ddqdat --incremented ,o.ddqdat + interval '1 year' ddqdat --incremented
,o.dcmdat ,o.dcmdat
,o.fesdat ,o.fesdat
,o.dhidat + (SELECT incr FROM tdr) dhidat --incremented ,o.dhidat + interval '1 year' dhidat --incremented
,o.fesind ,o.fesind
,o.dhpost ,o.dhpost
,sg.fspr --incremented ,gld.fspr --incremented
,o.ddqtoi ,o.ddqtoi
,o.ddqtsi ,o.ddqtsi
,o.fgqshp ,o.fgqshp
@ -646,35 +289,27 @@ SELECT
,o.fb_cst_loc_fut ,o.fb_cst_loc_fut
,o.calc_status ,o.calc_status
,o.flag ,o.flag
,(o.odate + (SELECT incr FROM tdr))::date odate --incremented ,(o.odate + interval '1 year')::date odate --incremented
,og.ssyr oseas --incremented ,o.oseas + 1 oseas --incremented
,(o.rdate + (SELECT incr FROM tdr))::date rdate --incremented ,(o.rdate + interval '1 year')::date rdate --incremented
,rg.ssyr rseas --incremented ,o.rseas + 1 rseas --incremented
,(o.sdate + (SELECT incr FROM tdr))::date sdate --incremented ,(o.sdate + interval '1 year')::date sdate --incremented
,sg.ssyr sseas --incremented ,o.sseas + 1 sseas --incremented
,'1+11' "version" ,'b22' "version"
,'copy' iter ,'copy' iter
FROM FROM
baseline o baseline o
-----join to date tables based on revised dating-------------- LEFT OUTER JOIN gld ON
LEFT OUTER JOIN rlarp.gld sg ON o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
sg.drange @> (o.sdate + (SELECT incr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld og ON
og.drange @> (o.odate + (SELECT incr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld rg ON
rg.drange @> (o.odate + (SELECT incr FROM tdr))::date
WHERE WHERE
-------only return orders where the forecast orderdate is in the target forecast range----------- o.odate + interval '1 year' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr)
(o.odate + (SELECT incr FROM tdr))::date <@ (SELECT selection FROM tdr) OR
(o.sdate + (SELECT incr FROM tdr))::date <@ (SELECT selection FROM tdr)
) )
--SELECT count(*) FROM baseline ,stage AS (
,incoming AS (
SELECT * FROM incr SELECT * FROM incr
UNION ALL UNION ALL
SELECT * FROM baseline SELECT * FROM baseline
) )
,negative AS ( ,remove AS (
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -784,14 +419,14 @@ SELECT * FROM baseline
,o.version ,o.version
,o.iter ,o.iter
FROM FROM
rlarp.osmf_dev o rlarp.osmfs_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.arange o.odate <@ tdr.drange
OR o.odate <@ tdr.selection OR o.odate <@ tdr.repl
) )
---only merge with targeted iterations ---only merge with targeted iterations
AND tdr.iter ? o.iter AND tdr.iter ? o.iter
@ -824,12 +459,12 @@ SELECT * FROM baseline
,o.version ,o.version
,o.iter ,o.iter
) )
,diff AS ( ,stack AS (
SELECT * FROM incoming SELECT * FROM stage
UNION ALL UNION ALL
SELECT * FROM negative SELECT * FROM remove
) )
,collapse_diff AS ( ,merge_diff AS (
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -940,15 +575,12 @@ SELECT * FROM baseline
,'actuals' AS version ,'actuals' AS version
,'copy' iter ,'copy' iter
FROM FROM
diff o stack o
WHERE WHERE
true
--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.arange FROM tdr) o.odate <@ (SELECT tdr.drange FROM tdr)
--OR o.sdate <@ (SELECT tdr.arange FROM tdr) OR o.odate <@ (SELECT tdr.repl FROM tdr)
--OR o.odate <@ (SELECT tdr.selection FROM tdr)
--OR o.sdate <@ (SELECT tdr.selection FROM tdr)
GROUP BY GROUP BY
o.dhidat o.dhidat
,o.fspr ,o.fspr
@ -975,12 +607,12 @@ 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 collapse_diff; INSERT INTO rlarp.osmf_dev SELECT * FROM merge_diff;
UPDATE UPDATE
@ -1016,6 +648,8 @@ 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,17 +1,41 @@
/*
baseline - selections from actual, plan, and forecast
incr - increment selection from baseline
incoming - baseline & incr stacked
negative - apply negative values to existing baseline to create a diff
diff - stack incoming and the negative
collapse_diff - group the diff to create single diff rows per item
*/
BEGIN; BEGIN;
--\timing --\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data; --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; 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 into baseline-------------------------------------
,DATERANGE(('2020-06-01'::date + '1 year'::interval)::date,('2021-06-01'::date + '1 year'::interval)::date,'[)') repl DATERANGE('2020-06-01','2021-07-07','[]') arange
,'1 year'::interval AS incr -----------plan into baseline----------------------------------------
,DATERANGE('2021-07-08','2022-06-01','[)') prange
-----------forecast into baseline------------------------------------
,DATERANGE('2022-06-01','2022-06-01','[)') frange
-----------baseline selection for increment--------------------------
,DATERANGE('2021-07-08','2021-07-08','[)') selection
-----------selection increment size----------------------------------
,'0 year'::interval AS incr
-----------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---------------------------------
,DATERANGE('2000-06-01','2021-07-08') overlap
); );
--select * from tdr
WITH WITH
gld AS ( gld AS (
SELECT SELECT
@ -38,6 +62,9 @@ gld AS (
) )
--SELECT * FROM gld --SELECT * FROM gld
,baseline AS ( ,baseline AS (
-------------------------------------------------------------------------------------
-----------------------------------actuals-------------------------------------------
-------------------------------------------------------------------------------------
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -145,8 +172,8 @@ gld AS (
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas ,ss.ssyr sseas
,'actuals' "version" ,o."version"
,'actuals' iter ,o.iter
FROM FROM
rlarp.osm_dev o rlarp.osm_dev o
--snap the ship dates of the historic fiscal period --snap the ship dates of the historic fiscal period
@ -158,14 +185,17 @@ gld AS (
WHERE WHERE
( (
--base period orders booked.... --base period orders booked....
o.odate <@ (SELECT drange FROM tdr) o.odate <@ (SELECT arange 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(arange) 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 arange FROM tdr) AND sseas IS NOT NULL)
) )
---exclude R&A's
AND fs_line = '41010' AND fs_line = '41010'
---exclude canceled orders
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
---exclude short ships
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER') AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
---exclude integrated quotes---- ---exclude integrated quotes----
AND version = 'ACTUALS' AND version = 'ACTUALS'
@ -191,7 +221,334 @@ gld AS (
,o.rseas ,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
,o."version"
,o.iter
UNION ALL
-------------------------------------------------------------------------------------
-----------------------------------plan----------------------------------------------
-------------------------------------------------------------------------------------
SELECT
-----------documents-------------
null::int "ddord#"
,null::int "dditm#"
,null::int "fgbol#"
,null::int "fgent#"
,null::int "diinv#"
,null::int "dilin#"
,null::int quoten
,null::int quotel
----------dates/status------------------
,o.odate dcodat
,o.rdate ddqdat
,null::date dcmdat
,null::date fesdat
,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat
,null::text fesind
,null::text dhpost
---for forecasting purposes there are no open orders, populate fspr----
,ss.fspr
-----------measures--------------------
,null::numeric ddqtoi
,null::numeric ddqtsi
,null::numeric fgqshp
,null::numeric diqtsh
,null::numeric diext
,null::numeric ditdis
,null::jsonb discj
,null::text dhincr
,o.plnt
,COALESCE(o.promo,'') promo
,null::text return_reas
,o.terms
,null::text custpo
,null::text remit_to
,null::text bill_class
,o.bill_cust
,null::text bill_rep
,null::text bill_terr
,null::text ship_class
,o.ship_cust
,null::text ship_rep
,null::text ship_terr
,o.dsm
,null::text account
,null::text shipgrp
,null::text geo
,null::text chan
,null::text chansub
,null::text orig_ctry
,null::text orig_prov
,null::text orig_post
,null::text bill_ctry
,null::text bill_prov
,null::text bill_post
,null::text dest_ctry
,null::text dest_prov
,null::text dest_post
,o.part
,null::text styc
,null::text colc
,null::text colgrp
,null::text coltier
,null::text colstat
,null::text sizc
,null::text pckg
,null::text kit
,null::text brnd
,null::text majg
,null::text ming
,null::text majs
,null::text mins
,null::text gldco
,null::text gldc
,null::text glec
,null::text harm
,null::text clss
,null::text brand
,null::text assc
,null::text ddunit
,null::text unti
,null::numeric lbs
,null::numeric plt
,null::text plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,sum(o.fb_qty) fb_qty
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_dis) fb_val_loc_dis
,sum(o.fb_val_loc_qt) fb_val_loc_qt
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,sum(o.fb_val_loc_tar) fb_val_loc_tar
,sum(o.fb_cst_loc) fb_cst_loc
,sum(o.fb_cst_loc_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
-----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas
,o."version"
,o.iter
FROM
rlarp.osmp_dev o
--snap the ship dates of the historic fiscal period
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
--get the shipping season for open orders based on the snapped date
LEFT OUTER JOIN gld ss ON
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
--base period orders booked....
o.odate <@ (SELECT prange FROM tdr)
--...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(prange) FROM tdr))
--...or anything that shipped in that period
--OR (o.sdate <@ (SELECT prange 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 = 'b22'
GROUP BY
ss.fspr
,o.plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,o.ship_cust
,o.dsm
,o.part
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr
,o."version"
,o.iter
UNION ALL
-------------------------------------------------------------------------------------
-----------------------------------forecast------------------------------------------
-------------------------------------------------------------------------------------
SELECT
-----------documents-------------
null::int "ddord#"
,null::int "dditm#"
,null::int "fgbol#"
,null::int "fgent#"
,null::int "diinv#"
,null::int "dilin#"
,null::int quoten
,null::int quotel
----------dates/status------------------
,o.odate dcodat
,o.rdate ddqdat
,null::date dcmdat
,null::date fesdat
,greatest(least(o.sdate,gld.edat),gld.sdat) dhidat
,null::text fesind
,null::text dhpost
---for forecasting purposes there are no open orders, populate fspr----
,ss.fspr
-----------measures--------------------
,null::numeric ddqtoi
,null::numeric ddqtsi
,null::numeric fgqshp
,null::numeric diqtsh
,null::numeric diext
,null::numeric ditdis
,null::jsonb discj
,null::text dhincr
,o.plnt
,COALESCE(o.promo,'') promo
,null::text return_reas
,o.terms
,null::text custpo
,null::text remit_to
,null::text bill_class
,o.bill_cust
,null::text bill_rep
,null::text bill_terr
,null::text ship_class
,o.ship_cust
,null::text ship_rep
,null::text ship_terr
,o.dsm
,null::text account
,null::text shipgrp
,null::text geo
,null::text chan
,null::text chansub
,null::text orig_ctry
,null::text orig_prov
,null::text orig_post
,null::text bill_ctry
,null::text bill_prov
,null::text bill_post
,null::text dest_ctry
,null::text dest_prov
,null::text dest_post
,o.part
,null::text styc
,null::text colc
,null::text colgrp
,null::text coltier
,null::text colstat
,null::text sizc
,null::text pckg
,null::text kit
,null::text brnd
,null::text majg
,null::text ming
,null::text majs
,null::text mins
,null::text gldco
,null::text gldc
,null::text glec
,null::text harm
,null::text clss
,null::text brand
,null::text assc
,null::text ddunit
,null::text unti
,null::numeric lbs
,null::numeric plt
,null::text plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,sum(o.fb_qty) fb_qty
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_dis) fb_val_loc_dis
,sum(o.fb_val_loc_qt) fb_val_loc_qt
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,sum(o.fb_val_loc_tar) fb_val_loc_tar
,sum(o.fb_cst_loc) fb_cst_loc
,sum(o.fb_cst_loc_cur) fb_cst_loc_cur
,sum(o.fb_cst_loc_fut) fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
-----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas
,o."version"
,o.iter
FROM
rlarp.osmf_dev o
--snap the ship dates of the historic fiscal period
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
--get the shipping season for open orders based on the snapped date
LEFT OUTER JOIN gld ss ON
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
--base period orders booked....
o.odate <@ (SELECT frange FROM tdr)
--...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(prange) FROM tdr))
--...or anything that shipped in that period
--OR (o.sdate <@ (SELECT prange 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'
GROUP BY
ss.fspr
,o.plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,o.ship_cust
,o.dsm
,o.part
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr
,o."version"
,o.iter
)
--SELECT * FROM baseline where version = 'b22'
,incr AS ( ,incr AS (
SELECT SELECT
o."ddord#" o."ddord#"
@ -202,14 +559,14 @@ SELECT
,o."dilin#" ,o."dilin#"
,o.quoten ,o.quoten
,o.quotel ,o.quotel
,o.dcodat + interval '1 year' dcodat --incremented ,o.dcodat + (SELECT incr FROM tdr) dcodat --incremented
,o.ddqdat + interval '1 year' ddqdat --incremented ,o.ddqdat + (SELECT incr FROM tdr) ddqdat --incremented
,o.dcmdat ,o.dcmdat
,o.fesdat ,o.fesdat
,o.dhidat + interval '1 year' dhidat --incremented ,o.dhidat + (SELECT incr 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,27 +646,35 @@ 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 incr 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 incr 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 incr FROM tdr))::date sdate --incremented
,o.sseas + 1 sseas --incremented ,sg.ssyr sseas --incremented
,'b22' "version" ,'1+11' "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 incr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld og ON
og.drange @> (o.odate + (SELECT incr FROM tdr))::date
LEFT OUTER JOIN rlarp.gld rg ON
rg.drange @> (o.odate + (SELECT incr 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 incr FROM tdr))::date <@ (SELECT selection FROM tdr) OR
(o.sdate + (SELECT incr FROM tdr))::date <@ (SELECT selection FROM tdr)
) )
,stage AS ( --SELECT count(*) FROM baseline
,incoming AS (
SELECT * FROM incr SELECT * FROM incr
UNION ALL UNION ALL
SELECT * FROM baseline SELECT * FROM baseline
) )
,remove AS ( ,negative AS (
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -419,14 +784,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.arange
OR o.odate <@ tdr.repl OR o.odate <@ tdr.selection
) )
---only merge with targeted iterations ---only merge with targeted iterations
AND tdr.iter ? o.iter AND tdr.iter ? o.iter
@ -459,12 +824,12 @@ SELECT * FROM baseline
,o.version ,o.version
,o.iter ,o.iter
) )
,stack AS ( ,diff AS (
SELECT * FROM stage SELECT * FROM incoming
UNION ALL UNION ALL
SELECT * FROM remove SELECT * FROM negative
) )
,merge_diff AS ( ,collapse_diff AS (
SELECT SELECT
-----------documents------------- -----------documents-------------
null::int "ddord#" null::int "ddord#"
@ -575,12 +940,15 @@ SELECT * FROM baseline
,'actuals' AS version ,'actuals' AS version
,'copy' iter ,'copy' iter
FROM FROM
stack o diff o
WHERE WHERE
true
--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.arange FROM tdr)
OR o.odate <@ (SELECT tdr.repl FROM tdr) --OR o.sdate <@ (SELECT tdr.arange FROM tdr)
--OR o.odate <@ (SELECT tdr.selection FROM tdr)
--OR o.sdate <@ (SELECT tdr.selection FROM tdr)
GROUP BY GROUP BY
o.dhidat o.dhidat
,o.fspr ,o.fspr
@ -607,12 +975,12 @@ 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 collapse_diff;
UPDATE UPDATE
@ -648,8 +1016,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';