2020-10-27 23:44:14 -04:00
|
|
|
--a baseline period will have to be identified
|
2020-10-23 00:16:55 -04:00
|
|
|
WITH
|
|
|
|
baseline AS (
|
|
|
|
-----------------------copy YTD sales---------------------------------------------------------------------
|
|
|
|
SELECT
|
2020-10-27 23:47:21 -04:00
|
|
|
[nonversion]
|
2020-10-27 23:12:10 -04:00
|
|
|
,'baseline' "version"
|
2020-10-23 00:16:55 -04:00
|
|
|
,'actuals' iter
|
|
|
|
FROM
|
|
|
|
rlarp.osm_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....
|
2020-10-27 23:44:14 -04:00
|
|
|
[target_odate] BETWEEN [target_odate_from] AND [target_odate_to]
|
2020-10-23 00:16:55 -04:00
|
|
|
--...or any open orders currently booked before cutoff....
|
2020-10-27 23:44:14 -04:00
|
|
|
OR ([status_flag] IN ([status_list]) and [target_date] <= [target_date_from])
|
2020-10-23 00:16:55 -04:00
|
|
|
--...or anything that shipped in that period
|
2020-10-27 23:44:14 -04:00
|
|
|
OR ([target_sdate] BETWEEN [target_sdate_from] AND [target_sdate_to])
|
2020-10-23 00:16:55 -04:00
|
|
|
)
|
2020-10-27 23:44:14 -04:00
|
|
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
2020-10-23 00:16:55 -04:00
|
|
|
UNION ALL
|
|
|
|
---------option 1: fill in the rest of the year, with the prior years sales-sales----------------------------
|
|
|
|
SELECT
|
2020-10-27 23:47:21 -04:00
|
|
|
[nonversion]
|
2020-10-27 23:12:10 -04:00
|
|
|
,'baseline' "version"
|
|
|
|
,'plug' iter
|
2020-10-23 00:16:55 -04:00
|
|
|
FROM
|
|
|
|
rlarp.osm_dev o
|
|
|
|
LEFT OUTER JOIN gld ON
|
|
|
|
gld.fspr = o.fspr
|
|
|
|
LEFT OUTER JOIN gld ss ON
|
|
|
|
greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' BETWEEN ss.sdat AND ss.edat
|
|
|
|
WHERE
|
2020-10-27 23:44:14 -04:00
|
|
|
[target_odate] BETWEEN [target_odate_plug_from] AND [target_odate_plug_to]
|
|
|
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
2020-10-23 00:16:55 -04:00
|
|
|
UNION ALL
|
|
|
|
--------option 2: fill in the remainder of the current year with current forecase-----------------------------
|
|
|
|
SELECT
|
2020-10-27 23:47:21 -04:00
|
|
|
[nonversion]
|
2020-10-27 23:12:10 -04:00
|
|
|
,'baseline' "version"
|
|
|
|
,'plug' iter
|
2020-10-23 00:16:55 -04:00
|
|
|
FROM
|
|
|
|
rlarp.osmp_dev o
|
|
|
|
LEFT OUTER JOIN gld ON
|
|
|
|
gld.fspr = o.fspr
|
|
|
|
LEFT OUTER JOIN gld ss ON
|
|
|
|
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
|
|
|
|
WHERE
|
2020-10-27 23:44:14 -04:00
|
|
|
[target_odate] BETWEEN [target_odate_plug_from] AND [target_odate_plug_to]
|
|
|
|
AND false
|
2020-10-23 00:16:55 -04:00
|
|
|
)
|
|
|
|
-------------------copy the baseline just generated and increment all the dates by one year------------------------------------
|
|
|
|
,incr AS (
|
2020-10-27 23:47:21 -04:00
|
|
|
SELECT
|
|
|
|
[nondate_columns]
|
2020-10-23 00:16:55 -04:00
|
|
|
,o.dcodat + interval '1 year' --incremented
|
|
|
|
,o.ddqdat + interval '1 year' --incremented
|
|
|
|
,o.dhidat + interval '1 year' --incremented
|
|
|
|
,o.odate + interval '1 year' --incremented
|
|
|
|
,o.oseas + 1 --incremented
|
|
|
|
,o.rdate + interval '1 year' --incremented
|
|
|
|
,o.rseas + 1 --incremented
|
|
|
|
,o.sdate + interval '1 year' --incremented
|
|
|
|
,o.sseas + 1 --incremented
|
2020-10-27 23:12:10 -04:00
|
|
|
,'baseline' "version"
|
2020-10-23 00:16:55 -04:00
|
|
|
,'copy' iter
|
|
|
|
FROM
|
|
|
|
baseline o
|
|
|
|
LEFT OUTER JOIN gld ON
|
|
|
|
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
|
|
|
|
WHERE
|
|
|
|
o.odate + interval '1 year' >= '2020-06-01'
|
|
|
|
)
|
|
|
|
-------------insert the baseline actuals + x months of forecast in addtion to 12 months of forecase----------------------------
|
|
|
|
INSERT INTO rlarp.osmf_dev
|
|
|
|
SELECT * FROM incr
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM baseline;
|
|
|
|
|
|
|
|
---identify short ships: causes disconnect with actual sales-------------------------------------------------------------------
|
|
|
|
--UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER';
|
|
|
|
|
|
|
|
---identify goofy ship dates: causes disconnect with sales when splicing in a forecast that has this problem-------------------
|
|
|
|
--UPDATE rlarp.osmfs SET iter = 'bad date' WHERE adj_shipdate < adj_orderdate;
|