forecast_api/build/build_rolling.sql

1057 lines
31 KiB
MySQL
Raw Permalink Normal View History

2021-07-12 11:32:52 -04:00
/*
2021-07-12 14:31:22 -04:00
establish an existing forecast to operate on top of
update to refrect actuals with a diff as an iteration
create and additional to remove pull-forward as actuals flow in
2021-07-12 11:32:52 -04:00
*/
2021-04-28 17:34:44 -04:00
BEGIN;
--\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data;
2021-07-12 11:32:52 -04:00
--will not match actuals exactly due to short-ships
TRUNCATE TABLE rlarp.osmf_dev;
2021-04-28 17:34:44 -04:00
DROP TABLE IF EXISTS tdr;
2021-07-12 11:32:52 -04:00
CREATE TEMP TABLE tdr AS (
2021-04-28 17:34:44 -04:00
SELECT
2021-07-12 11:32:52 -04:00
-----------actuals into baseline-------------------------------------
2021-08-02 15:45:27 -04:00
DATERANGE('2020-06-01','2021-07-31','[]') arange
2021-07-12 11:32:52 -04:00
-----------plan into baseline----------------------------------------
,DATERANGE('2020-06-01','2022-06-01','[)') prange
2021-07-12 11:32:52 -04:00
-----------forecast into baseline------------------------------------
2021-07-12 14:31:22 -04:00
,DATERANGE('2022-06-01','2022-06-01','[)') frange -- this range effectively excludes
2021-07-12 11:32:52 -04:00
-----------baseline selection for increment--------------------------
2021-08-02 15:45:27 -04:00
,DATERANGE('2021-07-31','2021-07-31','[)') selection
2021-07-12 11:32:52 -04:00
-----------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
2021-07-12 11:32:52 -04:00
-----------existing baseline overlap---------------------------------
2021-08-02 15:45:27 -04:00
,DATERANGE('2000-06-01','2021-07-31') overlap
);
2021-07-12 11:32:52 -04:00
--select * from tdr
WITH
gld AS (
2021-04-28 17:34:44 -04:00
SELECT
N1COMP COMP
,N1CCYY FSYR
,KPMAXP PERDS
,N1FSPP PERD
,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT
,N1ED01 EDAT
,to_char(N1ED01,'yymm') CAPR
,N1ED01 - N1SD01 +1 NDAYS
,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
KPCCYY = N1CCYY
CROSS JOIN tdr
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
--SELECT * FROM gld
,forecast_basis AS (
2021-07-12 11:32:52 -04:00
-------------------------------------------------------------------------------------
-----------------------------------plan----------------------------------------------
2021-07-12 11:32:52 -04:00
-------------------------------------------------------------------------------------
2021-04-28 17:34:44 -04:00
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
2021-04-28 17:34:44 -04:00
-----------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
,'2+10' AS "version"
,'plan' iter
2021-04-28 17:34:44 -04:00
FROM
rlarp.osmp_dev o
2021-04-28 17:34:44 -04:00
--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 o.sdate <@ (SELECT prange FROM tdr)
2021-04-28 17:34:44 -04:00
--...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(prange) FROM tdr))
2021-04-28 17:34:44 -04:00
--...or anything that shipped in that period
--OR (o.sdate <@ (SELECT prange FROM tdr) AND sseas IS NOT NULL)
2021-04-28 17:34:44 -04:00
)
2021-07-12 11:32:52 -04:00
---exclude R&A's
2021-04-28 17:34:44 -04:00
AND fs_line = '41010'
2021-07-12 11:32:52 -04:00
---exclude canceled orders
2021-04-28 17:34:44 -04:00
AND calc_status <> 'CANCELED'
2021-07-12 11:32:52 -04:00
---exclude short ships
--AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
2021-04-28 17:34:44 -04:00
---exclude integrated quotes----
AND version = 'b22'
2021-04-28 17:34:44 -04:00
GROUP BY
ss.fspr
2021-04-28 17:34:44 -04:00
,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
2021-07-12 11:32:52 -04:00
UNION ALL
-------------------------------------------------------------------------------------
-----------------------------------forecast------------------------------------------
2021-07-12 11:32:52 -04:00
-------------------------------------------------------------------------------------
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
,'2+10' AS "version"
,'fcst' iter
2021-07-12 11:32:52 -04:00
FROM
rlarp.osmf_dev o
2021-07-12 11:32:52 -04:00
--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)
2021-07-12 11:32:52 -04:00
--...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')
2021-07-12 11:32:52 -04:00
---exclude integrated quotes----
--AND version = 'ACTUALS'
2021-07-12 11:32:52 -04:00
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'
,actuals AS (
2021-07-12 11:32:52 -04:00
-------------------------------------------------------------------------------------
-----------------------------------actuals-------------------------------------------
2021-07-12 11:32:52 -04:00
-------------------------------------------------------------------------------------
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
,'2+10' "version"
,'actuals' iter
2021-07-12 11:32:52 -04:00
FROM
rlarp.osm_dev o
2021-07-12 11:32:52 -04:00
--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 arange FROM tdr)
2021-07-12 11:32:52 -04:00
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate <= (SELECT UPPER(arange) FROM tdr))
2021-07-12 11:32:52 -04:00
--...or anything that shipped in that period
OR (o.sdate <@ (SELECT arange FROM tdr) AND sseas IS NOT NULL)
2021-07-12 11:32:52 -04:00
)
---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'
2021-07-12 11:32:52 -04:00
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
2021-04-28 17:34:44 -04:00
)
--create a union of overlapped actuals and forecast with
--forecast as negative and actuals as positive such that
--adding the diff to the forecast yields actuals
,diff AS (
2021-07-12 15:37:52 -04:00
-------------------------------------------------------------------------------------------------
----------------------forecast is negative-------------------------------------------------------
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
,o.dhidat
,null::text fesind
,null::text dhpost
,o.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
,o.promo
,o.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
,o.sdate
,o.sseas
,'2+10' AS version
2021-07-12 15:37:52 -04:00
,'diff' AS iter
FROM
forecast_basis o
CROSS JOIN tdr
WHERE
--create negative forecast values for overlap (to be netted with actuals for a diff)
(
o.odate <@ DATERANGE(lower(prange),upper(arange))
OR o.sdate <@ DATERANGE(lower(prange),upper(arange))
)
--honor existing sales team adjustments and only alter baseline
--AND tdr.iter ? o.iter
GROUP BY
o.odate
,o.rdate
,o.dhidat
,o.fspr
,o.plnt
,o.promo
,o.return_reas
,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
,o.sdate
,o.sseas
--,o.version
--,o.iter
UNION ALL
2021-07-12 15:37:52 -04:00
-------------------------------------------------------------------------------------------------
----------------------actuals are positive-------------------------------------------------------
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
,o.dhidat
,null::text fesind
,null::text dhpost
,o.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
,o.promo
,o.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
,o.sdate
,o.sseas
,'2+10' AS version
2021-07-12 15:37:52 -04:00
,'diff' AS iter
FROM
actuals o
CROSS JOIN tdr
WHERE
--create negative forecast values for overlap (to be netted with actuals for a diff)
(
o.odate <@ DATERANGE(lower(prange),upper(arange))
OR o.sdate <@ DATERANGE(lower(prange),upper(arange))
)
--honor existing sales team adjustments and only alter baseline
--AND tdr.iter ? o.iter
GROUP BY
o.odate
,o.rdate
,o.dhidat
,o.fspr
,o.plnt
,o.promo
,o.return_reas
,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
,o.sdate
,o.sseas
--,o.version
--,o.iter
)
2021-07-12 11:32:52 -04:00
,collapse_diff AS (
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
,o.dhidat
,null::text fesind
,null::text dhpost
,o.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
,o.promo
,o.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
,round(sum(o.fb_qty),2) fb_qty
,round(sum(o.fb_val_loc),2) 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
,round(sum(o.fb_cst_loc),2) 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
,o.sdate
,o.sseas
--,o.version
--,o.iter
,'2+10' AS version
,'diff' iter
FROM
2021-07-12 11:32:52 -04:00
diff o
WHERE
2021-07-12 11:32:52 -04:00
true
--collect all the rows in the base period and
--the destination slot for the new rows
2021-07-12 11:32:52 -04:00
--o.odate <@ (SELECT tdr.arange 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
o.dhidat
,o.fspr
,o.plnt
,o.promo
,o.return_reas
,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
,o.sdate
,o.sseas
--,o.version
--,o.iter
2021-07-12 11:32:52 -04:00
--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
)
,load AS (
--include all the forecast specified
SELECT * FROM forecast_basis
UNION ALL
--include all the actual order up to the overlap period
SELECT * FROM actuals WHERE
actuals.odate < (SELECT lower(prange) FROM tdr)
UNION ALL
--include the overlap period as a diff
SELECT * FROM collapse_diff
)
INSERT INTO rlarp.osmf_dev SELECT * FROM load;
--UPDATE
-- rlarp.osmf_dev f
--SET
-- fspr = gld.fspr
--FROM
-- (
-- SELECT
-- N1COMP COMP
-- ,N1CCYY FSYRq
-- ,KPMAXP PERDS
-- ,N1FSPP PERD
-- ,to_char(N1FSYP,'FM0000') FSPR
-- ,N1SD01 SDAT
-- ,N1ED01 EDAT
-- ,to_char(N1ED01,'yymm') CAPR
-- ,N1ED01 - N1SD01 +1 NDAYS
-- ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
-- ,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'00') SSPR
-- FROM
-- LGDAT.GLDATREF
-- INNER JOIN LGDAT.GLDATE ON
-- KPCOMP = N1COMP AND
-- KPCCYY = N1CCYY
-- WHERE
-- N1COMP = 93
-- --AND DIGITS(N1FSYP) = '1901'
-- ) gld
--WHERE
-- f.sdate BETWEEN gld.sdat AND gld.edat
-- AND coalesce(f.fspr,'') <> gld.fspr;
2021-04-28 17:34:44 -04:00
COMMIT;
---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;