Compare commits

...

14 Commits

Author SHA1 Message Date
f2b6686f3e insert directly into the pool and igonore net cost amount as a filter 2021-05-03 09:09:21 -04:00
e79c0d5bb4 round before removing, keep any rows that have a qty, value, or cost 2021-04-30 11:25:44 -04:00
36d80dd296 relable actuals later as copy so it is considered part of the baseline 2021-04-29 18:25:09 -04:00
8de3ee5717 fiscal period was blank cause rows not to net to -0- with merge, iteration name incorrect for baseline 2021-04-29 16:12:54 -04:00
b4e2ca7919 target osfm_dev as forecast staging area, no need to preserve; set fspr; stage pool instead of direct insert for debug 2021-04-29 13:42:10 -04:00
0a96759945 all forecast builds become an iteration overtop existing data 2021-04-29 11:16:23 -04:00
ac3c50dbba work on merging actuals 2021-04-28 17:34:44 -04:00
97f73e5107 notes on old update prior year baseline approach 2021-04-28 11:14:43 -04:00
78a9658e80 need to include director comparison to hook for changes 2021-04-23 09:41:38 -04:00
67e5466f72 need to set director while snapping quota rep 2021-04-23 09:08:47 -04:00
0d7cabd0ee merge dev 2021-04-22 17:48:00 -04:00
da8e1353f3 notes on regional pricing and link in target pricing 2021-04-22 17:46:56 -04:00
704957a2fa update product notion to include color tier 2021-04-21 17:24:05 -04:00
683921dda0 work on last price 2021-04-20 16:11:34 -04:00
12 changed files with 1334 additions and 289 deletions

19
build/build_merge.sql Normal file
View File

@ -0,0 +1,19 @@
SELECT
version
,iter
,oseas
,g.sspr || ' ' || to_char(odate,'Mon') omon
,COUNT(*) cnt
,sum(fb_val_loc * r_rate) amt
FROM
rlarp.osmf_stage o
INNER JOIN rlarp.gld g ON
o.odate <@ g.drange
GROUP BY
version
,iter
,oseas
,g.sspr || ' ' || to_char(odate,'Mon')
ORDER BY
oseas
,omon

View File

@ -1,11 +1,8 @@
BEGIN; BEGIN;
DROP TABLE IF EXISTS rlarp.osm_pool; DROP TABLE IF EXISTS rlarp.osm_pool_stage;
DROP TABLE IF EXISTS rlarp.osm_log;
CREATE TABLE IF NOT EXISTS rlarp.osm_log(id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, doc jsonb); CREATE TABLE rlarp.osm_pool_stage AS (
CREATE TABLE IF NOT EXISTS rlarp.osm_pool AS (
WITH WITH
repc AS ( repc AS (
SELECT SELECT
@ -52,7 +49,7 @@ repc AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
) )
,logload AS ( ,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','Initial Build','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING * INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','merge actuals','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
) )
SELECT SELECT
fspr fspr
@ -174,10 +171,12 @@ FROM
--LIMIT 100 --LIMIT 100
) WITH DATA; ) WITH DATA;
CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);
CREATE INDEX osm_pool_logid ON rlarp.osm_pool(logid); --CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr);
GRANT ALL ON TABLE rlarp.osm_log TO api; --ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);
GRANT ALL ON TABLE rlarp.osm_pool TO api; --CREATE INDEX osm_pool_logid ON rlarp.osm_pool(logid);
--GRANT ALL ON TABLE rlarp.osm_log TO api;
--GRANT ALL ON TABLE rlarp.osm_pool TO api;
COMMIT; COMMIT;

183
build/build_pool_schema.sql Normal file
View File

@ -0,0 +1,183 @@
BEGIN;
DROP TABLE IF EXISTS rlarp.osm_pool;
DROP TABLE IF EXISTS rlarp.osm_log;
CREATE TABLE IF NOT EXISTS rlarp.osm_log(id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, doc jsonb);
CREATE TABLE IF NOT EXISTS rlarp.osm_pool AS (
WITH
repc AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,C.A30 REPP
,COALESCE(Q.DIR,'Other') DIRECTOR
FROM
LGDAT.CODE C
LEFT OUTER JOIN RLARP.QRH Q ON
Q.QR = LTRIM(RTRIM(C.A9))
WHERE
C.A2 = 'MM'
)
,SEG AS (
SELECT
GLEC
,SEGM
FROM
(
VALUES
('1CU','Sustainable'),
('1SU','Sustainable'),
('1GR','Greenhouse'),
('1NU','Nursery'),
('1RE','Retail'),
('2WI','Greenhouse'),
('3BM','Other'),
('3CO','Other'),
('3PE','Other'),
('3PP','Other'),
('4CO','Other'),
('4RA','Other'),
('9MI','Other'),
('9SA','Other'),
('9TO','Other')
) X(GLEC, SEGM)
)
----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
)
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','Initial Build','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
)
SELECT
fspr
,plnt
,promo
,terms
,bill_cust||' - '||bc.bvname bill_cust_descr
,ship_cust||' - '||sc.bvname ship_cust_descr
,dsm
,coalesce(repc.repp,dsm) quota_rep_descr
,repc.director
,account billto_group
,shipgrp shipto_group
,chan
,chansub
,CASE seg.segm
--for 1RE coded product
WHEN 'Retail' THEN
CASE o.bill_class
WHEN 'RONL' THEN 'Online'
WHEN 'RNAT' THEN 'National'
WHEN 'RMAS' THEN 'National'
ELSE
------ ship-to class ---------------------
CASE o.chan
WHEN 'GDIS' THEN 'Distribution'
WHEN 'NDIS' THEN 'Distribution'
WHEN 'RDIS' THEN 'Distribution'
WHEN 'GDRP' THEN 'Grower'
WHEN 'NDRP' THEN 'Grower'
WHEN 'RDRP' THEN 'Distribution'
WHEN 'GDIR' THEN 'Grower'
WHEN 'NDIR' THEN 'Grower'
-------this will probably need reviewed--------
WHEN 'RDIR' THEN 'Distribution'
WHEN 'NDIR' THEN 'Grower'
WHEN 'GDIR' THEN 'Grower'
ELSE 'Distribution'
END
END
--for 1SU 1CU coded product
WHEN 'Sustainable' THEN
CASE SUBSTRING(o.coltier,1,1)
--anything with a bio color tier is bio on the channel
WHEN 'R' THEN 'Bio'
ELSE
CASE o.glec
--any 1SU that is not bio is fiber
WHEN '1SU' THEN 'Fiber'
--any 1CU that is not bio is commercial
WHEN '1CU' THEN 'Commercial'
ELSE o.chan
END
END
ELSE o.chan
END chan_retail
,part
,part||coalesce(' - '||i.descr,'') part_descr
,stlcd part_group
,brnd branding
,o.majg||' - '||i.majgd majg_descr
,o.ming||' - '||i.mingd ming_descr
,o.majs||' - '||i.majsd majs_descr
,o.mins||' - '||i.minsd mins_descr
,seg.segm
,CASE WHEN o.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,fb_qty units
,fb_val_loc value_loc
,fb_val_loc * r_rate value_usd
,fb_cst_loc_cur cost_loc
,fb_cst_loc_cur * c_rate cost_usd
,calc_status
,flag
,o.odate order_date
,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') order_month
,oseas order_season
,rdate request_date
,to_char(CASE WHEN extract(month FROM o.rdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.rdate),'FM00')||' - '||to_char(o.rdate,'TMMon') request_month
,rseas request_season
,sdate ship_date
,to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon') ship_month
,sseas ship_season
,version
,iter
,logload.id logid
,logload.doc->>'tag' tag
,logload.doc->>'message' "comment"
,logload.doc->>'type' module
FROM
rlarp.osmf_dev o
CROSS JOIN logload
LEFT OUTER JOIN lgdat.cust bc ON
bc.bvcust = o.bill_cust
LEFT OUTER JOIN lgdat.cust sc ON
sc.bvcust = o.ship_cust
LEFT OUTER JOIN repc ON
repc.rcode = o.dsm
/*
LEFT OUTER JOIN lgdat.majg ON
bqgrp = o.majg
LEFT OUTER JOIN lgdat.mmgp ON
brmgrp = o.ming
AND BRGRP = o.majg
LEFT OUTER JOIN si ON
si.bsmjcd = o.majs
AND si.bsmncd = o.mins
LEFT OUTER JOIN sj ON
sj.bsmjcd = o.majs
*/
LEFT OUTER JOIN seg ON
seg.glec = o.glec
LEFT OUTER JOIN rlarp.itemm i ON
i.item = o.part
--LIMIT 100
) WITH DATA;
CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr);
ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);
CREATE INDEX osm_pool_logid ON rlarp.osm_pool(logid);
GRANT ALL ON TABLE rlarp.osm_log TO api;
GRANT ALL ON TABLE rlarp.osm_pool TO api;
COMMIT;

657
build/build_stage.sql Normal file
View File

@ -0,0 +1,657 @@
BEGIN;
--\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data;
TRUNCATE TABLE rlarp.osmf_dev;
DROP TABLE IF EXISTS tdr;
CREATE TEMP TABLE tdr AS (
SELECT
DATERANGE('2020-05-01','2021-05-01','[)') drange
,DATERANGE(('2020-05-01'::date + '1 year'::interval)::date,('2021-05-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
);
WITH
gld AS (
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
,baseline 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
,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
,'actuals' "version"
,'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....
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(drange) FROM tdr))
--...or anything that shipped in that period
OR (o.sdate <@ (SELECT drange FROM tdr) AND sseas IS NOT NULL)
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
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
)
,incr AS (
SELECT
o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.quoten
,o.quotel
,o.dcodat + interval '1 year' dcodat --incremented
,o.ddqdat + interval '1 year' ddqdat --incremented
,o.dcmdat
,o.fesdat
,o.dhidat + interval '1 year' dhidat --incremented
,o.fesind
,o.dhpost
,gld.fspr --incremented
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.diext
,o.ditdis
,o.discj
,o.dhincr
,o.plnt
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.remit_to
,o.bill_class
,o.bill_cust
,o.bill_rep
,o.bill_terr
,o.ship_class
,o.ship_cust
,o.ship_rep
,o.ship_terr
,o.dsm
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.chansub
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.bill_ctry
,o.bill_prov
,o.bill_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.styc
,o.colc
,o.colgrp
,o.coltier
,o.colstat
,o.sizc
,o.pckg
,o.kit
,o.brnd
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldco
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.ddunit
,o.unti
,o.lbs
,o.plt
,o.plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.fb_qty
,o.fb_val_loc
,o.fb_val_loc_dis
,o.fb_val_loc_qt
,o.fb_val_loc_pl
,o.fb_val_loc_tar
,o.fb_cst_loc
,o.fb_cst_loc_cur
,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
,'b22' "version"
,'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' >= (SELECT LOWER(drange) + INTERVAL '1 year' FROM tdr)
)
,stage AS (
SELECT * FROM incr
UNION ALL
SELECT * FROM baseline
)
,remove 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
,-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
,o.version
,o.iter
FROM
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.drange
OR o.odate <@ tdr.repl
)
---only merge with targeted iterations
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
)
,stack AS (
SELECT * FROM stage
UNION ALL
SELECT * FROM remove
)
,merge_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
,'actuals' AS version
,'copy' iter
FROM
stack o
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)
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
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;
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;
COMMIT;
END
---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;

View File

@ -1,3 +1,3 @@
DELETE FROM rlarp.osmf_dev WHERE odate < '2020-06-01'; DELETE FROM rlarp.osmf_dev WHERE odate < '2021-05-01';
INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev WHERE odate < '2020-06-01'; INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev WHERE odate < '2021-05-01';

View File

@ -1,6 +1,6 @@
BEGIN; BEGIN;
DELETE FROM rlarp.osm_pool WHERE order_date < '2020-06-01'; DELETE FROM rlarp.osm_pool WHERE order_date < '2021-05-01';
INSERT INTO rlarp.osm_pool INSERT INTO rlarp.osm_pool
WITH WITH
@ -135,7 +135,7 @@ FROM
LEFT OUTER JOIN rlarp.itemm i ON LEFT OUTER JOIN rlarp.itemm i ON
i.item = o.part i.item = o.part
WHERE WHERE
odate < '2020-06-01'; odate < '2021-05-01';
SELECT SELECT

View File

@ -145,11 +145,11 @@ gld AS (
WHERE WHERE
( (
--base period orders booked.... --base period orders booked....
o.odate <@ daterange('2019-06-01','2020-06-01') o.odate <@ daterange('2020-06-01','2021-05-01','[)')
--...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 < '2020-06-01') OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2021-05-01')
--...or anything that shipped in that period --...or anything that shipped in that period
OR o.fspr BETWEEN '2001' AND '2012' OR o.fspr BETWEEN '2101' AND '2111'
) )
AND fs_line = '41010' AND fs_line = '41010'
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'

View File

@ -11,3 +11,10 @@ Build A New Forecast
* build the pool for the more limited UI data set: build_pool.sql * build the pool for the more limited UI data set: build_pool.sql
* copy the pool to osmfs_dev from inclusion in the osm_stack and reporting: convert_pool_all.sql * copy the pool to osmfs_dev from inclusion in the osm_stack and reporting: convert_pool_all.sql
* refresh osm_stack_refresh() to integrate into reporting * refresh osm_stack_refresh() to integrate into reporting
Integrate Actuals
------------------------------------------------------------------------------------------------------------------
* `merge_actuals_prep` -> push a fresh baseline into a temp table
* `merge_actuals_exec` -> take the baseline and push it into `osmf`
* `merge_actuals_exec` -> manualls splice in `osmf` into `pool`

View File

@ -1,8 +1,5 @@
$PG -f build_forecast.sql $PGD -f ./build_stage.sql
$PG -f snap_itemm.sql $PGD -f ./snap_itemm.sql
$PG -f snap_cost_current.sql $PGD -f ./snap_cost_current.sql
$PG -f snap_customer.sql $PGD -f ./snap_customer.sql
$PG -f snap_fx.sql $PGD -f ./build_pool.sql
$PG -f build_pool.sql
$PG -f convert_pool_all.sql
$PG -c "CALL rlarp.osm_stack_refresh();"

View File

@ -50,10 +50,13 @@ UPDATE
rlarp.osm_pool o rlarp.osm_pool o
SET SET
quota_rep_descr = (regexp_match(r.repp,'.* - (.*)$'))[1] quota_rep_descr = (regexp_match(r.repp,'.* - (.*)$'))[1]
,director = r.director
FROM FROM
rlarp.repc r rlarp.repc r
WHERE WHERE
r.rcode = o.dsm r.rcode = o.dsm
AND quota_rep_descr <> (regexp_match(r.repp,'.* - (.*)$'))[1]; AND (
quota_rep_descr <> (regexp_match(r.repp,'.* - (.*)$'))[1]
OR o.director <> r.director
);
COMMIT; COMMIT;

View File

@ -5,7 +5,7 @@ SELECT
iter, iter,
sum(fb_val_loc) value_loc sum(fb_val_loc) value_loc
FROM FROM
rlarp.osmfs_dev o rlarp.osmf_stage o
GROUP BY GROUP BY
oseas, 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'), 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'),

View File

@ -4,36 +4,36 @@ WITH
------------------goal price increases--------------------- ------------------goal price increases---------------------
incr AS ( incr AS (
SELECT * FROM (VALUES SELECT * FROM (VALUES
('110','PP','B',0.25), ('110','PP','B',0.3),
('110','PP','T',0.25), ('110','PP','T',0.3),
('110','PP','L',0.4), ('110','PP','L',0.4),
('110','PP','M',0.4), ('110','PP','M',0.4),
('110','PP','P',0.4), ('110','PP','P',0.4),
('110','PP','C',0.4), ('110','PP','C',0.4),
('210','PE','B',0.25), ('210','PE','B',0.3),
('210','PE','T',0.25), ('210','PE','T',0.3),
('210','PE','L',0.25), ('210','PE','L',0.3),
('210','PE','M',0.25), ('210','PE','M',0.3),
('210','PE','P',0.25), ('210','PE','P',0.3),
('210','PE','C',0.25), ('210','PE','C',0.3),
('310','PE','B',0.15), ('310','PE','B',0.25),
('310','PE','T',0.25), ('310','PE','T',0.3),
('310','PE','L',0.25), ('310','PE','L',0.3),
('310','PE','M',0.25), ('310','PE','M',0.3),
('310','PE','P',0.25), ('310','PE','P',0.3),
('310','PE','C',0.25), ('310','PE','C',0.3),
('310','PP','B',0.13), ('310','PP','B',0.25),
('310','PP','T',0.16), ('310','PP','T',0.25),
('310','PP','L',0.16), ('310','PP','L',0.25),
('310','PP','M',0.16), ('310','PP','M',0.25),
('310','PP','P',0.16), ('310','PP','P',0.25),
('310','PP','C',0.16), ('310','PP','C',0.25),
('310','PS','B',0.13), ('310','PS','B',0.25),
('310','PS','T',0.16), ('310','PS','T',0.25),
('310','PS','L',0.16), ('310','PS','L',0.25),
('310','PS','M',0.16), ('310','PS','M',0.25),
('310','PS','P',0.16), ('310','PS','P',0.25),
('310','PS','C',0.16), ('310','PS','C',0.25),
('320','PE','B',0.25), ('320','PE','B',0.25),
('320','PE','T',0.25), ('320','PE','T',0.25),
('320','PE','L',0.25), ('320','PE','L',0.25),
@ -46,21 +46,13 @@ incr AS (
('320','PP','M',0.25), ('320','PP','M',0.25),
('320','PP','P',0.25), ('320','PP','P',0.25),
('320','PP','C',0.25), ('320','PP','C',0.25),
('910','PE','B',0.15), ('910','PE','B',0.25),
('910','PE','D',0.25), ('910','PE','D',0.25),
('910','PE','F',0.25), ('910','PE','F',0.25),
('910','PP','B',0.15), ('910','PP','B',0.25),
('910','PP','D',0.25), ('910','PP','D',0.25),
('910','PP','F',0.25), ('910','PP','F',0.25),
('910','PS','B',0.15), ('910','PS','B',0.25),
('910','','B',0.15),
('910','','D',0.25),
('910','','F',0.25),
('910','','T',0.25),
('910','','L',0.25),
('910','','M',0.25),
('910','','P',0.25),
('910','','C',0.25),
('910','PS','D',0.25), ('910','PS','D',0.25),
('910','PS','F',0.25), ('910','PS','F',0.25),
('610','','B',0.02), ('610','','B',0.02),
@ -68,10 +60,28 @@ incr AS (
('610','','W',0.02) ('610','','W',0.02)
) x(MAJG,ASSC,COLTIER,RATE) ) x(MAJG,ASSC,COLTIER,RATE)
) )
,chx AS (
SELECT * FROM ( VALUES
('DIRECT','DIR','Direct'),
('DISTRIB DROP SHIP','DRP','Drop'),
('DISTRIBUTOR','WHS','Warehouse')
) x(xchan, chan, tchan)
)
,ds AS (
SELECT * FROM ( VALUES
('B','X','BASE','Base',''),
('B','L','BASE LABELED','Base','L'),
('B','P','BASE PRINTED','Base','P'),
('C','X','COLOR','Color',''),
('C','L','COLOR LABELED','Color','L'),
('C','P','COLOR PRINTED','Color','P')
) x(colgrp, brand, dataseg, tcol, tbrand)
)
------------carve out pricing baseline data-------------------- ------------carve out pricing baseline data--------------------
,p AS ( ,p AS (
SELECT SELECT
o.part o.part
,o.styc||'.'||o.coltier||substring(o.sizc,1,3) productt
,o.styc||'.'||o.colgrp||substring(o.sizc,1,3) product ,o.styc||'.'||o.colgrp||substring(o.sizc,1,3) product
,o.glec ,o.glec
,o.styc ,o.styc
@ -82,6 +92,7 @@ incr AS (
,o.sizc ,o.sizc
,i.suffix ,i.suffix
,substring(o.chan,1,1) chgrp ,substring(o.chan,1,1) chgrp
,o.chan
,o.account ,o.account
,o.shipgrp ,o.shipgrp
,o.fb_qty units ,o.fb_qty units
@ -127,7 +138,7 @@ incr AS (
,o.shipgrp ,o.shipgrp
,o.odate DESC ,o.odate DESC
) )
--SELECT * FROM p WHERE account ~ 'ACOSTA' --SELECT * FROM p WHERE account ~ 'AMERICAN HORT' and product = 'AZA06000.CBXX' and chgrp = 'W' order by rn ASC
------------build global py asp------------------ ------------build global py asp------------------
,baseline AS ( ,baseline AS (
SELECT SELECT
@ -144,130 +155,91 @@ incr AS (
,assc ,assc
,chgrp ,chgrp
) )
----------pivot the pricing out into columns per customer/product-------- --SELECT
,pivot AS ( -- p.product
SELECT -- --,p.styc
p.product -- --,p.glec
,p.styc -- ,p.majg
,p.glec -- ,p.assc
,p.majg -- ,p.colgrp
,p.assc -- --,p.coltier
,p.colgrp -- --,p.sizc
,p.coltier -- --,p.suffix
,p.sizc -- ,p.chgrp
,p.suffix -- ,p.account
,p.chgrp -- ,p.shipgrp
,p.account -- --,bl.py_gasp
,p.shipgrp -- ,sum(units) FILTER (WHERE oseas = 2020) py_units
,bl.py_gasp -- ,sum(units) FILTER (WHERE oseas = 2021) cy_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units -- ,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp -- ,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,round(avg(price) FILTER (WHERE rn = 1),5) last_price -- ,max(odate) FILTER (WHERE rn = 1) last_order
,max(odate) FILTER (WHERE rn = 1) last_order -- ,i.rate incr_rate
,CASE p.chgrp -- ,CASE p.chgrp
WHEN 'D' THEN i.rate -- WHEN 'D' THEN .5
ELSE CASE p.majg -- ELSE CASE p.majg
WHEN '610' THEN .02 -- WHEN '610' THEN .02
ElSE CASE p.colgrp -- ElSE CASE p.colgrp
WHEN 'B' THEN .1 -- WHEN 'B' THEN .15
WHEN 'C' THEN .15 -- WHEN 'C' THEN .20
ELSE 1 -- ELSE 1
END -- END
END -- END
END rate -- END rate
,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL -- --,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- -- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
THEN 'unknown' -- -- THEN 'unknown'
ELSE 'new' -- -- ELSE 'new'
END -- -- END
ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL -- -- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
THEN 'lost' -- -- THEN 'lost'
ELSE 'repeat' -- -- ELSE 'repeat'
END -- -- END
END flag -- --END flag
FROM --FROM
p -- p
LEFT OUTER JOIN baseline bl ON -- LEFT OUTER JOIN baseline bl ON
bl.product = p.product -- bl.product = p.product
AND bl.majg = p.majg -- AND bl.majg = p.majg
AND bl.assc = p.assc -- AND bl.assc = p.assc
AND bl.chgrp = p.chgrp -- AND bl.chgrp = p.chgrp
LEFT OUTER JOIN incr i ON -- LEFT OUTER JOIN incr i ON
i.majg = p.majg -- i.majg = p.majg
AND i.assc = p.assc -- AND i.assc = p.assc
AND i.coltier = p.coltier -- AND i.coltier = p.coltier
AND p.glec <> '1RE' -- AND p.glec <> '1RE'
--WHERE
GROUP BY -- p.account ~ 'AMERICAN HORT' and p.product = 'AZA06000.CBXX' and p.chgrp = 'W'
p.product --GROUP BY
,p.styc -- p.product
,p.glec -- --,p.styc
,p.majg -- --,p.glec
,p.assc -- ,p.majg
,p.coltier -- ,p.assc
,p.colgrp -- --,p.coltier
,p.sizc -- ,p.colgrp
,p.suffix -- --,p.sizc
,p.chgrp -- --,p.suffix
,p.account -- ,p.chgrp
,p.shipgrp -- ,p.account
,bl.py_gasp -- ,p.shipgrp
,i.rate -- --,bl.py_gasp
) -- ,i.rate
----------------create the new price----------------- ----------calculate pricing as it sits in the forecast--------------
,adj AS (
SELECT
p.product
,p.styc
,p.glec
,p.majg
,p.assc
,p.coltier
,p.sizc
,p.suffix
,p.account
,p.shipgrp
,p.py_gasp
,p.cy_units
,p.py_asp
,p.last_price
,p.last_order
,p.rate
,p.flag
,CASE p.flag
----------------------if repeat business then get to prior year + target %--------------------------------------------------------------
WHEN 'repeat' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
WHEN 'lost' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
----------------------if new business, move towards py_gasp + target % : lesser of py gloabl + target or last + target------------------
WHEN 'new' THEN least(last_price * COALESCE(1+rate,1) - last_price,greatest(py_gasp * COALESCE(1+rate,1) - last_price,0))
END price_increment
FROM
pivot p
)
--SELECT * FROM adj LIMIT 10000
--------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"application of last price and target increases to all forecast orders",
"tag":"last price",
"type":"build"
}$$::jsonb doc
RETURNING *
)
,poolprice AS ( ,poolprice AS (
SELECT SELECT
i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product i.stlc||'.'||i.coltier||substring(i.sizc,1,3) productt
,i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product
,o.quota_rep_descr ,o.quota_rep_descr
,substring(majg,1,3) majg
,substring(chan,1,1) chgrp
,o.billto_group ,o.billto_group
,o.shipto_group ,o.shipto_group
,order_season ,order_season
,sum(units) units ,round(sum(units) ,2) fc_units
,sum(value_loc) valloc --,round(sum(value_loc) ,2) valloc
,sum(value_usd) valusd --,round(sum(value_usd) ,2) valusd
,sum(o.value_loc)/sum(o.units) price ,round(sum(o.value_loc)/sum(o.units),5) fc_price
,jsonb_agg(DISTINCT iter) iters ,jsonb_agg(DISTINCT iter) iters
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
@ -280,127 +252,335 @@ WHERE
--only include baseline stuff--- --only include baseline stuff---
AND segm <> 'Retail' AND segm <> 'Retail'
GROUP BY GROUP BY
i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) i.stlc||'.'||i.coltier||substring(i.sizc,1,3)
,i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
,o.quota_rep_descr ,o.quota_rep_descr
,substring(majg,1,3)
,substring(chan,1,1)
,o.billto_group ,o.billto_group
,o.shipto_group ,o.shipto_group
,order_season ,order_season
--AND iter <> 'upload price' --AND iter <> 'upload price'
HAVING
sum(o.units) <> 0
) )
, pooladj AS ( ----------pivot the pricing out into columns per customer/product--------
,pivot AS (
SELECT SELECT
p.product p.productt
,p.quota_rep_descr ,p.product
,p.billto_group --,p.styc
,p.shipto_group --,p.glec
,p.price ,p.majg
,a.py_gasp ,p.assc
,a.rate ,p.colgrp
,a.last_price --,p.coltier
,a.price_increment --,p.sizc
--,p.suffix
,p.chgrp
,p.account
,p.shipgrp
,bl.py_gasp
,sum(units) FILTER (WHERE oseas = 2020) py_units
,sum(units) FILTER (WHERE oseas = 2021) cy_units
,round(sum(val_usd) FILTER (WHERE oseas = 2020)/sum(units) FILTER (WHERE oseas = 2020),5) py_asp
,round(sum(val_usd) FILTER (WHERE oseas = 2021)/sum(units) FILTER (WHERE oseas = 2021),5) cy_asp
,round(avg(price) FILTER (WHERE rn = 1),5) last_price
,max(odate) FILTER (WHERE rn = 1) last_order
,CASE p.chgrp
WHEN 'D' THEN i.rate
ELSE CASE p.majg
WHEN '610' THEN .02
ElSE CASE p.colgrp
WHEN 'B' THEN .15
WHEN 'C' THEN .20
ELSE 1
END
END
END rate
,ms.avg_price target
--,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
-- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'unknown'
-- ELSE 'new'
-- END
-- ELSE CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'lost'
-- ELSE 'repeat'
-- END
--END flag
FROM FROM
poolprice p p
,adj a LEFT OUTER JOIN baseline bl ON
WHERE bl.product = p.product
a.product = p.product AND bl.majg = p.majg
AND a.account = p.billto_group AND bl.assc = p.assc
AND a.shipgrp = p.shipto_group AND bl.chgrp = p.chgrp
LEFT OUTER JOIN incr i ON
i.majg = p.majg
AND i.assc = p.assc
AND i.coltier = p.coltier
AND p.glec <> '1RE'
-----convert to target price channels---------
LEFT OUTER JOIN chx ON
chx.chan = p.chan
-----convert to target price product level----
LEFT OUTER JOIN ds ON
ds.colgrp = p.colgrp
AND ds.brand = substring(p.sizc,3,1)
LEFT OUTER JOIN pricequote.market_setavgprice ms ON
ms.mold = substring(p.product,1,8)
AND ms.chan = chx.xchan
AND ms.data_segment = ds.dataseg
AND ms.season = '2021'
AND ms.region = 'ALL'
GROUP BY
p.productt
,p.product
--,p.styc
--,p.glec
,p.majg
,p.assc
--,p.coltier
,p.colgrp
--,p.sizc
--,p.suffix
,p.chgrp
,p.account
,p.shipgrp
,bl.py_gasp
,ms.avg_price
,i.rate
) )
SELECT * FROM pooladj WHERE product ~ 'STG06000' AND shipto_group = 'BWI' limit 100 --,test_unique AS (
--SELECT * FROM poolprice WHERE product ~ 'TCA06600' AND shipto_group = 'BWI' AND quota_rep_descr = 'BRYAN HILL' LIMIT 1000
-------------build the iteration rows----------------
--,ins AS (
--SELECT --SELECT
-- o.fspr -- p.*
-- ,o.plnt ---master data -- ,count(*) OVER (partition by productt, majg, chgrp, account, shipgrp) cnt
-- ,o.promo --history date mix --FROM
-- ,o.terms -- pivot p
-- ,o.bill_cust_descr --history cust mix --)
-- ,o.ship_cust_descr --history cust mix --SELECT * FROM test_unique where cnt > 1
-- ,o.dsm --SELECT * FROM pivot LIMIT 1000
-- ,o.quota_rep_descr --master data --------------join forecast price-------------------
-- ,o.director ,fcp AS (
-- ,o.billto_group --master data SELECT
pp.productt
,pp.product
,pp.majg
,pp.quota_rep_descr
,pp.order_season
,pp.billto_group
,pp.shipto_group
,pp.fc_units
,pp.fc_price
,pp.iters
,p.py_gasp
,p.cy_units
,p.py_asp
,p.last_price
,p.last_order
,p.target
,p.rate
--need to link in targets pricing and price list for cap purposes---------
--also need to link regional price lists so we don't blow past those------
,COALESCE(py_asp,py_gasp) * (1 + rate) fc_price
FROM
poolprice pp
LEFT OUTER JOIN pivot p ON
pp.productt = p.productt
AND pp.majg = p.majg
AND pp.chgrp = p.chgrp
AND pp.billto_group = p.account
AND pp.shipto_group = p.shipgrp
)
SELECT * FROM fcp limit 100
----------------create the new price-----------------
--,adj AS (
--SELECT
-- p.product
-- ,p.styc
-- ,p.glec
-- ,p.majg
-- ,p.assc
-- ,p.coltier
-- ,p.sizc
-- ,p.suffix
-- ,p.account
-- ,p.shipgrp
-- ,p.py_gasp
-- ,p.cy_units
-- ,p.py_asp
-- ,p.last_price
-- ,p.last_order
-- ,p.rate
-- ,p.flag
-- ,CASE p.flag
-- ----------------------if repeat business then get to prior year + target %--------------------------------------------------------------
-- WHEN 'repeat' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
-- WHEN 'lost' THEN greatest(py_asp * COALESCE(1+rate,1) - last_price,0)
-- ----------------------if new business, move towards py_gasp + target % : lesser of py gloabl + target or last + target------------------
-- WHEN 'new' THEN least(last_price * COALESCE(1+rate,1) - last_price,greatest(py_gasp * COALESCE(1+rate,1) - last_price,0))
-- END price_increment
--FROM
-- pivot p
--)
--SELECT * FROM adj LIMIT 1000
----------------create a log entry--------------------
--,log AS (
-- INSERT INTO
-- rlarp.osm_log(doc)
-- SELECT
-- $${
-- "message":"application of last price and target increases to all forecast orders",
-- "tag":"last price",
-- "type":"build"
-- }$$::jsonb doc
-- RETURNING *
--)
--,poolprice AS (
--SELECT
-- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) product
-- ,o.quota_rep_descr
-- ,o.billto_group
-- ,o.shipto_group -- ,o.shipto_group
-- ,o.chan --master data -- ,order_season
-- ,o.chansub -- ,sum(units) units
-- ,o.chan_retail -- ,sum(value_loc) valloc
-- ,o.part -- ,sum(value_usd) valusd
-- ,o.part_descr -- ,sum(o.value_loc)/sum(o.units) price
-- ,o.part_group -- ,jsonb_agg(DISTINCT iter) iters
-- ,o.branding
-- ,o.majg_descr
-- ,o.ming_descr
-- ,o.majs_descr
-- ,o.mins_descr
-- ,o.segm
-- ,o.substance
-- ,o.fs_line --master data
-- ,o.r_currency --history cust mix
-- ,o.r_rate --master data
-- ,o.c_currency --master data
-- ,o.c_rate --master data
-- ,0::numeric units
-- ,ROUND(o.units * (a.price_increment/o.r_rate),2) value_loc
-- ,ROUND(o.units * a.price_increment,2) value_usd
-- ,0::numeric cost_loc
-- ,0::numeric cost_usd
-- ,o.calc_status --0
-- ,o.flag --0
-- ,o.order_date --history date mix
-- ,o.order_month
-- ,o.order_season
-- ,o.request_date --history date mix
-- ,o.request_month
-- ,o.request_season
-- ,o.ship_date --history date mix
-- ,o.ship_month
-- ,o.ship_season
-- ,o.version
-- ---this iteration has to be listed in the master template file in order to be effectively included---
-- ,'upload price' iter
-- ,log.id
-- ,COALESCE(log.doc->>'tag','') "tag"
-- ,log.doc->>'message' "comment"
-- ,log.doc->>'type' module
-- -----------debug columns---------
-- --,value_usd/units price
-- --,a.py_gasp
-- --,a.rate
-- --,a.last_price
-- --,a.price_increment
--FROM --FROM
-- rlarp.osm_pool o -- rlarp.osm_pool o
-- ,rlarp.itemmv i -- ,rlarp.itemmv i
-- ,adj a
-- ,log
--WHERE --WHERE
-- i.item = o.part -- i.item = o.part
-- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3) -- --AND o.units <> 0
-- AND a.account = o.billto_group
-- AND a.shipgrp = o.shipto_group
-- AND a.price_increment <> 0
-- AND o.units <> 0
-- ---only apply to 2022 orders---- -- ---only apply to 2022 orders----
-- AND o.order_date >= '2021-06-01' -- AND o.order_date >= '2021-06-01'
-- --only include baseline stuff--- -- --only include baseline stuff---
-- AND iter <> 'upload price' -- AND segm <> 'Retail'
--GROUP BY
-- i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
-- ,o.quota_rep_descr
-- ,o.billto_group
-- ,o.shipto_group
-- ,order_season
-- --AND iter <> 'upload price'
--) --)
-------------aggregate the impact------------ --, pooladj AS (
----SELECT * FROM ins limit 10000 -- SELECT
-- p.product
-- ,p.quota_rep_descr
-- ,p.billto_group
-- ,p.shipto_group
-- ,p.price
-- ,a.py_gasp
-- ,a.rate
-- ,a.last_price
-- ,a.price_increment
-- FROM
-- poolprice p
-- ,adj a
-- WHERE
-- a.product = p.product
-- AND a.account = p.billto_group
-- AND a.shipgrp = p.shipto_group
--)
--SELECT * FROM pooladj WHERE product ~ 'STG06000' AND shipto_group = 'BWI' limit 100
----SELECT * FROM poolprice WHERE product ~ 'TCA06600' AND shipto_group = 'BWI' AND quota_rep_descr = 'BRYAN HILL' LIMIT 1000
---------------build the iteration rows----------------
----,ins AS (
----SELECT ----SELECT
---- order_season ---- o.fspr
---- ,sum(value_loc) val_loc ---- ,o.plnt ---master data
---- ,sum(value_usd) val_usd ---- ,o.promo --history date mix
---- ,o.terms
---- ,o.bill_cust_descr --history cust mix
---- ,o.ship_cust_descr --history cust mix
---- ,o.dsm
---- ,o.quota_rep_descr --master data
---- ,o.director
---- ,o.billto_group --master data
---- ,o.shipto_group
---- ,o.chan --master data
---- ,o.chansub
---- ,o.chan_retail
---- ,o.part
---- ,o.part_descr
---- ,o.part_group
---- ,o.branding
---- ,o.majg_descr
---- ,o.ming_descr
---- ,o.majs_descr
---- ,o.mins_descr
---- ,o.segm
---- ,o.substance
---- ,o.fs_line --master data
---- ,o.r_currency --history cust mix
---- ,o.r_rate --master data
---- ,o.c_currency --master data
---- ,o.c_rate --master data
---- ,0::numeric units
---- ,ROUND(o.units * (a.price_increment/o.r_rate),2) value_loc
---- ,ROUND(o.units * a.price_increment,2) value_usd
---- ,0::numeric cost_loc
---- ,0::numeric cost_usd
---- ,o.calc_status --0
---- ,o.flag --0
---- ,o.order_date --history date mix
---- ,o.order_month
---- ,o.order_season
---- ,o.request_date --history date mix
---- ,o.request_month
---- ,o.request_season
---- ,o.ship_date --history date mix
---- ,o.ship_month
---- ,o.ship_season
---- ,o.version
---- ---this iteration has to be listed in the master template file in order to be effectively included---
---- ,'upload price' iter
---- ,log.id
---- ,COALESCE(log.doc->>'tag','') "tag"
---- ,log.doc->>'message' "comment"
---- ,log.doc->>'type' module
---- -----------debug columns---------
---- --,value_usd/units price
---- --,a.py_gasp
---- --,a.rate
---- --,a.last_price
---- --,a.price_increment
----FROM ----FROM
---- ins ---- rlarp.osm_pool o
----GROUP BY ---- ,rlarp.itemmv i
---- order_season; ---- ,adj a
--,del AS ( ---- ,log
-- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING * ----WHERE
--) ---- i.item = o.part
--INSERT INTO ---- AND a.product = i.stlc||'.'||i.colgrp||substring(i.sizc,1,3)
-- rlarp.osm_pool ---- AND a.account = o.billto_group
--SELECT * FROM ins; ---- AND a.shipgrp = o.shipto_group
-- ---- AND a.price_increment <> 0
--COMMIT; ---- AND o.units <> 0
---- ---only apply to 2022 orders----
---- AND o.order_date >= '2021-06-01'
---- --only include baseline stuff---
---- AND iter <> 'upload price'
----)
---------------aggregate the impact------------
------SELECT * FROM ins limit 10000
------SELECT
------ order_season
------ ,sum(value_loc) val_loc
------ ,sum(value_usd) val_usd
------FROM
------ ins
------GROUP BY
------ order_season;
----,del AS (
---- DELETE FROM rlarp.osm_pool WHERE iter = 'upload price' RETURNING *
----)
----INSERT INTO
---- rlarp.osm_pool
----SELECT * FROM ins;
----
----COMMIT;