Compare commits

...

44 Commits

Author SHA1 Message Date
be9ca7b848 ignore new dbeaver folder 2021-07-14 15:47:22 -04:00
6e7f45a010 isolate and remove pull-forward as an iteration 2021-07-14 15:46:58 -04:00
61b6cfe054 get rid of old months inquirey 2021-07-14 15:46:38 -04:00
e50113f825 comment out debug selections 2021-07-14 15:45:39 -04:00
ad56a96cd3 monthly inquirey 2021-07-14 15:44:47 -04:00
8e8cd605b2 revert back to old dates on rolling and start working on pull-forward removal 2021-07-12 17:55:13 -04:00
7de60f37ce include plan all the way back from 2021 2021-07-12 15:54:10 -04:00
0e3e1205df comments and version name 2021-07-12 15:37:52 -04:00
caa83b8a98 total up forecast pool by month 2021-07-12 15:35:15 -04:00
1c5b17daa0 update pool component to reset the log file and sequence for a new forecast 2021-07-12 15:34:50 -04:00
5520337446 summary of pool by month 2021-07-12 15:33:17 -04:00
73c3171d04 Merge branch 'dev' of github.com:The-HC-Companies/forecast_api into dev 2021-07-12 14:48:47 -04:00
a5dc4aaa85 group the actuals so the timestamp on the update is excluded 2021-07-12 14:48:31 -04:00
2a95101d3d
Update README.md 2021-07-12 14:44:56 -04:00
ed5f9a1d0f
Update README.md 2021-07-12 14:42:19 -04:00
fc8ee9b688
Update README.md 2021-07-12 14:39:21 -04:00
ebfe4d7ee1
Update README.md 2021-07-12 14:37:49 -04:00
1c8593a9a8 break in bullets 2021-07-12 14:33:51 -04:00
652257fbf0 revised for new rolling forecast approach 2021-07-12 14:32:03 -04:00
df12babf00 update approach and date range 2021-07-12 14:31:22 -04:00
c9d87ffccb rename script 2021-07-12 14:28:28 -04:00
39dc65ab9c immediately insert the forecast pool to the live pool 2021-07-12 14:27:05 -04:00
716c18e5ba differentiate service setup 2021-07-12 14:26:13 -04:00
391e6bf09a rename files to reflect intent 2021-07-12 11:32:52 -04:00
c5b9d39b5a update readme 2021-07-12 11:07:28 -04:00
3393136148 move inquiries to their own folder 2021-07-12 10:52:25 -04:00
1a6121d564 get rid of more old one-off scripts 2021-07-12 10:46:40 -04:00
a498b79630 get rid of old approach scripts 2021-07-12 10:30:39 -04:00
7c7f5b581c get rid of old scripts, or even slightly old scripts 2021-07-12 10:26:38 -04:00
b9c6c64764 back to original stage file 2021-07-09 16:18:01 -04:00
7f8ed574c5 work on 1+11 2021-07-09 16:14:29 -04:00
f3f7b7a177 work on a new forecast approach 2021-07-09 00:54:44 -04:00
f8a96d6df2 work on 1+11 forecast 2021-07-06 09:33:58 -04:00
8373b5753a move Retail 2021-06-28 09:19:06 -04:00
152dce9aeb smoothing scripts 2021-06-25 13:07:26 -04:00
1fdcd053bf move Jnauary sales back to June 2021-06-25 12:44:55 -04:00
1d045051b7 move select June and earlier order from October ship to June ship 2021-06-25 10:42:58 -04:00
11f6ae2e42 work on making sales adjustments 2021-06-25 09:36:35 -04:00
3e11a3305c convert forecast to budget 2021-06-14 09:57:00 -04:00
5ae24880f2 scale fiber 2021-06-14 09:56:29 -04:00
f65b0d02f8 curl the api 2021-06-10 13:04:33 -04:00
33fe05d2a7 top level generic scaling scripts 2021-06-10 13:04:01 -04:00
a190c1cf63 adjust account plug to be sales basis 2021-06-10 12:03:00 -04:00
b32d897b1d align rate column 2021-06-09 16:53:43 -04:00
64 changed files with 2880 additions and 19401 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ changes.html
*.log *.log
/.dbeaver /.dbeaver
.project .project
/.settings

View File

@ -1,54 +1,19 @@
Special concepts This will not work without exactly the right database schema which is out of scope. this is only the running process part of the forecast.
* relationships betwen dates (order, request, ship) and apply that mix to a new month
* apply a base mix of promo codes and terms codes to a new month of forecast where there is no existing baseline to pull from ## Setup
* effects of building baselines off of original, or all changes so far
* pricing and units * git clone (to //opt for verbatim use of the .service file)
* `npm install`
Slowness Options * create certs: `chmod 700 create_certs.sh` , `./create_certs.sh`
* move json to a separate table and the change can be marked with a sequence * clone sample env `cp .env.sample .env` and specify 1) database creds 2) working directory 3) listening port
* have the data actually be a query from the database * run: `node index.js`
* reduce number of columns in fc_pool * navigate to `https://localhost:8080/` to valide it's connectable
* open the spreadsheet and specify the target connection
Discussion
* special look at Mar-Apr sales since it is being copied from plan or older data additionally, to setup as service
- [ ] at the end of March, we will swap out the budget from March with actuals and re-forecast * copy .service file to //etc/systemd/system/ (adjust user/working direct if needed)
- [ ] need to think through how to overlay actuals into the budget * `sudo systemctl enable forecast_api.service`
* last price paid snap - preload
- [ ] dont do this ## Initial Forecast
* FX - currently nothing loaded
- [ ] we'll pick one (see Bruno) * all SQL depends on unchanged core sales matrix table schema
* cost - snap to current cost
- [ ] snap to current
* need to be able to reload the forecast as we lock in new months of sales data - replay changes over top?
* item 1
* refresh new part list
* add comments on monthly
* add a tag that is separate from the comment?
* need to test extrapolating osm_pool into osmp_dev
Review
* continuous update with actuals
* next years sales is going to change everyday as new things happen with open orders
* need to be populating shipping season instead of leaving null
* need to do with past-due's -> when do they ship?
* snapshot several years of history into the forecast so there is no need to bring in actuals
* going to have to rebuild the forecast when actuals change
CI
* snapshot pool
* adjust 2021 orders
* integrate with 2020 open to get to 2021 sales (osm_stack)
* show 2021 forecasted order as forecast
* show 2021 forecasted sales (need to update ship season so it is not null)
Walk Issues
* canceled orders
* changed orders
* short ships
* exchange rates
Things to Demo
* product swap out
* pivot re-org
* don't use filters
* assumes all changes target USD

View File

@ -1,2 +0,0 @@
DELETE FROM rlarp.osmf_dev WHERE dsm = 'PW';
INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev;

View File

@ -1,163 +0,0 @@
BEGIN;
DELETE FROM rlarp.osm_pool WHERE quota_rep_descr = 'PW';
INSERT INTO rlarp.osm_pool
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','Retail'),
('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,'') <> ''
)
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
WHEN 'Retail' THEN
CASE o.bill_class
WHEN 'RMAS' THEN 'MASS'
WHEN 'RNAT' THEN 'NATIONAL'
ELSE 'OTHER'
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.osmfs_dev o
INNER JOIN rlarp.osm_log logload ON
logload.id = 1
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
WHERE
dsm = 'PW';
/*
SELECT
"version",
iter,
order_season,
order_month ,
sum(value_usd ) value_usd
FROM
RLARP.osm_pool op
WHERE
--oseas = 2020
fs_line = '41010'
--AND odate < '2020-04-01'
AND CALC_STATUS <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
GROUP BY
"version",
iter,
order_season,
order_month;
*/
--ROLLBACK;
commit;

View File

@ -1,43 +0,0 @@
BEGIN;
WITH
plist AS (
SELECT DISTINCT
part
,plnt
FROM
rlarp.osmfs_dev
-----prevent a list of fake parts
INNER JOIN lgdat.stka ON
v6part = part
AND v6plnt = plnt
)
,clist AS (
SELECT
p.part
,p.plnt
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs) stdcost
FROM
plist p
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = p.part
AND im.cgplnt = p.plnt
LEFT OUTER JOIN lgdat.icstp ip ON
ip.chpart = p.part
AND ip.chplnt = p.plnt
LEFT OUTER JOIN lgdat.icstr ir ON
ir.y0part = p.part
AND ir.y0plnt = p.plnt
)
UPDATE
rlarp.osmfs_dev o
SET
fb_cst_loc_cur = c.stdcost * o.fb_qty
FROM
clist c
WHERE
c.part = o.part
AND c.plnt = o.plnt;
--AND version = 'b21';
commit;

View File

@ -1,209 +0,0 @@
----------------------------SET BILL-TO REP------------------------------------
UPDATE
RLARP.OSMFS_DEV S
SET
BILL_REP = C.BVSALM
,BILL_CLASS = C.BVCLAS
,BILL_TERR = C.BVTERR
,BILL_CTRY = C.bvctry
,bill_prov = C.bvprcd
,bill_post = C.bvpost
,remit_to = c.bvcomp
,ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = S.BILL_CUST
AND (
COALESCE(S.BILL_REP,'') <> C.BVSALM
OR COALESCE(S.BILL_CLASS,'') <> C.BVCLAS
OR COALESCE(S.BILL_TERR,'') <> C.BVTERR
OR COALESCE(BILL_CTRY,'') <> C.bvctry
OR COALESCE(bill_prov,'') <> C.bvprcd
OR COALESCE(bill_post,'') <> C.bvpost
OR COALESCE(remit_to,'') <> c.bvcomp::text
);
----------------------------SET SHIP-TO REP------------------------------------
UPDATE
RLARP.OSMFS_DEV S
SET
SHIP_REP = C.BVSALM
,SHIP_CLASS = C.BVCLAS
,SHIP_TERR = C.BVTERR
,dest_CTRY = C.bvctry
,dest_prov = C.bvprcd
,dest_post = C.bvpost
FROM
LGDAT.CUST C
WHERE
C.BVCUST = S.SHIP_CUST
AND (
COALESCE(S.SHIP_REP,'') <> C.BVSALM
OR COALESCE(S.SHIP_CLASS,'') <> C.BVCLAS
OR COALESCE(S.SHIP_TERR,'') <> C.BVTERR
OR COALESCE(dest_CTRY,'') <> C.bvctry
OR COALESCE(dest_prov,'') <> C.bvprcd
OR COALESCE(dest_post,'') <> C.bvpost
);
----------------------------SET BILLTO GROUP------------------------------------
UPDATE
RLARP.OSMFS_DEV O
SET
ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = O.BILL_CUST
AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END;
----------------------------SET SHIPTO GROUP------------------------------------
UPDATE
RLARP.OSMFS_DEV O
SET
SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = O.SHIP_CUST
AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,'');
---------------------------SET CHANNEL-----------------------------------------
UPDATE
rlarp.osmfs_dev
SET
CHAN = CASE SUBSTRING(BILL_CLASS,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END,
CHANSUB = CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END
WHERE
COALESCE(CHAN,'') <> CASE SUBSTRING(BILL_CLASS,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END
OR
COALESCE(CHANSUB,'') <> CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END;
---------------------------SET QUOTA REP---------------------------------------
UPDATE
RLARP.OSMFS_DEV S
SET
DSM = CR.QUOTA_REP
FROM
(
SELECT DISTINCT
VERSION,
COALESCE(GLEC,'') GLEC,
COALESCE(MING,'') MING,
BILL_CUST,
SHIP_CUST,
------------quota rep column--------------
RTRIM(
--retail items go to currep, or if null go to 90005
CASE WHEN S.GLEC IN ('1RE','1CU') THEN
CASE WHEN BVCTRY = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--select customers go to select reps
CASE ACCOUNT
------Alecia Latini-------------------------------
WHEN 'DO IT BEST' THEN '90006'
WHEN 'ACE HARDWARE' THEN '90006'
WHEN 'ALDI' THEN '90006'
WHEN 'AMAZON.COM' THEN '90006'
WHEN 'GARDEN RIDGE CORP' THEN '90006' --AKA "At Home"
WHEN 'TRUE VALUE' THEN '90006'
WHEN 'WAYFAIR' THEN '90006'
WHEN 'GRIFFIN' THEN '90006'
WHEN 'WAL-MART' THEN '90006'
------Tony Landino--------------------------------
WHEN 'THE HOME DEPOT' THEN '50802'
WHEN 'FRED MEYER' THEN '50802'
WHEN 'MENARDS' THEN '50802'
WHEN 'KROGER' THEN '50802'
WHEN 'OCEAN STATE JOBBERS' THEN '50802'
WHEN 'AURORA WHOLESALE' THEN '50802'
WHEN 'LEON KORRAL' THEN '50802'
--all other retail goes to Doran Marable-----------
ELSE '50200'
END
END
--minor group b52 goes to dedicated rep
ELSE
CASE WHEN MING = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN BILL_CLASS IN ('GDIR','NDIR') THEN
BILL_REP
ELSE
SHIP_REP
END
END
END
) QUOTA_REP
FROM
RLARP.OSMFS_DEV S
LEFT OUTER JOIN LGDAT.CUST ON
BVCUST = BILL_CUST
WHERE
COALESCE(GLEC,'') IS NOT NULL
) CR
WHERE
CR.VERSION = S.VERSION
AND CR.GLEC = COALESCE(S.GLEC,'')
AND CR.MING = COALESCE(S.MING,'')
AND CR.BILL_CUST = S.BILL_CUST
AND CR.SHIP_CUST = S.SHIP_CUST
AND COALESCE(S.DSM,'') <> CR.QUOTA_REP;

View File

@ -1,30 +0,0 @@
UPDATE
rlarp.osmfs_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;

View File

@ -1,9 +0,0 @@
------------update fx rates-----------------------------------------------------------------
UPDATE rlarp.osmf_dev SET r_rate = .7518 WHERE r_currency = 'CA' AND version = 'b21';
UPDATE rlarp.osmf_dev SET r_rate = 1.0000 WHERE r_currency = 'US' AND version = 'b21';
UPDATE rlarp.osmf_dev SET c_rate = .7518 WHERE c_currency = 'CA' AND version = 'b21';
UPDATE rlarp.osmf_dev SET c_rate = 1.0000 WHERE c_currency = 'US' AND version = 'b21';
UPDATE rlarp.osm_pool SET r_rate = .7518 WHERE r_currency = 'CA' AND version = 'b21';
UPDATE rlarp.osm_pool SET value_usd = value_loc * r_rate WHERE r_currency = 'CA' AND version = 'b21';
UPDATE rlarp.osm_pool SET c_rate = .7518 WHERE c_currency = 'CA' AND version = 'b21';
UPDATE rlarp.osm_pool SET cost_usd = cost_loc * c_rate WHERE c_currency = 'CA' AND version = 'b21';

View File

@ -1,27 +0,0 @@
UPDATE
RLARP.OSMFS_DEV O
SET
COLC = M.COLC
,COLGRP = M.COLGRP
,COLTIER = M.COLTIER
,COLSTAT = M.COLSTAT
,SIZC = M.SIZC
,PCKG = M.PACKAGE
,KIT = M.KIT
,BRND = M.BRANDING
,MAJG = M.MAJG
,MING = M.MING
,MAJS = M.MAJS
,MINS = M.MINS
,GLDC = M.GLCD
,GLEC = M.GLEC
,HARM = M.HARM
,CLSS = M.CLSS
,BRAND = M.BRAND
,ASSC = M.ASSC
,LBS = CASE M.NWUN WHEN 'KG' THEN 2.2046 ELSE 1 END*M.NWHT
,UNTI = M.UNTI
FROM
RLARP.ITEMM M
WHERE
M.ITEM = O.PART

View File

@ -1,299 +0,0 @@
--\timing
truncate table rlarp.osmfs_dev;
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
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
,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
,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
,ship_cust
,null::text ship_rep
,null::text ship_terr
,null::text 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
,'15mo' "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 BETWEEN '2019-06-01' AND '2020-05-31'
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2020-05-31')
OR o.fspr BETWEEN '2001' AND '2012'
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
AND version = 'ACTUALS'
AND dsm = 'PW'
GROUP BY
o.fspr
,plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,ship_cust
,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' --incremented
,o.ddqdat + interval '1 year' --incremented
,o.dcmdat
,o.fesdat
,o.dhidat + interval '1 year' --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' --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
,'b21' "version"
,'copy' iter
FROM
baseline o
LEFT OUTER JOIN gld ON
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
)
INSERT INTO rlarp.osmfs_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;

View File

@ -1,19 +0,0 @@
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,5 +1,11 @@
BEGIN; BEGIN;
--only do this when starting fresh
TRUNCATE TABLE rlarp.osm_pool;
TRUNCATE TABLE rlarp.osm_log CASCADE;
ALTER SEQUENCE rlarp.osm_log_id_seq RESTART 1;
DROP TABLE IF EXISTS rlarp.osm_pool_stage; DROP TABLE IF EXISTS rlarp.osm_pool_stage;
CREATE TABLE rlarp.osm_pool_stage AS ( CREATE TABLE rlarp.osm_pool_stage AS (
@ -16,6 +22,7 @@ repc AS (
WHERE WHERE
C.A2 = 'MM' C.A2 = 'MM'
) )
--select * from repc
,SEG AS ( ,SEG AS (
SELECT SELECT
GLEC GLEC
@ -40,17 +47,22 @@ repc AS (
('9TO','Other') ('9TO','Other')
) X(GLEC, SEGM) ) X(GLEC, SEGM)
) )
--select * from seg
----------------------------------------------------sales major codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS ( ,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
) )
--select * from sj
----------------------------------------------------sales minor codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS ( ,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
) )
--select * from si
,logload AS ( ,logload AS (
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 * INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','1+11','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
) )
--select * from logload
,loadset AS (
SELECT SELECT
fspr fspr
,plnt ,plnt
@ -168,10 +180,12 @@ FROM
seg.glec = o.glec seg.glec = o.glec
LEFT OUTER JOIN rlarp.itemm i ON LEFT OUTER JOIN rlarp.itemm i ON
i.item = o.part i.item = o.part
--LIMIT 100 )
SELECT * FROM loadset
) WITH DATA; ) WITH DATA;
--INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
--CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); --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); --ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);

View File

@ -1,183 +0,0 @@
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;

267
build/build_pullforward.sql Normal file
View File

@ -0,0 +1,267 @@
/*
you have to be able to isolate the original plan in order for this to work
if you start with a clean 1+11 its not going to work
this script targets the 'plan' iteration and the 'diff' iteration
if plan orders have already been 'diffed' no need to build an additional diff
will have to be side-by-side roll-up of these two iterations
*/
--BEGIN;
--\timing
--CREATE TABLE IF NOT EXISTS rlarp.osmf_stage AS (SELECT * FROM rlarp.osmf_dev) WITH no data;
--will not match actuals exactly due to short-ships
WITH
---these dates will have to be syncronized with the forecast build-----------
tdr AS (
SELECT
-----------actuals into baseline-------------------------------------
DATERANGE('2020-06-01','2021-07-12','[]') arange
-----------plan into baseline----------------------------------------
,DATERANGE('2021-06-01','2022-06-01','[)') prange
)
,booked AS materialized(
SELECT
billto_group
,shipto_group
,part_group
,sum(units) units
FROM
rlarp.osm_pool
WHERE
--find actual orders in the overlap range (where orders fall in both target ranges)
order_date <@ (SELECT arange FROM tdr)
AND order_date <@ (SELECT prange FROM tdr)
--anything that falls in this range is supposed to already match actuals, so no need to filter for actuals
GROUP BY
billto_group
,shipto_group
,part_group
)
,planned AS (
SELECT
p.billto_group
,p.shipto_group
,p.part_group
,p.order_date
,p.ship_date
,sum(p.units) FILTER (WHERE iter = 'plan') plan_units
,sum(p.units) FILTER (WHERE iter = 'diff') diff_units
,b.units booked
FROM
rlarp.osm_pool p
INNER JOIN booked b ON
b.billto_group = p.billto_group AND
b.shipto_group = p.shipto_group AND
b.part_group = p.part_group
WHERE
--need to be able to isolate the original budget
--budget orders have already been trued up to match actual, so should not be included in the this isolation
--find actual orders in the overlap range (where orders fall in both target ranges)
p.order_date <@ (SELECT prange FROM tdr)
AND p.iter IN ('plan','diff')
GROUP BY
p.billto_group
,p.shipto_group
,p.part_group
,p.order_date
,p.ship_date
,b.units
)
,plan_ranked AS (
SELECT
p.billto_group
,p.shipto_group
,p.part_group
,p.order_date
,p.ship_date
,round(p.plan_units,2) plan_units
,round(SUM(p.plan_units) FILTER (WHERE order_date >= (SELECT upper(arange) FROM tdr)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date),2) plan_rolling
,round(SUM(p.plan_units) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ),2) plan_units_tot
--,round(SUM(COALESCE(p.plan_units,0) + COALESCE(p.diff_units,0)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group ORDER BY p.order_date, p.ship_date),2) net_units_agg
,round(COALESCE(p.diff_units,0) + coalesce(p.plan_units,0),2) net_units
,round(SUM(COALESCE(p.plan_units,0) + COALESCE(p.diff_units,0)) OVER (PARTITION BY p.billto_group, p.shipto_group, p.part_group),2) net_units_tot
--,round(p.booked,2) booked
FROM
planned p
)
---flag any rows where
,eval AS (
SELECT
p.billto_group
,p.shipto_group
,p.part_group
,p.order_date
,p.ship_date
,p.plan_units
,p.plan_rolling
,p.plan_units_tot
--,p.net_units_agg
,p.net_units_tot
,p.net_units
--,p.booked
,plan_rolling <= (p.net_units_tot - p.plan_units_tot + plan_units) flag
--,CASE WHEN (p.plan_units_agg - p.plan_units) > p.booked AND p.plan_units_agg <= p.booked * 2 THEN true ELSE false END remove
FROM
plan_ranked p
)
--select * from eval limit 10000
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"force accounts to match target total value and margin",
"tag":"retail plug",
"type":"build"
}$$::jsonb doc
RETURNING *
)
,remove AS (
SELECT
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,round(-sum(o.units ),2) units
,round(-sum(o.value_loc),2) value_loc
,round(-sum(o.value_usd),2) value_usd
,round(-sum(o.cost_loc ),2) cost_loc
,round(-sum(o.cost_usd ),2) cost_usd
,o.calc_status
,o.flag
,o.order_date
,o.order_month
,o.order_season
,o.request_date
,o.request_month
,o.request_season
,o.ship_date
,o.ship_month
,o.ship_season
,'1+11' AS version
,'pf' iter
--,log.id
--,coalescE(log.doc->>'tag','') AS "tag"
--,log.doc->>'message' AS "comment"
--,log.doc->>'type' module
FROM
rlarp.osm_pool o
INNER JOIN eval e ON
e.billto_group = o.billto_group AND
e.shipto_group = o.shipto_group AND
e.part_group = o.part_group AND
e.order_date = o.order_date AND
e.ship_date = o.ship_date
--CROSS JOIN log
WHERE
e.flag
GROUP BY
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status
,o.flag
,o.order_date
,o.order_month
,o.order_season
,o.request_date
,o.request_month
,o.request_season
,o.ship_date
,o.ship_month
,o.ship_season
--,log.id
--,coalescE(log.doc->>'tag','') AS "tag"
--,log.doc->>'message' AS "comment"
--,log.doc->>'type' module
-----some bad forecast swings, if one is removed it's wacky
-----cap at -1,000,000
HAVING
round(-sum(o.value_loc),2) > -1000000
)
INSERT INTO rlarp.osm_pool SELECT * FROM remove;
-------sorted pull-forward scenarios------
--SELECT
-- billto_group,
-- shipto_group,
-- part_group,
-- order_date,
-- ship_date,
-- SUM(value_usd)
--FROM
-- remove
--GROUP BY
-- billto_group,
-- shipto_group,
-- part_group,
-- order_date,
-- ship_date
--ORDER BY
-- sum(value_usd) asc
--LIMIT 1000
-----pull-forward by month-----
--SELECT
-- order_season,
-- order_month,
-- SUM(value_usd)
--FROM
-- remove
--GROUP BY
-- order_season
-- order_month
--ROLLBACK;

1056
build/build_rolling.sql Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +0,0 @@
SELECT
order_season
,sum(coalesce(value_usd,0)) value_usd
FROM
rlarp.osm_pool
WHERE
order_season = 2022
--AND version = 'b21'
GROUP BY
order_season
UNION ALL
SELECT
oseas
,sum(fb_val_loc * r_rate) value_usd
FROM
rlarp.osmf_dev
WHERE
oseas = 2022
AND version = 'b22'
GROUP BY
oseas

View File

@ -1,22 +0,0 @@
SELECT
ship_season
,sum(value_usd) value_usd
FROM
rlarp.osm_pool
WHERE
ship_season = 2021
--AND version = 'b21'
GROUP BY
ship_season
UNION ALL
SELECT
sseas
,sum(fb_val_loc * r_rate) value_usd
FROM
rlarp.osmf_dev
WHERE
sseas = 2021
--AND version = 'b21'
GROUP BY
sseas

View File

@ -1,428 +0,0 @@
BEGIN;
DELETE FROM rlarp.osmfs_dev;
INSERT INTO
rlarp.osmfs_dev
SELECT
null::int4,
null::int4,
null::int4,
null::int4,
null::int4,
null::int4,
null::int4,
null::int4,
order_date,
request_date,
null::date,
null::date,
ship_date,
null::text,
null::text,
fspr,
null::numeric,
null::numeric,
null::numeric,
null::numeric,
null::numeric,
null::numeric,
null::jsonb,
null::text,
plnt,
promo,
null::text,
terms,
null::text,
null::text,
null::text,
rtrim(substring(bill_cust_descr,1,8)),
null::text,
null::text,
null::text,
rtrim(substring(ship_cust_descr,1,8)),
null::text,
null::text,
dsm,
billto_group,
shipto_group,
null::text,
chan,
chan,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
part,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::text,
null::numeric,
null::numeric,
null::text,
fs_line,
r_currency,
r_rate,
c_currency,
c_rate,
units,
value_loc,
null::numeric,
null::numeric,
null::numeric,
null::numeric,
cost_loc,
null::numeric,
null::numeric,
calc_status,
flag,
order_date,
order_season,
request_date,
request_season,
ship_date,
ship_season,
version,
iter
FROM
rlarp.osm_pool
WHERE
iter IN ('adj price','adj volume');
WITH
plist AS (
SELECT DISTINCT
part
,plnt
FROM
rlarp.osmfS_dev
)
,clist AS (
SELECT
p.part
,p.plnt
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs) stdcost
FROM
plist p
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = p.part
AND im.cgplnt = p.plnt
LEFT OUTER JOIN lgdat.icstp ip ON
ip.chpart = p.part
AND ip.chplnt = p.plnt
LEFT OUTER JOIN lgdat.icstr ir ON
ir.y0part = p.part
AND ir.y0plnt = p.plnt
)
UPDATE
rlarp.osmfs_dev o
SET
fb_cst_loc_cur = c.stdcost * o.fb_qty
FROM
clist c
WHERE
c.part = o.part
AND c.plnt = o.plnt;
----------------------------SET BILL-TO REP------------------------------------
UPDATE
RLARP.OSMFS_DEV S
SET
BILL_REP = C.BVSALM
,BILL_CLASS = C.BVCLAS
,BILL_TERR = C.BVTERR
,BILL_CTRY = C.bvctry
,bill_prov = C.bvprcd
,bill_post = C.bvpost
,remit_to = c.bvcomp
,ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = S.BILL_CUST
AND (
COALESCE(S.BILL_REP,'') <> C.BVSALM
OR COALESCE(S.BILL_CLASS,'') <> C.BVCLAS
OR COALESCE(S.BILL_TERR,'') <> C.BVTERR
OR COALESCE(BILL_CTRY,'') <> C.bvctry
OR COALESCE(bill_prov,'') <> C.bvprcd
OR COALESCE(bill_post,'') <> C.bvpost
OR COALESCE(remit_to,'') <> c.bvcomp::text
);
----------------------------SET SHIP-TO REP------------------------------------
UPDATE
RLARP.OSMFS_DEV S
SET
SHIP_REP = C.BVSALM
,SHIP_CLASS = C.BVCLAS
,SHIP_TERR = C.BVTERR
,dest_CTRY = C.bvctry
,dest_prov = C.bvprcd
,dest_post = C.bvpost
FROM
LGDAT.CUST C
WHERE
C.BVCUST = S.SHIP_CUST
AND (
COALESCE(S.SHIP_REP,'') <> C.BVSALM
OR COALESCE(S.SHIP_CLASS,'') <> C.BVCLAS
OR COALESCE(S.SHIP_TERR,'') <> C.BVTERR
OR COALESCE(dest_CTRY,'') <> C.bvctry
OR COALESCE(dest_prov,'') <> C.bvprcd
OR COALESCE(dest_post,'') <> C.bvpost
);
----------------------------SET BILLTO GROUP------------------------------------
UPDATE
RLARP.OSMFS_DEV O
SET
ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = O.BILL_CUST
AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END;
----------------------------SET SHIPTO GROUP------------------------------------
UPDATE
RLARP.OSMFS_DEV O
SET
SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
LGDAT.CUST C
WHERE
C.BVCUST = O.SHIP_CUST
AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,'');
---------------------------SET CHANNEL-----------------------------------------
UPDATE
rlarp.osmFS_dev
SET
CHAN = CASE SUBSTRING(BILL_CLASS,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END,
CHANSUB = CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END
WHERE
COALESCE(CHAN,'') <> CASE SUBSTRING(BILL_CLASS,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END
OR
COALESCE(CHANSUB,'') <> CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END;
---------------------------SET QUOTA REP---------------------------------------
UPDATE
rlarp.osmfs_dev s
SET
dsm = cr.quota_rep
FROM
(
SELECT DISTINCT
VERSION,
COALESCE(GLEC,'') GLEC,
MING,
BILL_CUST,
SHIP_CUST,
------------quota rep column--------------
CASE WHEN COALESCE(ming,'') = 'B52' THEN 'PW' ELSE
--if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available
CASE WHEN COALESCE(glec,'') = '1RE' AND COALESCE(cu.currep,'') <> '' THEN
cu.currep
--default logic
ELSE
CASE SUBSTR(bill_class,2,3)
WHEN 'DIS' THEN
ship_rep
ELSE
bill_rep
END
END
END QUOTA_REP
FROM
rlarp.osmfs_dev s
LEFT OUTER JOIN lgdat.cust ON
bvcust = bill_cust
LEFT OUTER JOIN lgpgm.usrcust cu ON
cu.cucust = s.bill_cust
WHERE
version = 'ACTUALS'
) CR
WHERE
CR.VERSION = S.VERSION
AND CR.GLEC = COALESCE(S.GLEC,'')
AND CR.MING = S.MING
AND CR.BILL_CUST = S.BILL_CUST
AND CR.SHIP_CUST = S.SHIP_CUST
AND COALESCE(S.DSM,'') <> CR.QUOTA_REP;
-------------------set fiscal period--------------------------------------
UPDATE
rlarp.osmfs_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;
UPDATE
rlarp.osmfS_dev
SET
r_rate = .7900
WHERE
r_currency = 'CA';
UPDATE
rlarp.osmfS_dev
SET
r_rate = 1
WHERE
r_currency = 'US';
UPDATE
rlarp.osmfS_dev
SET
c_rate = .7900
WHERE
c_currency = 'CA';
UPDATE
rlarp.osmfS_dev
SET
c_rate = 1
WHERE
c_currency = 'US';
UPDATE
RLARP.OSMFS_DEV O
SET
COLC = M.COLC
,COLGRP = M.COLGRP
,COLTIER = M.COLTIER
,COLSTAT = M.COLSTAT
,SIZC = M.SIZC
,PCKG = M.PACKAGE
,KIT = M.KIT
,BRND = M.BRANDING
,MAJG = M.MAJG
,MING = M.MING
,MAJS = M.MAJS
,MINS = M.MINS
,GLDC = M.GLCD
,GLEC = M.GLEC
,HARM = M.HARM
,CLSS = M.CLSS
,BRAND = M.BRAND
,ASSC = M.ASSC
,LBS = CASE M.NWUN WHEN 'KG' THEN 2.2046 ELSE 1 END*M.NWHT
,UNTI = M.UNTI
FROM
RLARP.ITEMM M
WHERE
M.ITEM = O.PART;
DELETE FROM rlarp.osmf_dev WHERE iter IN ('adj price','adj volume');
INSERT INTO rlarp.osmf_dev SELECT * FROM rlarp.osmfs_dev;
COMMIT;

111
build/convert_to_budget.sql Normal file
View File

@ -0,0 +1,111 @@
INSERT INTO
rlarp.osmp_dev
SELECT
"ddord#"
,"dditm#"
,"fgbol#"
,"fgent#"
,"diinv#"
,"dilin#"
,quoten
,quotel
,dcodat
,ddqdat
,dcmdat
,fesdat
,dhidat
,fesind
,dhpost
,fspr
,ddqtoi
,ddqtsi
,fgqshp
,diqtsh
,diext
,ditdis
,discj
,dhincr
,plnt
,promo
,return_reas
,terms
,custpo
,remit_to
,bill_class
,bill_cust
,bill_rep
,bill_terr
,ship_class
,ship_cust
,ship_rep
,ship_terr
,dsm
,account
,shipgrp
,geo
,chan
,chansub
,orig_ctry
,orig_prov
,orig_post
,bill_ctry
,bill_prov
,bill_post
,dest_ctry
,dest_prov
,dest_post
,part
,styc
,colc
,colgrp
,coltier
,colstat
,sizc
,pckg
,kit
,brnd
,majg
,ming
,majs
,mins
,gldco
,gldc
,glec
,harm
,clss
,brand
,assc
,ddunit
,unti
,lbs
,plt
,plcd
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,fb_qty
,fb_val_loc
,fb_val_loc_dis
,fb_val_loc_qt
,fb_val_loc_pl
,fb_val_loc_tar
,fb_cst_loc
,fb_cst_loc_cur
,fb_cst_loc_fut
,calc_status
,flag
,odate
,oseas
,rdate
,rseas
,sdate
,sseas
,'b22'
,iter || ' - ' || tag iter
FROM
rlarp.osmfs_dev
WHERE
oseas = 2022
OR sseas = 2022;

View File

@ -1,33 +0,0 @@
begin;
WITH d AS (
DELETE FROM
rlarp.osm_pool
WHERE
logid IN (
SELECT
p.logid
--jsonb_agg(DISTINCT p.quota_rep_descr),
--l.doc->>'user' usr,
--l.doc->>'message' com,
--(l.doc->>'stamp')::timestamp stamp,
,sum(value_usd) value_usd
FROM
rlarp.osm_pool p
INNER JOIN rlarp.osm_log l ON
l.id = p.logid
WHERE
l.doc->>'user' = 'Baggetta, Antony'
AND dsm NOT IN ('10023','10026','10032','30010','30033','30035','90050')
--quota_rep_descr IN ('JORDAN BUKOWSKI' ,'RICHARD MEULE')
AND logid > 1
GROUP BY
p.logid
--p.quota_rep_descr,
--l.doc->>'user',
--l.doc->>'message',
--(l.doc->>'stamp')::timestamp
)
AND dsm NOT IN ('10023','10026','10032','30010','30033','30035','90050')
)
SELECT * FROM d

View File

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

View File

@ -1,164 +0,0 @@
BEGIN;
DELETE FROM rlarp.osm_pool WHERE order_date < '2021-05-01';
INSERT INTO rlarp.osm_pool
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
('1SU','Sustainable'),
('1CU','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,'') <> ''
)
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
WHEN 'Retail' THEN
CASE o.bill_class
WHEN 'RMAS' THEN 'MASS'
WHEN 'RNAT' THEN 'NATIONAL'
ELSE 'OTHER'
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
INNER JOIN rlarp.osm_log logload ON
logload.id = 1
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
WHERE
odate < '2021-05-01';
SELECT
"version",
iter,
order_season,
order_month ,
sum(value_usd ) value_usd
FROM
RLARP.osm_pool op
WHERE
--oseas = 2020
fs_line = '41010'
--AND odate < '2020-04-01'
AND CALC_STATUS <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
GROUP BY
"version",
iter,
order_season,
order_month;
--ROLLBACK;
commit;

View File

@ -1,187 +0,0 @@
--\timing
truncate table rlarp.osmfs_dev;
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
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
,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
,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
,ship_cust
,null::text ship_rep
,null::text ship_terr
,null::text 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 r_rate
,o.c_currency
,o.c_rate 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
,'15mo' "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 <@ daterange('2020-06-01','2021-05-01','[)')
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2021-05-01')
--...or anything that shipped in that period
OR o.fspr BETWEEN '2101' AND '2111'
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
AND "version" = 'ACTUALS'
GROUP BY
o.fspr
,plnt
,COALESCE(o.promo,'')
,o.terms
,o.bill_cust
,ship_cust
,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
)
INSERT INTO rlarp.osmfs_dev
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;

View File

@ -1,111 +0,0 @@
WITH
targ AS (
SELECT * FROM (VALUES
('Pierre','Nursery','HYDROFARM',267888.16),
('Pierre','Nursery','ALTMAN PLANTS',71626.22),
('Pierre','Nursery','RS GROWERS SUPPLY',17154.0599999999),
('Pierre','Nursery','MID-AMERICAN GROWERS',51663.75),
('Pierre','Nursery','KAWAHARA NURSERY',48859.2),
('Baggetta','Nursery','PACIFIC NURSERY POTS',194412.359999999),
('Baggetta','Nursery','FARRAND',171375.300000001),
('Baggetta','Nursery','ALPHA FOLIAGE',59200.45),
('Baggetta','Nursery','HOLMBERG FARMS INC',52911.6),
('Baggetta','Nursery','MATSUDAS BY GREEN ACRES LLC',46723.47),
('Baggetta','Nursery','GREENLEAF NURSERY CO',24829.2),
('Baggetta','Nursery','GREENHOUSE MEGASTORE',22552.0399999999),
('Baggetta','Nursery','HICKORY HILL NURSERY',11072.7),
('Baggetta','Nursery','BRANTLEY NURSERIES',9762.47999999998),
('Baggetta','Nursery','DALLAS JOHNSON GREENHOUSE',8229.6),
('Baggetta','Nursery','BAMA GREEN',1165.5),
('Vander Deen','Greenhouse','HJS WHOLESALE LTD',310027.34),
('Vander Deen','Greenhouse','DEGOEYS NURSERY FLOWERS',26858.558706),
('Vander Deen','Greenhouse','DEVRY GREENHOUSES',10713.15),
('Vander Deen','Greenhouse','BIOFLORAL INC',9192.59000000001),
('Baggetta','Greenhouse','COSTA',600234.409999999),
('Baggetta','Greenhouse','SPARETIME SUPPLY',37966.8499999999),
('Baggetta','Greenhouse','ALPHA FOLIAGE',32183.66),
('Baggetta','Greenhouse','CASSCO',24248.74)
) x(director,glec,account,amount)
)
,sdate AS (
SELECT
targ.director
,targ.glec
,targ.account
,targ.amount
,p.order_season
,p.order_date
,p.ship_date
,p.ship_season
,p.part
,sum(p.value_usd) value_usd
FROM
rlarp.osm_pool p
INNER JOIN targ ON
p.billto_group = targ.account
AND p.segm = targ.glec
AND p.director = targ.director
WHERE
--order_season = 2021
ship_season = 2021
GROUP BY
targ.director
,targ.glec
,targ.account
,targ.amount
,p.order_season
,p.order_date
,p.ship_date
,p.ship_season
,p.part
)
,rev AS (
SELECT
director
,glec
,account
,amount
,order_season
,order_date
,part
,ship_date
,ship_season
,(ship_date + INTERVAL '1 year')::date rev_date
,gld.ssyr
,gld.fspr
,value_usd
,sum(value_usd) OVER (PARTITION BY director, glec, account, amount ORDER BY ship_date DESC, part) agg
,row_number() OVER (PARTITION BY director, glec, account, amount ORDER BY ship_date DESC) rn
,CASE WHEN sum(value_usd) OVER (PARTITION BY director, glec, account, amount ORDER BY ship_date DESC, part) >= amount THEN true ELSE false END flag
FROM
sdate
LEFT OUTER JOIN rlarp.gld gld ON
gld.drange @> (ship_date + INTERVAL '1 year')::date
ORDER BY
director
,glec
,account
,order_season
,amount
,ship_date DESC
)
,rnk AS (
SELECT
director,glec ,account
,min(rn) FILTER (WHERE flag = true) mflag
FROM
rev
GROUP BY
director,glec ,account ,account
)
,rejoin AS (
SELECT
rev.*
,rnk.mflag
,account || ' - '||to_char(amount,'FM999,999') goal
,rev.rn <= rnk.mflag AS include
FROM
rnk
NATURAL JOIN rev
)
SELECT * FROM rejoin

View File

@ -1,8 +0,0 @@
SELECT
ship_cust_descr, order_date, ship_date, sum(value_usd) sales
from
rlarp.osm_pool
where
ship_season = 2021
group by
ship_cust_descr, order_date, ship_date;

View File

@ -1,357 +0,0 @@
BEGIN;
WITH
move AS (
SELECT * FROM (VALUES
('HOME0004 - ALLIANCE INTERNATIONAL LLC',CAST('10/7/2019' AS DATE),CAST('6/1/2020' AS DATE),11322.85,CAST('7/2/2020' AS DATE)),
('CORS0004 - CORSOS PERENNIALS',CAST('10/23/2019' AS DATE),CAST('6/1/2020' AS DATE),30916.44,CAST('7/2/2020' AS DATE)),
('MONR0007 - MONROVIA - VISALIA CA',CAST('2/3/2020' AS DATE),CAST('6/1/2020' AS DATE),8409.6,CAST('7/2/2020' AS DATE)),
('BATT0001 - BATTLEFIELD FARMS',CAST('2/5/2020' AS DATE),CAST('6/1/2020' AS DATE),2285,CAST('7/2/2020' AS DATE)),
('BABI0001 - BABIKOW GREENHOUSES',CAST('2/7/2020' AS DATE),CAST('6/1/2020' AS DATE),9640.8,CAST('7/2/2020' AS DATE)),
('KALA0002 - KALAMAZOO FLOWER GROUP LLC',CAST('2/10/2020' AS DATE),CAST('6/1/2020' AS DATE),1005.4,CAST('7/2/2020' AS DATE)),
('HATR0001 - H.A. TREICHLER SONS',CAST('2/11/2020' AS DATE),CAST('6/1/2020' AS DATE),13425.44,CAST('7/2/2020' AS DATE)),
('KALA0002 - KALAMAZOO FLOWER GROUP LLC',CAST('3/13/2020' AS DATE),CAST('6/1/2020' AS DATE),2464,CAST('7/2/2020' AS DATE)),
('METR0002 - METROLINA GREENHOUSES',CAST('3/17/2020' AS DATE),CAST('6/1/2020' AS DATE),17619.12,CAST('7/2/2020' AS DATE)),
('JOHN0020 - JOHN BARONEDBA BARONE FARM',CAST('3/23/2020' AS DATE),CAST('6/1/2020' AS DATE),5571.85,CAST('7/2/2020' AS DATE)),
('JENS0002 - JENSEN DISTRIBUTION SERVICES',CAST('3/24/2020' AS DATE),CAST('6/1/2020' AS DATE),487.15,CAST('7/2/2020' AS DATE)),
('DIST0007 - TERIS SERVICES-PICK UP ONLY U',CAST('3/31/2020' AS DATE),CAST('6/1/2020' AS DATE),5318.78,CAST('7/2/2020' AS DATE)),
('DIST0007 - TERIS SERVICES-PICK UP ONLY U',CAST('4/7/2020' AS DATE),CAST('6/1/2020' AS DATE),400.28,CAST('7/2/2020' AS DATE)),
('BELA0001 - BELA FLOR NURSERIES',CAST('4/15/2020' AS DATE),CAST('6/1/2020' AS DATE),1219.71,CAST('7/2/2020' AS DATE)),
('JENS0002 - JENSEN DISTRIBUTION SERVICES',CAST('4/15/2020' AS DATE),CAST('6/1/2020' AS DATE),395.01,CAST('7/2/2020' AS DATE)),
('LLNU0003 - LL NURSERY SUPPLY INC WA',CAST('4/16/2020' AS DATE),CAST('6/1/2020' AS DATE),656.7,CAST('7/2/2020' AS DATE)),
('INTE0003 - GREENHOUSE MEGASTORE',CAST('4/23/2020' AS DATE),CAST('6/1/2020' AS DATE),283.06,CAST('7/2/2020' AS DATE)),
('CALC0004 - CAL COLOR GROWERS LLC',CAST('4/28/2020' AS DATE),CAST('6/1/2020' AS DATE),8273.25,CAST('7/2/2020' AS DATE)),
('BATT0001 - BATTLEFIELD FARMS',CAST('5/1/2020' AS DATE),CAST('6/1/2020' AS DATE),18919.26,CAST('7/2/2020' AS DATE)),
('LOWE3110 - LOWES OF MILTON CANADA # 3110',CAST('5/4/2020' AS DATE),CAST('6/1/2020' AS DATE),8901.3,CAST('7/2/2020' AS DATE)),
('SILV0007 - SILVERLEAF GHSE',CAST('5/6/2020' AS DATE),CAST('6/1/2020' AS DATE),19368.3,CAST('7/2/2020' AS DATE)),
('HOME0004 - ALLIANCE INTERNATIONAL LLC',CAST('5/11/2020' AS DATE),CAST('6/1/2020' AS DATE),6312.37,CAST('7/2/2020' AS DATE)),
('SHAD0009 - SHADY CREEK GREENHOUSES',CAST('5/12/2020' AS DATE),CAST('6/1/2020' AS DATE),1395.9,CAST('7/2/2020' AS DATE)),
('ISLA0008 - ISLAND GROWER SUPPLIES LLC',CAST('5/13/2020' AS DATE),CAST('6/1/2020' AS DATE),2160,CAST('7/2/2020' AS DATE)),
('LYSH0001 - LYS HYDRO GARDEN FRESNO',CAST('5/13/2020' AS DATE),CAST('6/1/2020' AS DATE),5383.08,CAST('7/2/2020' AS DATE)),
('LLNU0003 - LL NURSERY SUPPLY INC WA',CAST('5/13/2020' AS DATE),CAST('6/1/2020' AS DATE),667.89,CAST('7/2/2020' AS DATE)),
('STAN0009 - STANDEX ELECTRONICS',CAST('5/15/2020' AS DATE),CAST('6/1/2020' AS DATE),47.25,CAST('7/2/2020' AS DATE)),
('BRAD0020 - BRADFORD CO-OP STORAGE LTD (U)',CAST('5/15/2020' AS DATE),CAST('6/1/2020' AS DATE),460.8,CAST('7/2/2020' AS DATE)),
('CANA0034 - CANADIAN TIRE RETAIL DC 9004',CAST('5/16/2020' AS DATE),CAST('6/1/2020' AS DATE),4908.09,CAST('7/2/2020' AS DATE)),
('AJBI0001 - AJ BILLES DC CANADIAN TIRE',CAST('5/16/2020' AS DATE),CAST('6/1/2020' AS DATE),29620.77,CAST('7/2/2020' AS DATE)),
('LOWE3110 - LOWES OF MILTON CANADA # 3110',CAST('5/18/2020' AS DATE),CAST('6/1/2020' AS DATE),1595.47,CAST('7/2/2020' AS DATE)),
('BATT0001 - BATTLEFIELD FARMS',CAST('5/19/2020' AS DATE),CAST('6/1/2020' AS DATE),2088,CAST('7/2/2020' AS DATE)),
('HYDR0010 - HYDROFARM INC',CAST('5/20/2020' AS DATE),CAST('6/1/2020' AS DATE),22368.8,CAST('7/2/2020' AS DATE)),
('WILD0012 - WILDWOODS-PACIFIC NURSERY POTS',CAST('5/20/2020' AS DATE),CAST('6/1/2020' AS DATE),4180,CAST('7/2/2020' AS DATE)),
('GULF0002 - SUNBLEST GARDENS',CAST('5/20/2020' AS DATE),CAST('6/1/2020' AS DATE),21501.89,CAST('7/2/2020' AS DATE)),
('LOUI0001 - LOUIS DAVINO SR LLC',CAST('5/20/2020' AS DATE),CAST('6/1/2020' AS DATE),38150.76,CAST('7/2/2020' AS DATE)),
('DIST0007 - TERIS SERVICES-PICK UP ONLY U',CAST('5/21/2020' AS DATE),CAST('6/1/2020' AS DATE),3626.73,CAST('7/2/2020' AS DATE)),
('FRED0038 - FRED MEYER STORES',CAST('5/21/2020' AS DATE),CAST('6/1/2020' AS DATE),38706.76,CAST('7/2/2020' AS DATE)),
('LLNU0003 - LL NURSERY SUPPLY INC WA',CAST('5/21/2020' AS DATE),CAST('6/1/2020' AS DATE),3205.89,CAST('7/2/2020' AS DATE)),
('DOIT0008 - DO IT BEST CORP',CAST('5/21/2020' AS DATE),CAST('6/1/2020' AS DATE),551.34,CAST('7/2/2020' AS DATE)),
('BATT0001 - BATTLEFIELD FARMS',CAST('5/21/2020' AS DATE),CAST('6/1/2020' AS DATE),25150,CAST('7/2/2020' AS DATE)),
('AJBI0001 - AJ BILLES DC CANADIAN TIRE',CAST('5/23/2020' AS DATE),CAST('6/1/2020' AS DATE),339.94,CAST('7/2/2020' AS DATE)),
('AMAZ0033 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),998.94,CAST('7/2/2020' AS DATE)),
('AMAZ0134 - AMAZON LGB6',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),1058.85,CAST('7/2/2020' AS DATE)),
('AMAZ0115 - AMAZON.COM LGB4 LLC',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),743.79,CAST('7/2/2020' AS DATE)),
('AMAZ0093 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),3087.16,CAST('7/2/2020' AS DATE)),
('AMAZ0055 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),772.44,CAST('7/2/2020' AS DATE)),
('AMAZ0008 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),1735.38,CAST('7/2/2020' AS DATE)),
('AMAZ0123 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),720.9,CAST('7/2/2020' AS DATE)),
('AMAZ0156 - AMAZON - MEM1',CAST('5/25/2020' AS DATE),CAST('6/1/2020' AS DATE),750.12,CAST('7/2/2020' AS DATE)),
('FRED0038 - FRED MEYER STORES',CAST('5/26/2020' AS DATE),CAST('6/1/2020' AS DATE),4972.75,CAST('7/2/2020' AS DATE)),
('INTE0003 - GREENHOUSE MEGASTORE',CAST('5/26/2020' AS DATE),CAST('6/1/2020' AS DATE),16602.93,CAST('7/2/2020' AS DATE)),
('AMAZ0156 - AMAZON - MEM1',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),14.46,CAST('7/2/2020' AS DATE)),
('AMAZ0159 - AMAZON - SCK1',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('AMAZ0045 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('STON0028 - STONINGTON GARDENS',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),1370.99,CAST('7/2/2020' AS DATE)),
('AMAZ0083 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),130.14,CAST('7/2/2020' AS DATE)),
('AMAZ0073 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('AMAZ0012 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),28.92,CAST('7/2/2020' AS DATE)),
('AMAZ0127 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('AMAZ0093 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),14.46,CAST('7/2/2020' AS DATE)),
('AMAZ0043 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('AMAZ0140 - AMAZON - CLT3',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('AMAZ0114 - AMAZON.COM SERVICES,INC.',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),81.81,CAST('7/2/2020' AS DATE)),
('COPP0002 - COPPERSTATE FARMS MGMT LLC',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),16156.8,CAST('7/2/2020' AS DATE)),
('AMAZ0148 - AMAZON - IND9 9912622',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),14.46,CAST('7/2/2020' AS DATE)),
('AMAZ0144 - AMAZON - CMH3',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),27.27,CAST('7/2/2020' AS DATE)),
('INTE0003 - GREENHOUSE MEGASTORE',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),16810.8,CAST('7/2/2020' AS DATE)),
('DCME0009 - 9 DIS ASM DC 7103 MERCH WM',CAST('5/27/2020' AS DATE),CAST('6/1/2020' AS DATE),26970.81,CAST('7/2/2020' AS DATE)),
('AMAZ0019 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('AMAZ0155 - AMAZON - XIX7',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('AMAZ0107 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),65.4,CAST('7/2/2020' AS DATE)),
('AMAZ0099 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('AMAZ0108 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('AMAZ0090 - AMAZON.COM FULFILLMENT CTRCHH2',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('GOLD0038 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('7/2/2020' AS DATE)),
('GASK0001 - GASKO GREENHOUSES',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),39166.05,CAST('7/2/2020' AS DATE)),
('EAGL0001 - EAGLE CREEK WHOLESALE',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),489.5,CAST('8/2/2020' AS DATE)),
('AMAZ0123 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0127 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0029 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('DIST0007 - TERIS SERVICES-PICK UP ONLY U',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),19122.39,CAST('8/2/2020' AS DATE)),
('BEAU0002 - BEAUTIFUL LAND PRODUCTS',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),282.85,CAST('8/2/2020' AS DATE)),
('AMAZ0068 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0045 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0121 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0058 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('AMAZ0051 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('DEWA0001 - DEWAR NURSERIES',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),24181.3,CAST('8/2/2020' AS DATE)),
('AMAZ0091 - AMAZON.COM SERVICES,INC.',CAST('5/28/2020' AS DATE),CAST('6/1/2020' AS DATE),46.32,CAST('8/2/2020' AS DATE)),
('LUPI0001 - LUPINE LADY PORCUPINE GNHSE',CAST('5/29/2020' AS DATE),CAST('6/1/2020' AS DATE),112.42,CAST('8/2/2020' AS DATE)),
('YODE0010 - YODERS PRODUCE INC',CAST('5/29/2020' AS DATE),CAST('6/1/2020' AS DATE),228.75,CAST('8/2/2020' AS DATE)),
('JACK0020 - JACKSON COUNTY ARC INC',CAST('5/29/2020' AS DATE),CAST('6/1/2020' AS DATE),63.2,CAST('8/2/2020' AS DATE)),
('RAND0011 - RANDY WESTRATE GREENHOUSE',CAST('5/29/2020' AS DATE),CAST('6/1/2020' AS DATE),420.75,CAST('8/2/2020' AS DATE)),
('JOSE0006 - JOSE OCHOA',CAST('5/29/2020' AS DATE),CAST('6/1/2020' AS DATE),520.86,CAST('8/2/2020' AS DATE)),
('WERA0001 - WE RADTKE INC',CAST('5/30/2020' AS DATE),CAST('6/1/2020' AS DATE),1747.05,CAST('8/2/2020' AS DATE)),
('ERNS0003 - ERNST NURSERY',CAST('9/9/2019' AS DATE),CAST('6/2/2020' AS DATE),23493.6,CAST('8/2/2020' AS DATE)),
('SANM0002 - SAN MARCOS-AMER HORT SUPPLY',CAST('10/7/2019' AS DATE),CAST('6/2/2020' AS DATE),23118.67,CAST('8/2/2020' AS DATE)),
('CORS0004 - CORSOS PERENNIALS',CAST('10/23/2019' AS DATE),CAST('6/2/2020' AS DATE),18034.59,CAST('8/2/2020' AS DATE)),
('ALTM0034 - ALTMAN PLANTS - SALINAS',CAST('11/15/2019' AS DATE),CAST('6/2/2020' AS DATE),6092.7,CAST('8/2/2020' AS DATE)),
('ALTM0021 - ALTMANS SPECIALTY PLANTS PERRI',CAST('11/15/2019' AS DATE),CAST('6/2/2020' AS DATE),36831.64,CAST('8/2/2020' AS DATE)),
('WILL0064 - WILLOWBROOK NURSERIES INC',CAST('11/25/2019' AS DATE),CAST('6/2/2020' AS DATE),3474,CAST('8/2/2020' AS DATE)),
('CAVI0001 - CAVICCHIO GREENHOUSES INC',CAST('12/10/2019' AS DATE),CAST('6/2/2020' AS DATE),12716.82,CAST('8/2/2020' AS DATE)),
('ENVI0008 - EVL NURSERY',CAST('12/16/2019' AS DATE),CAST('6/2/2020' AS DATE),3551.31,CAST('8/2/2020' AS DATE)),
('MONR0018 - MONROVIA - DAYTON OR',CAST('1/6/2020' AS DATE),CAST('6/2/2020' AS DATE),29433.6,CAST('8/2/2020' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('1/30/2020' AS DATE),CAST('6/2/2020' AS DATE),924.93,CAST('8/2/2020' AS DATE)),
('METR0002 - METROLINA GREENHOUSES',CAST('2/11/2020' AS DATE),CAST('6/2/2020' AS DATE),13200,CAST('8/2/2020' AS DATE)),
('HEAR0011 - HEARTLAND-SUN WAREHOUSE24HR N',CAST('2/28/2020' AS DATE),CAST('6/2/2020' AS DATE),4442.04,CAST('8/2/2020' AS DATE)),
('FORE0005 - FOREST GLEN GREENHOUSES (C)',CAST('3/4/2020' AS DATE),CAST('6/2/2020' AS DATE),1747.44,CAST('8/2/2020' AS DATE)),
('VPNU0002 - VP NURSERY',CAST('3/6/2020' AS DATE),CAST('6/2/2020' AS DATE),19356.48,CAST('8/2/2020' AS DATE)),
('ACOS0002 - ACOSTA FARMS NURSERY INC',CAST('3/9/2020' AS DATE),CAST('6/2/2020' AS DATE),4391.78,CAST('8/2/2020' AS DATE)),
('ACOS0002 - ACOSTA FARMS NURSERY INC',CAST('3/10/2020' AS DATE),CAST('6/2/2020' AS DATE),8700,CAST('8/2/2020' AS DATE)),
('PLEA0026 - PLEASANT VALLEY FARM INC',CAST('3/11/2020' AS DATE),CAST('6/2/2020' AS DATE),731.2,CAST('8/2/2020' AS DATE)),
('ROCK0026 - ROCKWELL FARMS',CAST('3/16/2020' AS DATE),CAST('6/2/2020' AS DATE),15949.44,CAST('8/2/2020' AS DATE)),
('CLEA0015 - CLEARVIEW GREENHOUSES',CAST('3/16/2020' AS DATE),CAST('6/2/2020' AS DATE),6698.45,CAST('8/2/2020' AS DATE)),
('OLDN0001 - OLD NORTH GROWERS',CAST('3/16/2020' AS DATE),CAST('6/2/2020' AS DATE),7484.51,CAST('8/2/2020' AS DATE)),
('METR0002 - METROLINA GREENHOUSES',CAST('3/17/2020' AS DATE),CAST('6/2/2020' AS DATE),6767.1,CAST('8/2/2020' AS DATE)),
('BAMA0001 - BAMA GREEN',CAST('3/17/2020' AS DATE),CAST('6/2/2020' AS DATE),13264.85,CAST('8/2/2020' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('3/18/2020' AS DATE),CAST('6/2/2020' AS DATE),1234.9,CAST('8/2/2020' AS DATE)),
('TREE0044 - TREESAP FARMS-FOREST GROVE OR',CAST('3/24/2020' AS DATE),CAST('6/2/2020' AS DATE),3047.22,CAST('8/2/2020' AS DATE)),
('HEAR0011 - HEARTLAND-SUN WAREHOUSE24HR N',CAST('3/24/2020' AS DATE),CAST('6/2/2020' AS DATE),271,CAST('8/2/2020' AS DATE)),
('YOUN0005 - YOUNGS PLANT FARM INC - FARM 2',CAST('3/25/2020' AS DATE),CAST('6/2/2020' AS DATE),44807.6,CAST('8/2/2020' AS DATE)),
('YOUN0004 - YOUNGS PLANT FARM INC - FARM 1',CAST('3/25/2020' AS DATE),CAST('6/2/2020' AS DATE),27216.51,CAST('8/2/2020' AS DATE)),
('DIXI0001 - DIXIE GREEN INC.',CAST('3/26/2020' AS DATE),CAST('6/2/2020' AS DATE),8568,CAST('8/2/2020' AS DATE)),
('WILL0028 - WILLIAMS PLANT FARM',CAST('3/31/2020' AS DATE),CAST('6/2/2020' AS DATE),2812.5,CAST('8/2/2020' AS DATE)),
('PLEA0026 - PLEASANT VALLEY FARM INC',CAST('4/2/2020' AS DATE),CAST('6/2/2020' AS DATE),468.18,CAST('8/2/2020' AS DATE)),
('SPAR0001 - SPARETIME SUPPLY',CAST('4/2/2020' AS DATE),CAST('6/2/2020' AS DATE),17690.4,CAST('8/2/2020' AS DATE)),
('HYDR0010 - HYDROFARM INC',CAST('4/3/2020' AS DATE),CAST('6/2/2020' AS DATE),19445.93,CAST('8/2/2020' AS DATE)),
('CARL0001 - CARL BREHOB SON',CAST('4/10/2020' AS DATE),CAST('6/2/2020' AS DATE),450.6,CAST('8/2/2020' AS DATE)),
('ALPH0003 - ALPHA FOLIAGE',CAST('4/13/2020' AS DATE),CAST('6/2/2020' AS DATE),16207.16,CAST('8/2/2020' AS DATE)),
('FISH0009 - FISHER NURSERY INC - CA',CAST('4/13/2020' AS DATE),CAST('6/2/2020' AS DATE),2211.86,CAST('8/2/2020' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('4/14/2020' AS DATE),CAST('6/2/2020' AS DATE),2769.33,CAST('8/2/2020' AS DATE)),
('ROCK0026 - ROCKWELL FARMS',CAST('4/15/2020' AS DATE),CAST('6/2/2020' AS DATE),5061.84,CAST('8/2/2020' AS DATE)),
('SCOT0019 - SCOTTS NURSERY LTD (C)',CAST('4/17/2020' AS DATE),CAST('6/2/2020' AS DATE),18909.45,CAST('8/2/2020' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('4/17/2020' AS DATE),CAST('6/2/2020' AS DATE),172.4,CAST('8/2/2020' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('4/22/2020' AS DATE),CAST('6/2/2020' AS DATE),607.81,CAST('8/2/2020' AS DATE)),
('KMNU0003 - KM NURSERY',CAST('4/22/2020' AS DATE),CAST('6/2/2020' AS DATE),30531.6,CAST('8/2/2020' AS DATE)),
('SPAR0001 - SPARETIME SUPPLY',CAST('4/27/2020' AS DATE),CAST('6/2/2020' AS DATE),6633.9,CAST('2/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('4/28/2020' AS DATE),CAST('6/2/2020' AS DATE),8518.61,CAST('2/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('4/29/2020' AS DATE),CAST('6/2/2020' AS DATE),926.2,CAST('2/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('4/30/2020' AS DATE),CAST('6/2/2020' AS DATE),6024.27,CAST('2/2/2021' AS DATE)),
('AJBI0001 - AJ BILLES DC CANADIAN TIRE',CAST('5/2/2020' AS DATE),CAST('6/2/2020' AS DATE),752.43,CAST('2/2/2021' AS DATE)),
('CARL0001 - CARL BREHOB SON',CAST('5/6/2020' AS DATE),CAST('6/2/2020' AS DATE),627.25,CAST('2/2/2021' AS DATE)),
('ENVI0008 - EVL NURSERY',CAST('5/6/2020' AS DATE),CAST('6/2/2020' AS DATE),1587.84,CAST('2/2/2021' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('5/7/2020' AS DATE),CAST('6/2/2020' AS DATE),1734.3,CAST('2/2/2021' AS DATE)),
('LOWE3110 - LOWES OF MILTON CANADA # 3110',CAST('5/11/2020' AS DATE),CAST('6/2/2020' AS DATE),3066.42,CAST('2/2/2021' AS DATE)),
('BUCK0006 - BUCKHORN NURSERY',CAST('5/11/2020' AS DATE),CAST('6/2/2020' AS DATE),1804,CAST('2/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('5/12/2020' AS DATE),CAST('6/2/2020' AS DATE),73.8,CAST('2/2/2021' AS DATE)),
('WALD0003 - WALDO ASSOCIATES',CAST('5/13/2020' AS DATE),CAST('6/2/2020' AS DATE),5142.12,CAST('2/2/2021' AS DATE)),
('WHIT0003 - WHITES NURSERY',CAST('5/14/2020' AS DATE),CAST('6/2/2020' AS DATE),1859.68,CAST('2/2/2021' AS DATE)),
('MENA3339 - MENARDS INC',CAST('5/18/2020' AS DATE),CAST('6/2/2020' AS DATE),1974.54,CAST('2/2/2021' AS DATE)),
('WEST0646 - NUTRIEN AG SOL. - WATSONVILLE',CAST('5/18/2020' AS DATE),CAST('6/2/2020' AS DATE),9660.19,CAST('2/2/2021' AS DATE)),
('HOMU5089 - THE HOME DEPOT #5089 PICK UP',CAST('5/19/2020' AS DATE),CAST('6/2/2020' AS DATE),4166.76,CAST('2/2/2021' AS DATE)),
('HOMU5642 - THE HOME DEPOT #5642 PICK UP',CAST('5/19/2020' AS DATE),CAST('6/2/2020' AS DATE),7687.15,CAST('2/2/2021' AS DATE)),
('HOMU5030 - THE HOME DEPOT #5030 PICK UP',CAST('5/19/2020' AS DATE),CAST('6/2/2020' AS DATE),4395.64,CAST('2/2/2021' AS DATE)),
('HYDR0010 - HYDROFARM INC',CAST('5/20/2020' AS DATE),CAST('6/2/2020' AS DATE),12492.62,CAST('2/2/2021' AS DATE)),
('CARL0001 - CARL BREHOB SON',CAST('5/20/2020' AS DATE),CAST('6/2/2020' AS DATE),1747.28,CAST('2/2/2021' AS DATE)),
('HOMU5034 - THE HOME DEPOT #5034 PICK UP',CAST('5/21/2020' AS DATE),CAST('6/2/2020' AS DATE),18292.72,CAST('2/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('5/21/2020' AS DATE),CAST('6/2/2020' AS DATE),1104.45,CAST('2/2/2021' AS DATE)),
('WOOD0001 - WOODBURN NURSERY',CAST('5/21/2020' AS DATE),CAST('6/2/2020' AS DATE),6138,CAST('2/2/2021' AS DATE)),
('ROBE0014 - ROBERTAS YOUNG PLANTS',CAST('5/21/2020' AS DATE),CAST('6/2/2020' AS DATE),9819.29,CAST('2/2/2021' AS DATE)),
('HOMU5084 - THE HOME DEPOT #5084 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),6897.97,CAST('2/2/2021' AS DATE)),
('CASS0002 - CASSCO',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),20617.7,CAST('2/2/2021' AS DATE)),
('HOMU5085 - THE HOME DEPOT #5085 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),17337.57,CAST('2/2/2021' AS DATE)),
('HOMU5089 - THE HOME DEPOT #5089 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),9208.18,CAST('2/2/2021' AS DATE)),
('HOMU5643 - THE HOME DEPOT #5643 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),963.84,CAST('2/2/2021' AS DATE)),
('HOMU5642 - THE HOME DEPOT #5642 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),576.04,CAST('2/2/2021' AS DATE)),
('HOMU5520 - THE HOME DEPOT #5520 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),7713.04,CAST('2/2/2021' AS DATE)),
('HOMU5030 - THE HOME DEPOT #5030 PICK UP',CAST('5/22/2020' AS DATE),CAST('6/2/2020' AS DATE),5072.52,CAST('2/2/2021' AS DATE)),
('AMAZ0058 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/2/2020' AS DATE),578.27,CAST('2/2/2021' AS DATE)),
('LOWE3110 - LOWES OF MILTON CANADA # 3110',CAST('5/25/2020' AS DATE),CAST('6/2/2020' AS DATE),1637.71,CAST('2/2/2021' AS DATE)),
('AMAZ0006 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/2/2020' AS DATE),2438.8,CAST('2/2/2021' AS DATE)),
('AMAZ0085 - AMAZON.COM SERVICES,INC.',CAST('5/25/2020' AS DATE),CAST('6/2/2020' AS DATE),2800.81,CAST('2/2/2021' AS DATE)),
('HOMU5851 - THE HOME DEPOT #5851 PICK UP',CAST('5/26/2020' AS DATE),CAST('6/2/2020' AS DATE),19600.94,CAST('2/2/2021' AS DATE)),
('HOMU5520 - THE HOME DEPOT #5520 PICK UP',CAST('5/26/2020' AS DATE),CAST('6/2/2020' AS DATE),10885.09,CAST('5/2/2021' AS DATE)),
('HOMU5084 - THE HOME DEPOT #5084 PICK UP',CAST('5/26/2020' AS DATE),CAST('6/2/2020' AS DATE),3289.09,CAST('4/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('5/26/2020' AS DATE),CAST('6/2/2020' AS DATE),1422.36,CAST('4/2/2021' AS DATE)),
('BRAN0014 - BRANTLEY NURSERIES-BUSHNELL',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),9762.48,CAST('5/2/2021' AS DATE)),
('BWIC000G - BWI - SPRINGFIELD MO',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),12315.24,CAST('3/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),408,CAST('3/2/2021' AS DATE)),
('DIST0004 - TERIS SVSC DAPPROVISIONNEMNTC',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),1942.22,CAST('3/2/2021' AS DATE)),
('WALM602A - WALMART DC 6002A-ASM DIS',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),30551,CAST('3/2/2021' AS DATE)),
('CASS0002 - CASSCO',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),6635.15,CAST('3/2/2021' AS DATE)),
('HOMU5087 - THE HOME DEPOT #5087 PICK UP',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),12138.15,CAST('3/2/2021' AS DATE)),
('TJSG0001 - TJS GREENHOUSE LANDSCAPE',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),1085.19,CAST('3/2/2021' AS DATE)),
('HOMU5642 - THE HOME DEPOT #5642 PICK UP',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),4149.17,CAST('3/2/2021' AS DATE)),
('SCMD0001 - SCM DC 6063A-ASM DIS WM PICKUP',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),18407.75,CAST('3/2/2021' AS DATE)),
('HOMU5639 - THE HOME DEPOT #5639 PICK UP',CAST('5/27/2020' AS DATE),CAST('6/2/2020' AS DATE),4594.91,CAST('3/2/2021' AS DATE)),
('GRIF0001 - GRIFFIN GREENHOUSE - TEWKSBURY',CAST('5/28/2020' AS DATE),CAST('6/2/2020' AS DATE),660.9,CAST('3/2/2021' AS DATE)),
('CWKI0001 - CW KITCHENS INC',CAST('5/28/2020' AS DATE),CAST('6/2/2020' AS DATE),780.23,CAST('3/2/2021' AS DATE)),
('LLNU0001 - LL NURSERY SUPPLY INC CA',CAST('5/28/2020' AS DATE),CAST('6/2/2020' AS DATE),40536.37,CAST('3/2/2021' AS DATE)),
('ROSE0026 - ROSEVILLE FARMS',CAST('5/29/2020' AS DATE),CAST('6/2/2020' AS DATE),10254.6,CAST('3/2/2021' AS DATE)),
('PLEA0026 - PLEASANT VALLEY FARM INC',CAST('5/29/2020' AS DATE),CAST('6/2/2020' AS DATE),12979.2,CAST('3/2/2021' AS DATE)),
('BRAD0020 - BRADFORD CO-OP STORAGE LTD (U)',CAST('5/29/2020' AS DATE),CAST('6/2/2020' AS DATE),5908.07,CAST('3/2/2021' AS DATE)),
('GLOB0004 - GLOBAL HORTICULTURAL INC (C)',CAST('5/31/2019' AS DATE),CAST('6/3/2020' AS DATE),4159.13,CAST('3/2/2021' AS DATE)),
('GREE0092 - GREENSCAPE',CAST('8/13/2019' AS DATE),CAST('6/3/2020' AS DATE),565.45,CAST('3/2/2021' AS DATE)),
('QUAL0021 - QUALITY GHSE SITE 2',CAST('8/15/2019' AS DATE),CAST('6/3/2020' AS DATE),28746.9,CAST('3/2/2021' AS DATE)),
('MARK0020 - MARKET BASKET OF ST PETERS #6',CAST('9/5/2019' AS DATE),CAST('6/3/2020' AS DATE),89.16,CAST('3/2/2021' AS DATE)),
('TWIN0007 - TWIN OAKS GROWERS',CAST('9/9/2019' AS DATE),CAST('6/3/2020' AS DATE),2310,CAST('3/2/2021' AS DATE)),
('UCGM0001 - UGC - MARKET BASKET',CAST('9/17/2019' AS DATE),CAST('6/3/2020' AS DATE),133.77,CAST('3/2/2021' AS DATE)),
('DREA0005 - DREAMS WITH COLORS INC',CAST('10/3/2019' AS DATE),CAST('6/3/2020' AS DATE),11549.2,CAST('4/2/2021' AS DATE)),
('RSGR0004 - RS GROWERS SUPPLY INC',CAST('10/14/2019' AS DATE),CAST('6/3/2020' AS DATE),4339.84,CAST('4/2/2021' AS DATE)),
('WEAV0003 - WEAVERLAND VALLEY GREENHOUSE',CAST('10/18/2019' AS DATE),CAST('6/3/2020' AS DATE),1039.92,CAST('4/2/2021' AS DATE)),
('FOST0001 - FOSTERS INC RETAIL',CAST('10/22/2019' AS DATE),CAST('6/3/2020' AS DATE),1162.1,CAST('4/2/2021' AS DATE)),
('WETS0003 - BFG SUPPLY - HARRISONBURG(W13)',CAST('10/22/2019' AS DATE),CAST('6/3/2020' AS DATE),1788.07,CAST('4/2/2021' AS DATE)),
('BFGS0011 - BFG SUPPLY - ST PAUL',CAST('10/22/2019' AS DATE),CAST('6/3/2020' AS DATE),1448.7,CAST('4/2/2021' AS DATE)),
('BFGS0004 - BFG SUPPLY - GRAND RAPIDS(W04)',CAST('10/22/2019' AS DATE),CAST('6/3/2020' AS DATE),973.07,CAST('4/2/2021' AS DATE)),
('BFGS0015 - BFG SUPPLY - CHESWICK (W08)',CAST('10/23/2019' AS DATE),CAST('6/3/2020' AS DATE),2197.89,CAST('4/2/2021' AS DATE)),
('FOST0001 - FOSTERS INC RETAIL',CAST('10/25/2019' AS DATE),CAST('6/3/2020' AS DATE),1575.99,CAST('4/2/2021' AS DATE)),
('ARTK0002 - ART KNAPP - HESTIA PRODUCTIONS',CAST('10/28/2019' AS DATE),CAST('6/3/2020' AS DATE),610.56,CAST('4/2/2021' AS DATE)),
('EARL0012 - EARL MAY SEED NURSERY',CAST('10/28/2019' AS DATE),CAST('6/3/2020' AS DATE),174.23,CAST('4/2/2021' AS DATE)),
('DEGR0004 - DEGROOTS NURSERIES',CAST('10/31/2019' AS DATE),CAST('6/3/2020' AS DATE),205.12,CAST('4/2/2021' AS DATE)),
('PLAN0009 - PLANTERS SEED SPICE RET',CAST('11/1/2019' AS DATE),CAST('6/3/2020' AS DATE),234.61,CAST('4/2/2021' AS DATE)),
('CYGR0003 - CY GROWERS SUPPLIES LTD U',CAST('11/4/2019' AS DATE),CAST('6/3/2020' AS DATE),1566,CAST('4/2/2021' AS DATE)),
('BFGS0004 - BFG SUPPLY - GRAND RAPIDS(W04)',CAST('11/4/2019' AS DATE),CAST('6/3/2020' AS DATE),187.77,CAST('4/2/2021' AS DATE)),
('CISC0001 - CISCO COMPANIES (THE)',CAST('11/11/2019' AS DATE),CAST('6/3/2020' AS DATE),533.92,CAST('4/2/2021' AS DATE)),
('BERG0012 - BERGENS GREENHOUSE',CAST('11/12/2019' AS DATE),CAST('6/3/2020' AS DATE),84.46,CAST('4/2/2021' AS DATE)),
('BERG0029 - BERGEN NURSERY',CAST('11/12/2019' AS DATE),CAST('6/3/2020' AS DATE),759.73,CAST('4/2/2021' AS DATE)),
('GREE0223 - EDDIS WHOLESALE GDN SUPPLIES',CAST('11/13/2019' AS DATE),CAST('6/3/2020' AS DATE),87.58,CAST('4/2/2021' AS DATE)),
('KOLL0002 - KOLLMANS GREENHOUSE',CAST('11/15/2019' AS DATE),CAST('6/3/2020' AS DATE),174.67,CAST('4/2/2021' AS DATE)),
('ALTM0032 - ALTMAN PLANTS - TROUP',CAST('11/15/2019' AS DATE),CAST('6/3/2020' AS DATE),933,CAST('4/2/2021' AS DATE)),
('MISC0001 - MISC ACCT - (US )',CAST('11/18/2019' AS DATE),CAST('6/3/2020' AS DATE),0,CAST('4/2/2021' AS DATE)),
('DOIT0030 - DIB-ROLLIERS INC #4447',CAST('11/19/2019' AS DATE),CAST('6/3/2020' AS DATE),23.04,CAST('4/2/2021' AS DATE)),
('BWIC000T - BWI - TEXARKANA TX',CAST('11/20/2019' AS DATE),CAST('6/3/2020' AS DATE),21.77,CAST('4/2/2021' AS DATE)),
('GREE0005 - GREEN ISLAND DISTRIBUTORS INC.',CAST('11/20/2019' AS DATE),CAST('6/3/2020' AS DATE),156.56,CAST('4/2/2021' AS DATE)),
('MAPL0025 - MAPLE LEAF GARDEN',CAST('11/20/2019' AS DATE),CAST('6/3/2020' AS DATE),87.58,CAST('4/2/2021' AS DATE)),
('WYAT0007 - WYATT QUARLES SEED CO',CAST('11/21/2019' AS DATE),CAST('6/3/2020' AS DATE),506.32,CAST('4/2/2021' AS DATE)),
('BWIC000E - BWI - GREENVILLE-SPARTANBURG',CAST('11/22/2019' AS DATE),CAST('6/3/2020' AS DATE),662.73,CAST('4/2/2021' AS DATE)),
('HALI0002 - HALIFAX SEED DIST CENTER (C)',CAST('11/22/2019' AS DATE),CAST('6/3/2020' AS DATE),555.71,CAST('4/2/2021' AS DATE)),
('EDDI0001 - EDDIS WHOLESALE GDN SUPPLIES',CAST('12/2/2019' AS DATE),CAST('6/3/2020' AS DATE),86.01,CAST('4/2/2021' AS DATE)),
('MARI0030 - MARIA GARDENS',CAST('12/3/2019' AS DATE),CAST('6/3/2020' AS DATE),790.55,CAST('4/2/2021' AS DATE)),
('MARI0007 - MARIA GARDENS - N. ROYALTON',CAST('12/4/2019' AS DATE),CAST('6/3/2020' AS DATE),675.25,CAST('4/2/2021' AS DATE)),
('LLNU0003 - LL NURSERY SUPPLY INC WA',CAST('12/5/2019' AS DATE),CAST('6/3/2020' AS DATE),445.85,CAST('4/2/2021' AS DATE)),
('LLNU0001 - LL NURSERY SUPPLY INC CA',CAST('12/5/2019' AS DATE),CAST('6/3/2020' AS DATE),358.1,CAST('4/2/2021' AS DATE)),
('CAVI0001 - CAVICCHIO GREENHOUSES INC',CAST('12/6/2019' AS DATE),CAST('6/3/2020' AS DATE),2316.1,CAST('4/2/2021' AS DATE)),
('GRIF0010 - GRIFFIN GREENHOUSE - STAFFORD',CAST('12/6/2019' AS DATE),CAST('6/3/2020' AS DATE),527.51,CAST('4/2/2021' AS DATE)),
('BFGS0015 - BFG SUPPLY - CHESWICK (W08)',CAST('12/6/2019' AS DATE),CAST('6/3/2020' AS DATE),1640.54,CAST('4/2/2021' AS DATE)),
('COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)',CAST('12/6/2019' AS DATE),CAST('6/3/2020' AS DATE),18673.2,CAST('4/2/2021' AS DATE)),
('BFGS0015 - BFG SUPPLY - CHESWICK (W08)',CAST('12/9/2019' AS DATE),CAST('6/3/2020' AS DATE),716.74,CAST('4/2/2021' AS DATE)),
('BFGS0004 - BFG SUPPLY - GRAND RAPIDS(W04)',CAST('12/9/2019' AS DATE),CAST('6/3/2020' AS DATE),521.93,CAST('4/2/2021' AS DATE)),
('HORI0005 - HORIZON DISTRIBUTION INC',CAST('12/10/2019' AS DATE),CAST('6/3/2020' AS DATE),133.47,CAST('4/2/2021' AS DATE)),
('CARL0002 - CARLIN HORTICULTURAL SUPP U',CAST('12/10/2019' AS DATE),CAST('6/3/2020' AS DATE),155.55,CAST('4/2/2021' AS DATE)),
('CAVI0001 - CAVICCHIO GREENHOUSES INC',CAST('12/10/2019' AS DATE),CAST('6/3/2020' AS DATE),1695.58,CAST('4/2/2021' AS DATE)),
('CARL0001 - CARL BREHOB SON',CAST('12/12/2019' AS DATE),CAST('6/3/2020' AS DATE),478.87,CAST('4/2/2021' AS DATE)),
('BWIC000G - BWI - SPRINGFIELD MO',CAST('12/12/2019' AS DATE),CAST('6/3/2020' AS DATE),68.23,CAST('4/2/2021' AS DATE)),
('GARD0095 - GARD N WISE SALT LAKE',CAST('12/12/2019' AS DATE),CAST('6/3/2020' AS DATE),214.58,CAST('4/2/2021' AS DATE)),
('GARD0122 - GARD N WISE DENVER',CAST('12/12/2019' AS DATE),CAST('6/3/2020' AS DATE),632.41,CAST('4/2/2021' AS DATE)),
('STRA0022 - STRADERS GARDEN SUPPLY',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),186.94,CAST('4/2/2021' AS DATE)),
('STRA0020 - STRADERS GARDEN CENTER #1',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),290.19,CAST('4/2/2021' AS DATE)),
('PLRO0001 - PL ROHRER BRO INC RET',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),21.77,CAST('4/2/2021' AS DATE)),
('STRA0001 - STRADERS GREENHOUSE-POWELL',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),119.62,CAST('4/2/2021' AS DATE)),
('STRA0023 - STRADERS GARDEN SUPPLY',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),186.94,CAST('4/2/2021' AS DATE)),
('STRA0002 - STRADERS GREENHOUSE-GROVE CITY',CAST('12/13/2019' AS DATE),CAST('6/3/2020' AS DATE),186.94,CAST('4/2/2021' AS DATE)),
('LUCA0001 - LUCAS GREENHOUSE',CAST('12/16/2019' AS DATE),CAST('6/3/2020' AS DATE),101062.73,CAST('4/2/2021' AS DATE)),
('ENVI0008 - EVL NURSERY',CAST('12/16/2019' AS DATE),CAST('6/3/2020' AS DATE),22023.66,CAST('4/2/2021' AS DATE)),
('WILD0012 - WILDWOODS-PACIFIC NURSERY POTS',CAST('12/16/2019' AS DATE),CAST('6/3/2020' AS DATE),14853.15,CAST('4/2/2021' AS DATE)),
('APAC0001 - APACHE SEEDS LTD',CAST('12/17/2019' AS DATE),CAST('6/3/2020' AS DATE),663.82,CAST('4/2/2021' AS DATE)),
('GALE0003 - GALES GARDEN CENTER',CAST('12/18/2019' AS DATE),CAST('6/3/2020' AS DATE),622.16,CAST('4/2/2021' AS DATE)),
('OTTO0004 - OTTEN BROS NRSY LANDSCPG INC',CAST('12/19/2019' AS DATE),CAST('6/3/2020' AS DATE),211.66,CAST('4/2/2021' AS DATE)),
('INTE0014 - INTERMOUNTAIN FARMERS ASSN',CAST('12/20/2019' AS DATE),CAST('6/3/2020' AS DATE),87.46,CAST('4/2/2021' AS DATE)),
('GARD0095 - GARD N WISE SALT LAKE',CAST('12/20/2019' AS DATE),CAST('6/3/2020' AS DATE),71.63,CAST('4/2/2021' AS DATE)),
('GRIF0002 - GRIFFIN GREENHOUSE - SCHENECTA',CAST('12/26/2019' AS DATE),CAST('6/3/2020' AS DATE),1849.79,CAST('4/2/2021' AS DATE)),
('COST0007 - COSTA COLOR-MIAMI FL',CAST('1/8/2020' AS DATE),CAST('6/3/2020' AS DATE),15629.4,CAST('4/2/2021' AS DATE)),
('COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)',CAST('1/9/2020' AS DATE),CAST('6/3/2020' AS DATE),6652.8,CAST('4/2/2021' AS DATE)),
('COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)',CAST('1/10/2020' AS DATE),CAST('6/3/2020' AS DATE),944,CAST('4/2/2021' AS DATE)),
('FOST0001 - FOSTERS INC RETAIL',CAST('1/13/2020' AS DATE),CAST('6/3/2020' AS DATE),888.04,CAST('4/2/2021' AS DATE)),
('INTE0003 - GREENHOUSE MEGASTORE',CAST('1/13/2020' AS DATE),CAST('6/3/2020' AS DATE),368.52,CAST('4/2/2021' AS DATE)),
('JOSE0009 - JOSEPH GEREMIA FARM',CAST('1/13/2020' AS DATE),CAST('6/3/2020' AS DATE),1739.3,CAST('4/2/2021' AS DATE)),
('COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)',CAST('1/14/2020' AS DATE),CAST('6/3/2020' AS DATE),5026.94,CAST('4/2/2021' AS DATE)),
('RITC0001 - RITCHIE FEED SEED C',CAST('1/15/2020' AS DATE),CAST('6/3/2020' AS DATE),1.1,CAST('4/2/2021' AS DATE)),
('KUBE0001 - KUBE PAK CORPORATION',CAST('1/16/2020' AS DATE),CAST('6/3/2020' AS DATE),874.5,CAST('4/2/2021' AS DATE)),
('FRYS0001 - FRYSVILLE FARMS (MILES FRY)',CAST('1/17/2020' AS DATE),CAST('6/3/2020' AS DATE),1857.6,CAST('4/2/2021' AS DATE)),
('PRIN0001 - PRINCE CORPORATION',CAST('1/20/2020' AS DATE),CAST('6/3/2020' AS DATE),211.16,CAST('4/2/2021' AS DATE)),
('GALE0006 - GALES GARDEN CENTER',CAST('1/20/2020' AS DATE),CAST('6/3/2020' AS DATE),19.47,CAST('4/2/2021' AS DATE)),
('CKGR0004 - CK GREENHOUSES',CAST('1/20/2020' AS DATE),CAST('6/3/2020' AS DATE),4442.04,CAST('4/2/2021' AS DATE)),
('MATS0002 - MATSUDAS OF SACRAMENTO',CAST('1/20/2020' AS DATE),CAST('6/3/2020' AS DATE),57056.64,CAST('5/2/2021' AS DATE)),
('GRIF0005 - GRIFFIN GREENHOUSE - AUBURN',CAST('1/21/2020' AS DATE),CAST('6/3/2020' AS DATE),7399.49,CAST('5/2/2021' AS DATE)),
('GRIF0014 - GRIFFIN GREENHOUSE - MORGANTWN',CAST('1/21/2020' AS DATE),CAST('6/3/2020' AS DATE),16031.53,CAST('5/2/2021' AS DATE)),
('GRIF0004 - GRIFFIN GREENHOUSE - CHESHIRE',CAST('1/21/2020' AS DATE),CAST('6/3/2020' AS DATE),6166.24,CAST('5/2/2021' AS DATE)),
('GROW0001 - GROWER DIRECT FARMS INC',CAST('1/24/2020' AS DATE),CAST('6/3/2020' AS DATE),1199.44,CAST('5/2/2021' AS DATE)),
('CHAR0009 - CHARLES SPITZNER',CAST('1/24/2020' AS DATE),CAST('6/3/2020' AS DATE),7350,CAST('5/2/2021' AS DATE)),
('KURT0018 - KURTZ FARMS',CAST('1/27/2020' AS DATE),CAST('6/3/2020' AS DATE),3689.69,CAST('5/2/2021' AS DATE)),
('PARK0004 - PARKS BROTHERS FARMS',CAST('1/27/2020' AS DATE),CAST('6/3/2020' AS DATE),2250,CAST('5/2/2021' AS DATE)),
('JAYW0001 - JAY W GUILD',CAST('1/28/2020' AS DATE),CAST('6/3/2020' AS DATE),8388.9,CAST('5/2/2021' AS DATE)),
('VAND0001 - VANDENBERG BULB CO INC',CAST('1/29/2020' AS DATE),CAST('6/3/2020' AS DATE),70.35,CAST('5/2/2021' AS DATE)),
('LITT0001 - LITTLE MOUNTAIN GREENHOUSES U',CAST('2/3/2020' AS DATE),CAST('6/3/2020' AS DATE),61.92,CAST('5/2/2021' AS DATE)),
('JIFF0006 - JIFFY PRODUCTS OF AMERICA',CAST('2/4/2020' AS DATE),CAST('6/3/2020' AS DATE),16652.25,CAST('5/2/2021' AS DATE)),
('RSGR0004 - RS GROWERS SUPPLY INC',CAST('2/6/2020' AS DATE),CAST('6/3/2020' AS DATE),0.02,CAST('5/2/2021' AS DATE)),
('SCHL0001 - SCHLEGEL GRHSE',CAST('2/6/2020' AS DATE),CAST('6/3/2020' AS DATE),3646.58,CAST('5/2/2021' AS DATE)),
('VANW0013 - VAN WINGERDEN GREENHOUSE LLC',CAST('2/7/2020' AS DATE),CAST('6/3/2020' AS DATE),537.3,CAST('5/2/2021' AS DATE)),
('RITC0001 - RITCHIE FEED SEED C',CAST('2/7/2020' AS DATE),CAST('6/3/2020' AS DATE),179.46,CAST('5/2/2021' AS DATE)),
('LONG0002 - LONGFELLOWS GREENHOUSE',CAST('2/7/2020' AS DATE),CAST('6/3/2020' AS DATE),5536.48,CAST('5/2/2021' AS DATE)),
('AAPL0001 - AA PLANTS INC',CAST('2/7/2020' AS DATE),CAST('6/3/2020' AS DATE),3843,CAST('5/2/2021' AS DATE)),
('BFGS0004 - BFG SUPPLY - GRAND RAPIDS(W04)',CAST('2/10/2020' AS DATE),CAST('6/3/2020' AS DATE),553.3,CAST('5/2/2021' AS DATE)),
('KALA0002 - KALAMAZOO FLOWER GROUP LLC',CAST('2/10/2020' AS DATE),CAST('6/3/2020' AS DATE),502.7,CAST('5/2/2021' AS DATE)),
('DOIT0079 - DO IT BEST CORP',CAST('2/10/2020' AS DATE),CAST('6/3/2020' AS DATE),494.4,CAST('5/2/2021' AS DATE)),
('DOIT0004 - DO IT BEST CORP',CAST('2/11/2020' AS DATE),CAST('6/3/2020' AS DATE),82.4,CAST('5/2/2021' AS DATE)),
('GRIF0002 - GRIFFIN GREENHOUSE - SCHENECTA',CAST('2/12/2020' AS DATE),CAST('6/3/2020' AS DATE),376.6,CAST('5/2/2021' AS DATE)),
('NATU0027 - NATURALLY BEAUTIFUL PLANT PROD',CAST('2/12/2020' AS DATE),CAST('6/3/2020' AS DATE),31636.8,CAST('5/2/2021' AS DATE)),
('FARM0012 - FARMETTE GARDENS',CAST('2/13/2020' AS DATE),CAST('6/3/2020' AS DATE),6028.37,CAST('5/2/2021' AS DATE)),
('SPOR0002 - SPORTEL GREENHOUSE',CAST('2/13/2020' AS DATE),CAST('6/3/2020' AS DATE),3728.54,CAST('5/2/2021' AS DATE)),
('ARET0001 - ARETT SALES - BRISTOL',CAST('2/14/2020' AS DATE),CAST('6/3/2020' AS DATE),266.62,CAST('5/2/2021' AS DATE)),
('GREE0005 - GREEN ISLAND DISTRIBUTORS INC.',CAST('2/14/2020' AS DATE),CAST('6/3/2020' AS DATE),21.21,CAST('5/2/2021' AS DATE)),
('FARM0001 - FARMERS COOPERATIVE ELEVATOR',CAST('2/17/2020' AS DATE),CAST('6/3/2020' AS DATE),495,CAST('5/2/2021' AS DATE)),
('VANW0009 - VAN WINGERDEN INTERNATIONAL',CAST('2/17/2020' AS DATE),CAST('6/3/2020' AS DATE),908.6,CAST('5/2/2021' AS DATE)),
('CIVA0001 - CIVANO NURSERY',CAST('2/18/2020' AS DATE),CAST('6/3/2020' AS DATE),50700,CAST('5/2/2021' AS DATE))
) x(ship_cust_descr,order_date,ship_date,sales,revised)
)
,upd AS (
UPDATE
rlarp.osm_pool p
SET
ship_date = revised
FROM
move
WHERE
move.ship_cust_descr = p.ship_cust_descr
AND move.order_date = p.order_date
AND move.ship_date = p.ship_date
AND p.ship_season = 2021
RETURNING *
)
SELECT ship_month ,sum(value_usd) FROM upd group by ship_month;
UPDATE
rlarp.osm_pool
SET
ship_month = gld.sspr || ' - ' || to_char(ship_date,'FMMon')
,ship_season = gld.ssyr
,fspr = gld.fspr
FROM
rlarp.gld gld
WHERE
ship_date BETWEEN gld.sdat AND gld.edat
RETURNING *;
--ROLLBACK;
--SELECT
-- move.revised
-- ,sum(sales)
-- ,sum(value_usd)
--FROM
-- rlarp.osm_pool p
-- INNER JOIN move ON
-- move.ship_cust_descr = p.ship_cust_descr
-- AND move.order_date = p.order_date
-- AND move.ship_date = p.ship_date
--WHERE
-- ship_season = 2021
--GROUP BY
-- move.revised

View File

@ -1,5 +0,0 @@
$PG -f ./build_stage.sql
$PG -f ./snap_itemm.sql
$PG -f ./snap_cost_current.sql
$PG -f ./snap_customer.sql
$PG -f ./build_pool.sql

7
build/rebuild_script.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
#$PG -f ./build_stage.sql;
$PG -f ./build_rolling.sql;
$PG -f ./snap_itemm.sql;
$PG -f ./snap_cost_current.sql;
$PG -f ./snap_customer.sql;
$PG -f ./build_pool.sql;

View File

@ -1,33 +0,0 @@
SELECT
oseas
,go.drange
,go.sspr
,sum(fb_val_loc * r_rate)
FROM
rlarp.osm_dev o
--snap the ship dates of the historic fiscal period
--LEFT OUTER JOIN rlarp.gld ON
-- gld.fspr = o.fspr
LEFT OUTER JOIN rlarp.gld go ON
odate between go.sdat and go.edat
--get the shipping season for open orders based on the snapped date
--LEFT OUTER JOIN rlarp.gld ss ON
-- greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
--base period orders booked....
o.odate BETWEEN '2020-06-01' AND '2021-04-04'
----...or any open orders currently booked before cutoff....
--OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2021-04-04')
----...or anything that shipped in that period
--OR o.fspr BETWEEN '2101' AND '2110'
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
--AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
---exclude integrated quotes----
AND version = 'ACTUALS'
GROUP BY
oseas
,go.drange
,go.sspr

View File

@ -1,413 +0,0 @@
with
SEG AS (
SELECT
GLEC
,SEGM
FROM
(
VALUES
('1CU','Retail'),
('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)
)
,fp as (
SELECT
od.part,
sum(value_usd) sales,
sum(cost_usd) scost
FROM
rlarp.osm_pool od
LEFT OUTER JOIN rlarp.itemmv m ON
m.item = od.part
WHERE
m.item is null
AND coalesce(od.part,'') <> ''
GROUP BY
od.part
)
,repl AS (
SELECT * FROM (VALUES
('ANP00001A39','ANP00001A39C003LRDBP','New 2020'),
('ANP12000A39','ANP12000A39C001LRCOM','New 2020'),
('ANP15000A39','ANP15000A39C001LRCON','New 2020'),
('ANP17500A39','ANP17500A39C001LRCOO','New 2020'),
('ARP06000DE3','ARP06000DE3C012LRDBQ','New 2020'),
('ARP06000P26','ARP06000P26C012LRDBR','New 2020'),
('ARP06000P80','ARP06000P80C012LRDBS','New 2020'),
('ARP06000P81','ARP06000P81C012LRDBT','New 2020'),
('ARP08000DE3','ARP08000DE3C012LRDBU','New 2020'),
('ARP08000P26','ARP08000P26C012LRDBV','New 2020'),
('ARP08000P80','ARP08000P80C012LRDBW','New 2020'),
('ARP08000P81','ARP08000P81C012LRDBX','New 2020'),
('ARP10000DE3','ARP10000DE3C010LRDBY','New 2020'),
('ARP10000P26','ARP10000P26C010LRDBZ','New 2020'),
('ARP10000P80','ARP10000P80C010LRDCA','New 2020'),
('ARP10000P81','ARP10000P81C010LRDCB','New 2020'),
('ARP12000DE3','ARP12000DE3C010LRDCC','New 2020'),
('ARP12000P26','ARP12000P26C010LRDCD','New 2020'),
('ARP12000P80','ARP12000P80C010LRDCE','New 2020'),
('ARP12000P81','ARP12000P81C010LRDCF','New 2020'),
('ARP16000DE3','ARP16000DE3C008LRDCG','New 2020'),
('ARP16000P26','ARP16000P26C008LRDCH','New 2020'),
('ARP16000P80','ARP16000P80C008LRDCI','New 2020'),
('ARP16000P81','ARP16000P81C008LRDCJ','New 2020'),
('ASP06000DE3','ASP06000DE3C012LRDCK','New 2020'),
('ASP06000P26','ASP06000P26C012LRDCL','New 2020'),
('ASP06000P80','ASP06000P80C012LRDCM','New 2020'),
('ASP06000P81','ASP06000P81C012LRDCN','New 2020'),
('ASP08000DE3','ASP08000DE3C012LRDCO','New 2020'),
('ASP08000P26','ASP08000P26C012LRDCP','New 2020'),
('ASP08000P80','ASP08000P80C012LRDCQ','New 2020'),
('ASP08000P81','ASP08000P81C012LRDCR','New 2020'),
('ASP10000DE3','ASP10000DE3C010LRDCS','New 2020'),
('ASP10000P26','ASP10000P26C010LRDCT','New 2020'),
('ASP10000P80','ASP10000P80C010LRDCU','New 2020'),
('ASP10000P81','ASP10000P81C010LRDCV','New 2020'),
('ASP12000DE3','ASP12000DE3C010LRDCW','New 2020'),
('ASP12000P26','ASP12000P26C010LRDCX','New 2020'),
('ASP12000P80','ASP12000P80C010LRDCY','New 2020'),
('ASP12000P81','ASP12000P81C010LRDCZ','New 2020'),
('ASP16000DE3','ASP16000DE3C008LRDDB','New 2020'),
('ASP16000P26','ASP16000P26C008LRDDC','New 2020'),
('ASP16000P80','ASP16000P80C008LRDDE','New 2020'),
('ASP16000P81','ASP16000P81C008LRDDF','New 2020'),
('CAR12000P26','CAR13000P261004LRDBE','New 2020'),
('CAR12000P80','CAR13000P801004LRDBF','New 2020'),
('CAR15000P26','CAR16000P261004LRDBG','New 2020'),
('CAR15000P80','CAR16000P801004LRDBH','New 2020'),
('CAS12000P26','CAS12000P261004LRDBI','New 2020'),
('CAS12000P80','CAS12000P801004LRDBJ','New 2020'),
('CAS15000P26','CAS15000P261004LRDBK','New 2020'),
('CAS15000P80','CAS15000P801004LRDBL','New 2020'),
('CNA06000A10','CNA06000A10C024LRCJJ','New 2020'),
('CNA06000A42','CNA06000A42C024LRCDC','New 2020'),
('CNA06000BG3','CNA06000BG3C024LRCJK','New 2020'),
('CNA06000F89','CNA06000F89C024LRCJL','New 2020'),
('CNA08000A10','CNA08000A10C024LRCJM','New 2020'),
('CNA08000A42','CNA08000A42C024LRBXH','New 2020'),
('CNA08000BG3','CNA08000BG3C024LRCJN','New 2020'),
('CNA08000F89','CNA08000F89C024LRCJO','New 2020'),
('CNA10000A10','CNA10000A10C012LRCJP','New 2020'),
('CNA10000BG3','CNA10000BG3C012LRCJQ','New 2020'),
('CNA10000F89','CNA10000F89C012LRCJR','New 2020'),
('CNA12000A10','CNA12000A10C012LRCJS','New 2020'),
('CNA12000BG3','CNA12000BG3C012LRCJT','New 2020'),
('CNA12000F89','CNA12000F89C012LRCJU','New 2020'),
('ECA06000B78','ECA06000B781028LRCJV','New 2020'),
('ECA06000DE2','ECA06000DE21028LRCJW','New 2020'),
('ECA08000B78','ECA08000B781024LRCJX','New 2020'),
('ECA08000DE2','ECA08000DE21024LRCJY','New 2020'),
('ECA10000B78','ECA10000B781020LRCJZ','New 2020'),
('ECA10000DE2','ECA10000DE21020LRCKA','New 2020'),
('ECA12000B78','ECA12000B781016LRCKB','New 2020'),
('ECA12000DE2','ECA12000DE21016LRCKC','New 2020'),
('ECA16000B78','ECA16000B781010LRCKD','New 2020'),
('ECA16000DE2','ECA16000DE21010LRCKE','New 2020'),
('ECA20000B78','ECA20000B781006LRCKF','New 2020'),
('ECA20000DE2','ECA20000DE21006LRCKG','New 2020'),
('GAB12000BG3','GAB12000BG3C012LRCKH','New 2020'),
('GAB12000DE2','GAB12000DE2C012LRCKI','New 2020'),
('GAB14000BG3','GAB14000BG3C012LRCKJ','New 2020'),
('GAB14000DE2','GAB14000DE2C012LRCKK','New 2020'),
('GAB18000BG3','GAB18000BG3C006LRCKL','New 2020'),
('GAB18000DE2','GAB18000DE2C006LRCKM','New 2020'),
('GAB22000BG3','GAB22000BG3C006LRCKN','New 2020'),
('GAB22000DE2','GAB22000DE2C006LRCKO','New 2020'),
('GDP08000BG3','GDP08000BG3C012LRCOJ','New 2020'),
('GDP08000DE2','GDP08000DE2C012LRCOK','New 2020'),
('LIA06000B78','LIA06000B78C024LRCKP','New 2020'),
('LIA06000DE2','LIA06000DE2C024LRCKQ','New 2020'),
('LIA08000B78','LIA08000B78C024LRCKR','New 2020'),
('LIA08000DE2','LIA08000DE2C024LRCKS','New 2020'),
('LIA10000B78','LIA10000B78C012LRCKT','New 2020'),
('LIA10000DE2','LIA10000DE2C012LRCKU','New 2020'),
('LIA12000B78','LIA12000B78C012LRCKV','New 2020'),
('LIA12000DE2','LIA12000DE2C012LRCKW','New 2020'),
('LIA14000B78','LIA14000B78C012LRCKX','New 2020'),
('LIA14000DE2','LIA14000DE2C012LRCKY','New 2020'),
('LIA16000B78','LIA16000B78C012LRCKZ','New 2020'),
('LIA16000DE2','LIA16000DE2C012LRCLA','New 2020'),
('LIA20000B78','LIA20000B78C006LRCLB','New 2020'),
('LIA20000DE2','LIA20000DE2C006LRCLC','New 2020'),
('LIA24000B78','LIA24000B78C006LRCLD','New 2020'),
('LIA24000DE2','LIA24000DE2C006LRCLE','New 2020'),
('MSA12001A42','MSA12001A421008LRCLF','New 2020'),
('MSA12001B78','MSA12001B781008LRCLG','New 2020'),
('MSA12001DE2','MSA12001DE21008LRCLH','New 2020'),
('MSA16001A42','MSA16001A42C004LRCLI','New 2020'),
('MSA16001B78','MSA16001B78C004LRCLJ','New 2020'),
('MSA16001DE2','MSA16001DE2C004LRCLK','New 2020'),
('MSA20001A42','MSA20001A42C004LRCLL','New 2020'),
('MSA20001B78','MSA20001B78C004LRCLM','New 2020'),
('MSA20001DE2','MSA20001DE2C004LRCLN','New 2020'),
('SEA07001A42','SEA07001A421008LRCLP','New 2020'),
('SEA07001B78','SEA07001B781008LRCLQ','New 2020'),
('SEA07001DE2','SEA07001DE21008LRCLR','New 2020'),
('SEA09001A42','SEA09001A421008LRCLT','New 2020'),
('SEA09001B78','SEA09001B781008LRCLU','New 2020'),
('SEA09001DE2','SEA09001DE21008LRCLV','New 2020'),
('SEA12001A42','SEA12001A421006LRCLX','New 2020'),
('SEA12001B78','SEA12001B781006LRCLY','New 2020'),
('SEA12001DE2','SEA12001DE21006LRCLZ','New 2020'),
('SEA14001A42','SEA14001A421006LRCMB','New 2020'),
('SEA14001B78','SEA14001B781006LRCMC','New 2020'),
('SEA14001DE2','SEA14001DE21006LRCMD','New 2020'),
('SEA16000A42','SEA16001A421006LRCMF','New 2020'),
('SEA16000B78','SEA16001B781006LRCMG','New 2020'),
('SEA16000DE2','SEA16001DE21006LRCMH','New 2020'),
('SKR12000A56','SKR12000A56C010LRDDI','New 2020'),
('SKR12000E66','SKR12000E66C010LRDDJ','New 2020'),
('SKR12000G27','SKR12000G27C010LRDDK','New 2020'),
('SKR16000A56','SKR16000A56C008LRDDL','New 2020'),
('SKR16000E66','SKR16000E66C008LRDDM','New 2020'),
('SKR16000G27','SKR16000G27C008LRDDN','New 2020'),
('SKS12000A56','SKS12000A56C010LRDDO','New 2020'),
('SKS12000E66','SKS12000E66C010LRDDP','New 2020'),
('SKS12000G27','SKS12000G27C010LRDDQ','New 2020'),
('SKS16000A56','SKS16000A56C008LRDDR','New 2020'),
('SKS16000E66','SKS16000E66C008LRDDS','New 2020'),
('SKS16000G27','SKS16000G27C008LRDDT','New 2020'),
('SLI06000B78','SLI06000B78C024LRCMI','New 2020'),
('SLI06000DE2','SLI06000DE2C024LRCMJ','New 2020'),
('SLI08000B78','SLI08000B78C024LRCMK','New 2020'),
('SLI08000DE2','SLI08000DE2C024LRCML','New 2020'),
('SLI10000B78','SLI10000B78C012LRCMM','New 2020'),
('SLI10000DE2','SLI10000DE2C012LRCMN','New 2020'),
('SLI12000B78','SLI12000B78C012LRCMO','New 2020'),
('SLI12000DE2','SLI12000DE2C012LRCMP','New 2020'),
('SLI14000B78','SLI14000B78C012LRCMQ','New 2020'),
('SLI14000DE2','SLI14000DE2C012LRCMR','New 2020'),
('SLI16000B78','SLI17000B78C012LRCMS','New 2020'),
('SLI17000DE2','SLI17000DE2C012LRCMT','New 2020'),
('SLI20000B78','SLI20000B78C006LRCMU','New 2020'),
('SLI20000DE2','SLI20000DE2C006LRCMV','New 2020'),
('SLI24000B78','SLI24000B78C006LRCMW','New 2020'),
('SLI24000DE2','SLI24000DE2C006LRCMX','New 2020'),
('SVN18000A10','SVN18000A10C006LRCMY','New 2020'),
('SVN18000A42','SVN18000A42C006LRCMZ','New 2020'),
('SVN24000A10','SVN24000A10C006LRCNA','New 2020'),
('SVN24000A42','SVN24000A42C006LRCNB','New 2020'),
('SVN30000A10','SVN30000A10C006LRCNC','New 2020'),
('SVN30000A42','SVN30000A42C006LRCND','New 2020'),
('VNP18000A10','VNP18000A10C006LRCNE','New 2020'),
('VNP18000A42','VNP18000A42C006LRCNF','New 2020'),
('VNP24000A10','VNP24000A10C006LRCNG','New 2020'),
('VNP24000A42','VNP24000A42C006LRCNH','New 2020'),
('VNP30000A10','VNP30000A10C006LRCNI','New 2020'),
('VNP30000A42','VNP30000A42C006LRCNJ','New 2020'),
('WBP16000P82','WBP16000P82C008LRDDG','New 2020'),
('ROS15500A42','ROS15500A42C012LRDFA','New 2021'),
('SRO15500A42','SRO15500A42C012LRDFB','New 2021'),
('CSB06000A58','CSB06000A58C020LRDHO','New 2021'),
('CSB06000E67','CSB06000E67C020LRDHM','New 2021'),
('CSB06000G28','CSB06000G28C020LRDHN','New 2021'),
('CSB08000A58','CSB08000A58C020LRDHU','New 2021'),
('CSB08000E67','CSB08000E67C020LRDHS','New 2021'),
('CSB08000G28','CSB08000G28C020LRDHT','New 2021'),
('CNA06000A02','CNA06000A02C024LRDGU','New 2021'),
('CNA06000CXX','CNA06000CXXC024LRDGV','New 2021'),
('CNA06000DE1','CNA06000DE1C024LRDGW','New 2021'),
('CNA08000A02','CNA08000A02C024LRDGX','New 2021'),
('CNA08000CXX','CNA08000CXXC024LRDGY','New 2021'),
('CNA08000DE1','CNA08000DE1C024LRDGZ','New 2021'),
('CNA10000A02','CNA10000A02C012LRDHA','New 2021'),
('CNA10000CXX','CNA10000CXXC012LRDHB','New 2021'),
('CNA10000DE1','CNA10000DE1C012LRDHC','New 2021'),
('CNA12000A02','CNA12000A02C012LRDHD','New 2021'),
('CNA12000CXX','CNA12000CXXC012LRDHE','New 2021'),
('CNA12000DE1','CNA12000DE1C012LRDHF','New 2021'),
('DFB06000A58','DFB06000A58C024LRDHR','New 2021'),
('DFB06000E67','DFB06000E67C024LRDHP','New 2021'),
('DFB06000G28','DFB06000G28C024LRDHQ','New 2021'),
('DFB08000A58','DFB08000A58C024LRDHX','New 2021'),
('DFB08000E67','DFB08000E67C024LRDHV','New 2021'),
('DFB08000G28','DFB08000G28C024LRDHW','New 2021'),
('ECH12000A42','ECH12000A421010LRDFM','New 2021'),
('ECH12000E21','ECH12000E211010LRDFK','New 2021'),
('ECH12000G18','ECH12000G181010LRDFL','New 2021'),
('ECA06000A02','ECA06000A021028LRDEJ','New 2021'),
('ECA06000A42','ECA06000A421028LRDEI','New 2021'),
('ECA08000A02','ECA08000A021024LRDEL','New 2021'),
('ECA08000A42','ECA08000A421024LRDEK','New 2021'),
('ECA10000A02','ECA10000A021020LRDEM','New 2021'),
('ECA10000A42','ECA10000A421020LRDEN','New 2021'),
('ECA12000A02','ECA12000A021016LRDEO','New 2021'),
('ECA12000A42','ECA12000A421016LRDEP','New 2021'),
('ECA16000A02','ECA16000A021010LRDES','New 2021'),
('ECA16000A42','ECA16000A421010LRDET','New 2021'),
('ECA20000A02','ECA20000A021006LRDEW','New 2021'),
('ECA20000A42','ECA20000A421006LRDEX','New 2021'),
('ECI12000A42','ECI12000A421010LRDFP','New 2021'),
('ECI12000E21','ECI12000E211010LRDFN','New 2021'),
('ECI12000G18','ECI12000G181010LRDFO','New 2021'),
('ECP06000A02','ECP06000A021028LRDGK','New 2021'),
('ECP06000A42','ECP06000A421028LRDGM','New 2021'),
('ECP06000DE2','ECP06000DE21028LRDGL','New 2021'),
('ECP06000E35','ECP06000E351028LRDGJ','New 2021'),
('ECP06000G18','ECP06000G181028LRDGI','New 2021'),
('ECP08000A02','ECP08000A021024LRDGP','New 2021'),
('ECP08000A42','ECP08000A421024LRDGR','New 2021'),
('ECP08000DE2','ECP08000DE21024LRDGQ','New 2021'),
('ECP08000E35','ECP08000E351024LRDGO','New 2021'),
('ECP08000G18','ECP08000G181024LRDGN','New 2021'),
('ECP10000A02','ECP10000A021020LRDGA','New 2021'),
('ECP10000A42','ECP10000A421020LRDGC','New 2021'),
('ECP10000DE2','ECP10000DE21020LRDGB','New 2021'),
('ECP10000E35','ECP10000E351020LRDFZ','New 2021'),
('ECP10000G18','ECP10000G181020LRDFY','New 2021'),
('ECP12000A02','ECP12000A021016LRDGF','New 2021'),
('ECP12000A42','ECP12000A421016LRDGH','New 2021'),
('ECP12000DE2','ECP12000DE21016LRDGG','New 2021'),
('ECP12000E35','ECP12000E351016LRDGE','New 2021'),
('ECP12000G18','ECP12000G181016LRDGD','New 2021'),
('ECW24000A10','ECW24000A101006LRDFQ','New 2021'),
('ECW24000A42','ECW24000A421006LRDFT','New 2021'),
('ECW24000E35','ECW24000E351006LRDFS','New 2021'),
('ECW24000G18','ECW24000G181006LRDFR','New 2021'),
('ECW30000A10','ECW30000A101006LRDFU','New 2021'),
('ECW30000A42','ECW30000A421006LRDFX','New 2021'),
('ECW30000E35','ECW30000E351006LRDFW','New 2021'),
('ECW30000G18','ECW30000G181006LRDFV','New 2021'),
('GAB12000A42','GAB12000A42C012LRDFC','New 2021'),
('GAB14000A42','GAB14000A42C012LRDFD','New 2021'),
('GAB22000A42','GAB22000A42C006LRDFE','New 2021'),
('GDP08000A10','GDP08000A10C012LRDFF','New 2021'),
('HBM12000H13','HBM12000H136010LRDIB','New 2021'),
('HBM12000H26','HBM12000H226010LRDIC','New 2021'),
('HBM12000H22','HBM12000H266010LRDIA','New 2021'),
('PA.08005B71',CAST(NULL AS VARCHAR(255)),'New 2021'),
('PA.08005E22','PA.08005E22C250LYHQO','New 2021'),
('SPP08000A58','SPP08000A581004LRDHZ','New 2021'),
('SPP08000E67','SPP08000E671004LRDHY','New 2021'),
('PZA09000YB5','PZA09000YB5C012LRDFG','New 2021'),
('PZA12000YB5','PZA12000YB5C012LRDFH','New 2021'),
('PZA16000YB5','PZA16000YB5C006LRDFI','New 2021'),
('PZA20000YB5','PZA20000YB5C006LRDFJ','New 2021'),
('SSP02500A58','SSP02500A58C020LRDHI','New 2021'),
('SSP02500E67','SSP02500E67C020LRDHG','New 2021'),
('SSP02500G28','SSP02500G28C020LRDHH','New 2021'),
('SSP03000A58','SSP03000A58C020LRDHL','New 2021'),
('SSP03000E67','SSP03000E67C020LRDHJ','New 2021'),
('SSP03000G28','SSP03000G28C020LRDHK','New 2021'),
('RZ.MT160G18',CAST(NULL AS VARCHAR(255)),'New 2021'),
('RZ.MT160A42',CAST(NULL AS VARCHAR(255)),'New 2021'),
('RZ.MT160XXX',CAST(NULL AS VARCHAR(255)),'New 2021'),
('RZ.WC2G3B66','RZ.WC2G3B66C020LRCOT','New 2021')
) x(f11,fp,note)
)
, fin AS (
SELECT
fp.part,
fp.sales,
min(item) item,
repl.fp,
COALESCE(repl.fp,min(item)) pick
FROM
fp
LEFT OUTER JOIN rlarp.itemmv m ON
substring(m.item,1,11) = substring(fp.part,1,11)
LEFT OUTER JOIN repl ON
repl.f11 = substring(fp.part,1,11)
GROUP BY
fp.part,
fp.sales,
repl.fp
ORDER BY
fp.sales desc,
fp.part
)
--SELECT * FROM fin;
/*
,upd AS (
UPDATE
rlarp.osm_pool o
SET
part = fin.pick
FROM
fin
WHERE
fin.part = o.part
RETURNING *
)
SELECT * from upd;
*/
--SELECT * FROM rlarp.osm_pool o inner join fin on fin.part = o.part ;
select * from fin;
ROLLBACK;
/*
,ins AS (
SELECT
p.fspr
,p.plnt
,p.promo
,p.terms
,p.bill_cust_descr
,p.ship_cust_descr
,p.dsm
,p.quota_rep_descr
,p.director
,p.billto_group
,p.shipto_group
,p.chan
,p.chansub
,p.chan_retail
,fin.pick
,i.item||coalesce(' - '||i.descr,'') part_descr
,i.stlc part_group
,i.branding
,i.majg||' - '||i.majgd majg_descr
,i.ming||' - '||i.mingd ming_descr
,i.majs||' - '||i.majsd majs_descr
,i.mins||' - '||i.minsd mins_descr
,seg.segm
,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
,p.fs_line
,p.r_currency
,p.r_rate
,p.c_currency
,p.c_rate
,p.units
,p.value_loc
,p.value_usd
,p.cost_loc
,p.cost_usd
,p.calc_status
,p.flag
,p.order_date
,p.order_month
,p.order_season
,p.request_date
,p.request_month
,p.request_season
,p.ship_date + INTERVAL '1 year'
,ship_month
,ship_season
,'b21' AS version
,'fake parts' iter
,logl.id logid
,logl.doc->>'tag' tag
,logl.doc->>'message' "comment"
,logl.doc->>'type' module
FROM
rlarp.osm_pool p
INNER JOIN fin ON
fin.part = p.part
LEFT OUTER JOIN rlarp.itemm i ON
i.item = fin.pick
LEFT OUTER JOIN seg ON
seg.glec = i.glec;
*/

View File

@ -1,47 +0,0 @@
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
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
--SELECT * FROM gld
select oseas, sum(fb_val_loc * r_rate)
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 BETWEEN '2019-06-01' AND '2020-05-31'
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2020-05-31')
OR o.sseas BETWEEN '2001' AND '2012'
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
AND dsm = 'PW'
and version = 'ACTUALS'
group by oseas

View File

@ -1,63 +0,0 @@
SELECT
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') order_month,
version,
iter,
sum(fb_val_loc) value_loc
FROM
rlarp.osmf_stage o
GROUP BY
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'),
version,
iter
ORDER BY
oseas asc,
order_month asc
------------------------------------------short ships-------------------------------------
SELECT
version,
iter,
oseas,
SUM(FB_VAL_LOC::numeric *r_rate::Numeric)
FROM
RLARP.osmf_dev
WHERE
--oseas = 2020
fs_line = '41010'
--AND odate < '2020-04-01'
AND CALC_STATUS <> 'CANCELED'
AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
GROUP BY
version,
iter,
oseas
select distinct version from rlarp.osm_pool
SELECT
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') order_month,
r_currency,
r_rate,
sum(fb_val_loc) value_loc
FROM
rlarp.osmf_dev o
WHERE
oseas >= 2020
--AND fs_line = '41010'
--AND odate < '2020-04-01'
GROUP BY
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'),
r_currency,
r_rate
--glec
ORDER BY
oseas asc,
order_month asc

15
inquirey/osmf_months.sql Normal file
View File

@ -0,0 +1,15 @@
SELECT
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
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'plan'),0) plan2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'plan'),0) plan2022
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'diff'),0) diff2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'diff'),0) diff2022
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2021 AND iter = 'actuals'),0) act2021
,ROUND(SUM(fb_val_loc * r_rate) FILTER (WHERE oseas = 2022 AND iter = 'actuals'),0) act2022
FROM
rlarp.osmfs_dev o
WHERE
oseas IN (2021,2022)
--AND quota_rep_descr = 'COLIN MAXWELL'
GROUP BY
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')

17
inquirey/pool_months.sql Normal file
View File

@ -0,0 +1,17 @@
SELECT
order_month
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'plan') plan2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'plan') plan2022
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'diff') diff2021
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'diff') diff2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'pf') pf2022
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'pf') pf2022
,SUM(value_usd) FILTER (WHERE order_season = 2021 AND iter = 'actuals') act2021
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter = 'actuals') act2022
FROM
rlarp.osm_pool
WHERE
order_season IN (2021,2022)
--AND quota_rep_descr = 'COLIN MAXWELL'
GROUP BY
order_month

66
inquirey/tots.sql Normal file
View File

@ -0,0 +1,66 @@
SELECT
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') order_month,
version,
iter,
sum(fb_val_loc * r_rate) value_loc
FROM
rlarp.osmf_dev o
WHERE
fs_line = '41010'
and calc_status <> 'CANCELED'
GROUP BY
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'),
version,
iter
ORDER BY
oseas asc,
order_month asc
--------------------------------------------short ships-------------------------------------
--SELECT
-- version,
-- iter,
-- oseas,
-- SUM(FB_VAL_LOC::numeric *r_rate::Numeric)
--FROM
-- RLARP.osmf_dev
--WHERE
-- --oseas = 2020
-- fs_line = '41010'
-- --AND odate < '2020-04-01'
-- --AND CALC_STATUS <> 'CANCELED'
-- AND NOT (calc_status = 'CLOSED' AND flag = 'REMAINDER')
--GROUP BY
-- version,
-- iter,
-- oseas
--
-- select distinct version from rlarp.osm_pool
--
--
--
--
--SELECT
-- 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') order_month,
-- r_currency,
-- r_rate,
-- sum(fb_val_loc) value_loc
--FROM
-- rlarp.osmf_dev o
--WHERE
-- oseas >= 2020
-- --AND fs_line = '41010'
-- --AND odate < '2020-04-01'
--GROUP BY
-- 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'),
-- r_currency,
-- r_rate
-- --glec
--ORDER BY
-- oseas asc,
-- order_month asc

View File

@ -1,578 +0,0 @@
/*----------------replace these tags--------------
target_increment
scenario = submitted
target_month
---------------------------------------------------*/
WITH
target AS (select target_increment incr)
,testv AS (
SELECT
sum(units) tot
,sum(units) FILTER (WHERE iter = 'copy') base
,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart
,sum(value_loc *r_rate) totsales
,sum(value_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales
FROM
rlarp.osm_pool
WHERE
-----------------scenario----------------------------
scenario = target_scenario
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
)
,flagv AS (
SELECT
tot
,base
,newpart
,CASE WHEN tot = 0 THEN
CASE WHEN base = 0 THEN
CASE WHEN newpart = 0 THEN
'unclean data. tested -> does not exist'
ELSE
'scale new part'
END
ELSE
'scale copy'
END
ELSE
'scale all'
END flag
,CASE WHEN totsales = 0 THEN
CASE WHEN basesales = 0 THEN
CASE WHEN newpartsales = 0 THEN
'no price'
ELSE
'scale new part'
END
ELSE
'scale copy'
END
ELSE
'scale all'
END flagsales
FROM
testv
)
,GLD AS (
SELECT
N1COMP COMP
,N1CCYY FSYR
,KPMAXP PERDS
,N1FSPP PERD
,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT
,N1ED01 EDAT
,daterange(n1sd01, n1ed01,'[]') drange
,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'
)
,mseq AS (
SELECT * FROM
(
VALUES
('01 - Jun',1,6,-1)
,('02 - Jul',2,7,-1)
,('03 - Aug',3,8,-1)
,('04 - Sep',4,9,-1)
,('05 - Oct',5,10,-1)
,('06 - Nov',6,11,-1)
,('07 - Dec',7,12,-1)
,('08 - Jan',8,1,0)
,('09 - Feb',9,2,0)
,('10 - Mar',10,3,0)
,('11 - Apr',11,4,0)
,('12 - May',12,5,0)
) x(m,s,cal,yr)
)
,alldates AS (
SELECT
promo
,terms
,order_month
,mseq.s seq
,order_date
,request_date
,ship_date
,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM
rlarp.osm_pool
LEFT OUTER JOIN mseq ON
mseq.m = order_month
WHERE
-----------------scenario----------------------------
scenario = target_scenario
-----------------additional params-------------------
AND version = 'b21'
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket'
END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
GROUP BY
promo
,terms
,order_month
,mseq.s
,order_date
,request_date
,ship_date
HAVING
sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
)
--select * from alldates
,dom AS (
SELECT
extract(day FROM order_date) dom
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
extract(day FROM order_date)
)
---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)---------------
,mmix AS (
SELECT
to_char(order_date,'Mon') _month
,seq
,promo
,sum(extract(day from order_date)*value_usd) dom_wa
--,request_date-order_date rlag
,sum((request_date-order_date)*(value_usd)) rlag_wa
--,ship_date - request_date slag
,sum((ship_date - request_date)*(value_usd)) slag_wa
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
to_char(order_date,'Mon')
,seq
,promo
--,extract(day from order_date)
--,request_date-order_date
--,ship_date - request_date
)
,targm AS (select s, m from mseq where m = 'target_month' )
,mmixp AS (
SELECT
_month
,seq
,promo
,greatest(least(round((dom_wa/value_usd)::numeric,0)::int,28),1) odom
,round((rlag_wa/value_usd)::numeric,0)::int rlag
,round((slag_wa/value_usd)::numeric,0)::int slag
,value_usd/sum(value_usd) over (partition by _month) momix
--,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc
FROM
mmix
)
,closest AS (
SELECT
_month, targm.s, m
FROM
mmixp
CROSS JOIN targm
ORDER BY
abs(seq - targm.s) ASC
LIMIT 1
)
--SELECT * FROM mmixp order by seq asc, vperc desc
,basemix AS (
SELECT
plnt ---master data
,0::numeric(11,0) "ddord#" --0
,0::numeric(11,0) "dditm#" --0
,0::numeric(11,0) "fgbol#" --0
,0::numeric(11,0) "fgent#" --0
,0::numeric(9,0) "diinv#" --0
,0::numeric(3,0) "dilin#" --0
,null::text promo --history date mix
,null::text return_reas --0
,terms
,''::text custpo --0
,'I' dhincr --0
,null::numeric diext
,null::numeric ditdis
,null::date dcodat --calculated date mix
,null::date ddqdat --calculated date mix
,null::date dcmdat --calculated date mix
,null::date dhidat --calculated date mix
,null::text fspr --calculated date mix
,remit_to --master data
,bill_class --master data
,bill_cust --history cust mix
,bill_rep --master data
,bill_terr --master data
,ship_class --master data
,ship_cust --history cust mix
,ship_rep --master data
,ship_terr --master data
,quota_rep --master data
,account --master data
,shipgrp --master data
,geo --master data
,chan --master data
,orig_ctry --master data
,orig_prov --master data
,orig_post --master data
,dest_ctry --master data
,dest_prov --master data
,dest_post --master data
,part --history part mix
,ord_gldc --master data
,majg --master data
,ming --master data
,majs --master data
,mins --master data
,gldc --master data
,glec --master data
,harm --master data
,clss --master data
,brand --master data
,assc --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,0::numeric(15,5) ddqtoi --0
,0::numeric(15,5) ddqtsi --0
,0::numeric(15,5) fgqshp --0
,0::numeric(15,5) diqtsh --0
,sum(coalesce(units,0)) units --history value
,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix
,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data
,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data
,sum(coalesce(value_loc,0)) value_loc --history value
,sum(coalesce(value_loc_pl,0)) value_loc_pl --0
,calc_status --0
,flag --0
,null::date order_date --history date mix
,null::date request_date --history date mix
,null::date ship_date --history date mix
,null::date order_date --history
,null::date adj_request_date --history
,null::date ship_date --history
---------------ui columns-------------------------
,null::numeric order_season
,null::text order_month
,null::numeric ship_season
,null::text ship_month
,null::numeric request_season
,null::text request_month
,part_descr
,part_family
,part_group
,branding
,color
,segm
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
,null value_loc
,null value_usd
,null cost_loc
,null cost_usd
,null units
FROM
rlarp.osm_pool
WHERE
-----------------scenario----------------------------
scenario = target_scenario
-----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN module = 'new basket'
END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
GROUP BY
plnt ---master data
,terms
,remit_to --master data
,bill_class --master data
,bill_cust --history cust mix
,bill_rep --master data
,bill_terr --master data
,ship_class --master data
,ship_cust --history cust mix
,ship_rep --master data
,ship_terr --master data
,quota_rep --master data
,account --master data
,shipgrp --master data
,geo --master data
,chan --master data
,orig_ctry --master data
,orig_prov --master data
,orig_post --master data
,dest_ctry --master data
,dest_prov --master data
,dest_post --master data
,part --history part mix
,ord_gldc --master data
,majg --master data
,ming --master data
,majs --master data
,mins --master data
,gldc --master data
,glec --master data
,harm --master data
,clss --master data
,brand --master data
,assc --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,calc_status
,flag
,part_descr
,part_family
,part_group
,branding
,color
,segm
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
)
,scale AS (
SELECT
(SELECT incr::numeric FROM target) incr
,(SELECT sum(units) FROM basemix) base
,(SELECT incr::numeric FROM target)/(SELECT sum(units) FROM basemix) factor
)
,final AS (
SELECT
b.plnt --master data
,b."ddord#" --0
,b."dditm#" --0
,b."fgbol#" --0
,b."fgent#" --0
,b."diinv#" --0
,b."dilin#" --0
,m.promo --history date mix
,b.return_reas --0
,b.terms --history cust mix
,b.custpo --0
,b.dhincr --0
,b.diext --0
,b.ditdis --0
,b.dcodat --calculated date mix
,b.ddqdat --calculated date mix
,b.dcmdat --calculated date mix
,b.dhidat --calculated date mix
,b.fspr --calculated date mix
,b.remit_to --master data
,b.bill_class --master data
,b.bill_cust --history cust mix
,b.bill_rep --master data
,b.bill_terr --master data
,b.ship_class --master data
,b.ship_cust --history cust mix
,b.ship_rep --master data
,b.ship_terr --master data
,b.quota_rep --master data
,b.account --master data
,b.shipgrp --master data
,b.geo --master data
,b.chan --master data
,b.orig_ctry --master data
,b.orig_prov --master data
,b.orig_post --master data
,b.dest_ctry --master data
,b.dest_prov --master data
,b.dest_post --master data
,b.part --history part mix
,b.ord_gldc --master data
,b.majg --master data
,b.ming --master data
,b.majs --master data
,b.mins --master data
,b.gldc --master data
,b.glec --master data
,b.harm --master data
,b.clss --master data
,b.brand --master data
,b.assc --master data
,b.fs_line --master data
,b.r_currency --history cust mix
,b.r_rate --master data
,b.c_currency --master data
,b.c_rate --master data
,b.ddqtoi --0
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --0
,b.units*s.factor*m.momix units
,b.fb_cst_loc*s.factor*m.momix fb_cst_loc
,b.fb_cst_loc_cur*s.factor*m.momix fb_cst_loc_cur
,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut
,b.value_loc*s.factor*m.momix value_loc
,b.value_loc_pl*s.factor*m.momix value_loc_pl
,b.calc_status --0
,b.flag --0
,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag request_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_request_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date
,'replace_version' "version" --calculated
,'replace_source'||' volume' iter --calculated
-----------------------ui columns--------------------------------
,'replace_iterdet' iterdet
,$$replace_iterdef$$::jsonb iterdef
,od.ssyr order_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom),'Mon') order_month
,sd.ssyr ship_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
,rd.ssyr request_season
,to_char(make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag,'Mon') request_month
,b.part_descr
,b.part_family
,b.part_group
,b.branding
,b.color
,b.segm
,b.bill_cust_descr
,b.billto_group
,b.ship_cust_descr
,b.shipto_group
,b.majg_descr
,b.ming_descr
,b.majs_descr
,b.mins_descr
,b.mod_chan
,b.mod_chansub
,b.quota_rep_descr
,b.director_descr
,(b.value_loc*s.factor*m.momix)::numeric value_loc
,(b.value_loc*s.factor*m.momix*r_rate)::numeric value_usd
,(b.fb_cst_loc*s.factor*m.momix)::numeric cost_loc
,(b.fb_cst_loc*s.factor*m.momix*c_rate)::numeric cost_usd
,(b.units*s.factor*m.momix)::numeric units
FROM
basemix b
CROSS JOIN scale s
CROSS JOIN mmixp m
CROSS JOIN closest
LEFT OUTER JOIN mseq ON
mseq.m = closest.m
LEFT OUTER JOIN gld od ON
make_date(mseq.yr + 2020,mseq.cal,m.odom) BETWEEN od.sdat AND od.edat
LEFT OUTER JOIN gld rd ON
make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag BETWEEN rd.sdat AND rd.edat
LEFT OUTER JOIN gld sd ON
make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
WHERE
m._month = (SELECT _month FROM closest)
)
, ins AS (
INSERT INTO rlarp.osm_pool SELECT * FROM final RETURNING *
)
,insagg AS (
SELECT
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,sum(value_loc) value_loc
,sum(value_usd) value_usd
,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd
,sum(units) units
FROM
ins
GROUP BY
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
)
SELECT json_agg(row_to_json(insagg)) x from insagg

View File

@ -1,52 +0,0 @@
WITH
pool AS (
SELECT
substr(a.v6part,1,3) "family"
,substr(a.v6part,1,8) mold
,a.v6part part
,COALESCE(m.avmajg,p.awmajg) majg
,COALESCE(m.avming,p.awming) ming
,COALESCE(m.avmajs,p.awmajs) majs
,COALESCE(m.avmins,p.awmins) mins
FROM
lgdat.stka a
LEFT OUTER JOIN lgdat.stkmm m ON
m.avpart = v6part
LEFT OUTER JOIN lgdat.stkmp p ON
p.awpart = a.v6part
WHERE
COALESCE(awgled, avgled) = '1RE'
AND v6stat = 'A'
--and avmajg = '910'
GROUP BY
a.v6part
,COALESCE(m.avmajg,p.awmajg)
,COALESCE(m.avming,p.awming)
,COALESCE(m.avmajs,p.awmajs)
,COALESCE(m.avmins,p.awmins)
)
,llist AS (
SELECT
family
,mold
,min(part) part
,jsonb_agg(majg) majg
,jsonb_agg(ming) ming
,jsonb_agg(majs) majs
,jsonb_agg(mins) mins
FROM
pool
GROUP BY
family
,mold
)
SELECT
family
,mold
,part
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.majg) a(e)) x) majg
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.ming) a(e)) x) ming
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.majs) a(e)) x) majs
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.mins) a(e)) x) mins
FROM
llist

View File

@ -1,518 +0,0 @@
BEGIN;
DROP TABLE rlarp.osm_fcpool;
--EXPLAIN
--\timing
CREATE TABLE rlarp.osm_fcpool AS (
WITH
---------------------------------------last actual date------------------------------------------------------------------------------------------------------------------------------------------------
SALES_CUTOFF AS (
SELECT MAX(FSPR) MAX_FSPR FROM RLARP.OSM
)
,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
--if the period date is less than the first forecasted order it is actual from perspective of the plan
,CASE WHEN MAX_FSPR = to_char(N1FSYP,'FM0000') THEN 'C' ELSE CASE WHEN SALES_CUTOFF.MAX_FSPR > to_char(N1FSYP,'FM0000') THEN 'P' ELSE 'F' END END CM
,CASE
WHEN MAX_FSPR = to_char(N1FSYP,'FM0000') THEN
'C'
ELSE
CASE
WHEN SALES_CUTOFF.MAX_FSPR > to_char(N1FSYP,'FM0000') THEN
CASE
WHEN SUBSTR(SALES_CUTOFF.MAX_FSPR,1,2) > SUBSTR(to_char(N1FSYP,'FM0000'),1,2) THEN
'P'
ELSE
'C'
END
ELSE
'F'
END
END CY
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
KPCCYY = N1CCYY
CROSS JOIN SALES_CUTOFF
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
--SELECT * FROM gld
--CREATE INDEX SE_DAT ON gld (sdat, edat);
--SELECT daterange(MIN(SDAT),CURRENT_DATE,'[]') INTO _ytd FROM GLD WHERE CY = 'C';
--SELECT ssyr INTO _y FROM gld WHERE current_date <@ daterange(sdat,edat);
--CREATE TEMP TABLE rslt AS
--RETURN QUERY
---------------------------------------segments--------------------------------------------------------------------------------------------------------------------------------------------------------
,SEG AS (
SELECT
x.GLEC
,x.SEGM
FROM
(
VALUES
('1CU','Retail'),
('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)
)
---------------------------------------branding flag---------------------------------------------------------------------------------------------------------------------------------------------------
,MG AS (
SELECT
X.F1, X.MING, X.GRP
FROM
(
VALUES
('L','000','Branded'),
('L','B10','Branded'),
('L','B11','Branded'),
('L','B52','Branded'),
('L','C10','Branded'),
('L','D12','Branded'),
('L','M11','Branded'),
('L','P12','Branded'),
('L','P13','Branded'),
('L','S10','Branded'),
('L','Y10','Branded'),
('L','Y11','Branded'),
('L','Y12','Branded'),
('P','B10','Branded'),
('P','B11','Branded'),
('P','B52','Branded')
) AS X (F1, MING, GRP)
)
---------------------------------------directors-------------------------------------------------------------------------------------------------------------------------------------------------------
,REPC AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,LTRIM(RTRIM(C.A9)) || ' - ' || 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'
)
--MOLD DESCRIPTIONS
,MD AS (
SELECT
U.MOLD
,MAX(U.DESCR) DESCR
FROM
(
SELECT
SUBSTR(M.AVPART,1,8) MOLD
,MAX(M.AVDES1) DESCR
FROM
LGDAT.STKMM M
WHERE
LENGTH(M.AVPART) > 8
AND SUBSTR(M.AVGLED,1,1) <= '2'
GROUP BY
SUBSTR(M.AVPART,1,8)
UNION ALL
SELECT
SUBSTR(P.AWPART,1,8) MOLD
,MAX(P.AWDES1) DESCR
FROM
LGDAT.STKMP P
WHERE
LENGTH(P.AWPART) > 8
AND SUBSTR(P.AWGLED,1,1) <= '2'
GROUP BY
SUBSTR(P.AWPART,1,8)
) U
GROUP BY
U.MOLD
)
----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
)
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
----------------------------------------------------date ranges per season------------------------------------------------------------------------------------------------------------------------------
,seas AS(
SELECT
ssyr
,daterange(MIN(sdat),MAX(edat),'[]') dr
,current_date <@ daterange(MIN(sdat),MAX(edat),'[]') cy
FROM
gld
GROUP BY
ssyr
ORDER BY
ssyr DESC
)
,intv AS (
SELECT current_date - lower(dr) dy from seas where cy = true
)
,std AS (
SELECT
s.ssyr
,s.dr
,s.cy
,daterange(lower(dr),dy + LOWER(dr),'[]') ytd
FROM
seas s
CROSS JOIN intv
)
--select * from std
---------------------------------------union actual and forecast---------------------------------------------------------------------------------------------------------------------------------------
,UN AS (
--plan
SELECT
osm.plnt
,osm."ddord#"
,osm."dditm#"
,osm."fgbol#"
,osm."fgent#"
,osm."diinv#"
,osm."dilin#"
,osm.promo
,osm.return_reas
,osm.terms
,osm.custpo
,osm.dhincr
,osm.diext
,osm.ditdis
,osm.dcodat
,osm.ddqdat
,osm.dcmdat
,osm.dhidat
,osm.fspr
,osm.remit_to
,osm.bill_class
,osm.bill_cust
,osm.bill_rep
,osm.bill_terr
,osm.ship_class
,osm.ship_cust
,osm.ship_rep
,osm.ship_terr
,osm.quota_rep
,osm.account
,osm.shipgrp
,osm.geo
,osm.chan
,osm.orig_ctry
,osm.orig_prov
,osm.orig_post
,osm.dest_ctry
,osm.dest_prov
,osm.dest_post
,osm.part
,osm.ord_gldc
,osm.majg
,osm.ming
,osm.majs
,osm.mins
,osm.gldc
,osm.glec
,osm.harm
,osm.clss
,osm.brand
,osm.assc
,osm.fs_line
,osm.r_currency
,osm.r_rate
,osm.c_currency
,osm.c_rate
,osm.ddqtoi
,osm.ddqtsi
,osm.fgqshp
,osm.diqtsh
,osm.fb_qty
,osm.fb_cst_loc
,osm.fb_cst_loc_cur
,osm.fb_cst_loc_fut
,osm.fb_val_loc
,osm.fb_val_loc_pl
,osm.calc_status
,osm.flag
,osm.orderdate
,osm.requestdate
,greatest(least(osm.shipdate,sd.edat),sd.sdat) shipdate
,osm.adj_orderdate
,osm.adj_requestdate
,osm.adj_shipdate
,osm."version"
,osm.iter
----------------presentation columns----------------------------------------------
,null::text iterdet
,null::jsonb iterdef
,OD.SSYR order_season
,to_char(osm.adj_orderdate,'Mon') order_month
,SD.SSYR ship_season
,to_char(osm.adj_shipdate,'Mon') ship_month
,RD.SSYR request_season
,to_char(osm.adj_requestdate,'Mon') request_month
,osm.PART ||COALESCE(' - '|| RTRIM(COALESCE(AVDES1, AWDES1)),'') part_descr
,F.F3||COALESCE(' - '||F.DESCR,'') part_family
,SUBSTRING(PART,1,8)||COALESCE(' - '||MD.DESCR,'') part_group
,COALESCE(MG.GRP,'Unbranded') branding
,CASE WHEN substring(osm.glec,1,1) <= '2' THEN SUBSTRING(osm.part,9,3) ELSE '' END color
,COALESCE(SEG.SEGM,'Other') segm
,osm.bill_cust || COALESCE(' - '|| RTRIM(BC.BVNAME),'') bill_cust_descr
,osm.account billto_group
,osm.ship_cust || COALESCE(' - '|| RTRIM(SC.BVNAME),'') ship_cust_descr
,osm.shipgrp shipto_group
,osm.MAJG||COALESCE(' - '||BQDES,'') majg_descr
,osm.MING||COALESCE(' - '||BRDES,'') ming_descr
,osm.MAJS||COALESCE(' - '||SJ.BSDES1,'') majs_descr
,osm.MINS||COALESCE(' - '||SI.BSDES1,'') mins_descr
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN coalesce(L.RETAIL)
ELSE osm.CHAN
END mod_chan
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN coalesce(L.RETAILSUB)
ELSE ''
END mod_chansub
,COALESCE(REPC.REPP,osm.QUOTA_REP) quota_rep_descr
,COALESCE(REPC.DIRECTOR,osm.QUOTA_REP) director_descr
,fb_val_loc value_loc
,fb_val_loc * r_rate value_usd
,fb_cst_loc cost_loc
,fb_cst_loc * c_rate cost_usd
,fb_qty units
FROM
rlarp.osmfs osm
LEFT OUTER JOIN GLD SD ON
osm.FSPR = sd.FSPR
LEFT OUTER JOIN GLD OD ON
osm.ADJ_ORDERDATE BETWEEN od.SDAT AND od.EDAT
LEFT OUTER JOIN GLD RD ON
osm.ADJ_REQUESTDATE BETWEEN RD.SDAT AND RD.EDAT
LEFT OUTER JOIN SEG ON
SEG.GLEC = osm.GLEC
--AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN MG ON
MG.F1 = SUBSTR(osm.PART,16,1)
AND MG.MING = osm.MING
LEFT OUTER JOIN RLARP.FAMILY F ON
F.F3 = SUBSTR(osm.PART,1,3)
LEFT OUTER JOIN LGDAT.STKMM M ON
M.AVPART = osm.PART
LEFT OUTER JOIN LGDAT.STKMP P ON
P.AWPART = osm.PART
LEFT OUTER JOIN MD ON
MD.MOLD = SUBSTR(osm.PART,1,8)
LEFT OUTER JOIN RLARP.FFCHNL L ON
L.BILL = osm.BILL_CLASS
AND L.SHIP = osm.SHIP_CLASS
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = osm.BILL_CUST
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = osm.SHIP_CUST
LEFT OUTER JOIN REPC ON
REPC.RCODE = osm.QUOTA_REP
LEFT OUTER JOIN SI ON
SI.BSMJCD = osm.MAJS
AND SI.BSMNCD = osm.MINS
LEFT OUTER JOIN SJ ON
SJ.BSMJCD = osm.MAJS
LEFT OUTER JOIN LGDAT.MAJG ON
BQGRP = osm.MAJG
LEFT OUTER JOIN LGDAT.MMGP ON
BRMGRP = osm.MING
AND BRGRP = osm.MAJG
WHERE
osm.FS_LINE = '41010'
AND osm.CALC_STATUS <> 'CANCELED'
AND SUBSTR(osm.GLEC,1,1) <= '2'
/*
UNION ALL
SELECT
osm.plnt
,osm."ddord#"
,osm."dditm#"
,osm."fgbol#"
,osm."fgent#"
,osm."diinv#"
,osm."dilin#"
,osm.promo
,osm.return_reas
,osm.terms
,osm.custpo
,osm.dhincr
,osm.diext
,osm.ditdis
,osm.dcodat
,osm.ddqdat
,osm.dcmdat
,osm.dhidat
,osm.fspr
,osm.remit_to
,osm.bill_class
,osm.bill_cust
,osm.bill_rep
,osm.bill_terr
,osm.ship_class
,osm.ship_cust
,osm.ship_rep
,osm.ship_terr
,osm.quota_rep
,osm.account
,osm.shipgrp
,osm.geo
,osm.chan
,osm.orig_ctry
,osm.orig_prov
,osm.orig_post
,osm.dest_ctry
,osm.dest_prov
,osm.dest_post
,osm.part
,osm.ord_gldc
,osm.majg
,osm.ming
,osm.majs
,osm.mins
,osm.gldc
,osm.glec
,osm.harm
,osm.clss
,osm.brand
,osm.assc
,osm.fs_line
,osm.r_currency
,osm.r_rate
,osm.c_currency
,osm.c_rate
,osm.ddqtoi
,osm.ddqtsi
,osm.fgqshp
,osm.diqtsh
,osm.fb_qty
,osm.fb_cst_loc
,osm.fb_cst_loc_cur
,osm.fb_cst_loc_fut
,osm.fb_val_loc
,osm.fb_val_loc_pl
,osm.calc_status
,osm.flag
,osm.orderdate
,osm.requestdate
,greatest(least(osm.shipdate,sd.edat),sd.sdat) shipdate
,osm.adj_orderdate
,osm.adj_requestdate
,osm.adj_shipdate
,osm."version"
,osm.iter
----------------presentation columns----------------------------------------------
,null::text iterdet
,null::jsonb iterdef
,OD.SSYR order_season
,to_char(osm.adj_orderdate,'Mon') order_month
,SD.SSYR ship_season
,to_char(osm.adj_shipdate,'Mon') ship_month
,RD.SSYR request_season
,to_char(osm.adj_requestdate,'Mon') request_month
,PART ||' - '|| RTRIM(COALESCE(AVDES1, AWDES1)) part_descr
,F.F3||' - '||F.DESCR part_family
,SUBSTRING(PART,1,8)||' - '||MD.DESCR part_group
,COALESCE(MG.GRP,'Unbranded') branding
,CASE WHEN substring(osm.glec,1,1) <= '2' THEN SUBSTRING(osm.part,9,3) ELSE '' END color
,COALESCE(SEG.SEGM,'Other') segm
,osm.bill_cust ||' - '|| RTRIM(BC.BVNAME) bill_cust_descr
,osm.account billto_group
,osm.ship_cust ||' - '|| RTRIM(SC.BVNAME) ship_cust_descr
,osm.shipgrp shipto_group
,osm.MAJG||COALESCE(' - '||BQDES,'') majg_descr
,osm.MING||COALESCE(' - '||BRDES,'') ming_descr
,osm.MAJS||COALESCE(' - '||SJ.BSDES1,'') majs_descr
,osm.MINS||COALESCE(' - '||SI.BSDES1,'') mins_descr
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN coalesce(L.RETAIL,'')
ELSE osm.CHAN
END mod_chan
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN coalesce(L.RETAILSUB,'')
ELSE ''
END mod_chansub
,COALESCE(REPC.REPP,osm.QUOTA_REP) quota_rep_descr
,COALESCE(REPC.DIRECTOR,osm.QUOTA_REP) director_descr
,osm.fb_val_loc::numeric value_loc
,(osm.fb_val_loc * osm.r_rate)::numeric value_usd
,osm.fb_cst_loc::numeric cost_loc
,(osm.fb_cst_loc * osm.c_rate)::numeric cost_usd
,osm.fb_qty::numeric units
FROM
rlarp.osmfs osm
LEFT OUTER JOIN GLD SD ON
osm.FSPR = sd.FSPR
LEFT OUTER JOIN GLD OD ON
osm.ADJ_ORDERDATE BETWEEN od.SDAT AND od.EDAT
LEFT OUTER JOIN GLD RD ON
osm.ADJ_REQUESTDATE BETWEEN RD.SDAT AND RD.EDAT
LEFT OUTER JOIN SEG ON
SEG.GLEC = osm.GLEC
--AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN MG ON
MG.F1 = SUBSTR(osm.PART,16,1)
AND MG.MING = osm.MING
LEFT OUTER JOIN RLARP.FAMILY F ON
F.F3 = SUBSTR(osm.PART,1,3)
LEFT OUTER JOIN LGDAT.STKMM M ON
M.AVPART = osm.PART
LEFT OUTER JOIN LGDAT.STKMP P ON
P.AWPART = osm.PART
LEFT OUTER JOIN MD ON
MD.MOLD = SUBSTR(osm.PART,1,8)
LEFT OUTER JOIN RLARP.FFCHNL L ON
L.BILL = osm.BILL_CLASS
AND L.SHIP = osm.SHIP_CLASS
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = osm.BILL_CUST
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = osm.SHIP_CUST
LEFT OUTER JOIN REPC ON
REPC.RCODE = osm.QUOTA_REP
LEFT OUTER JOIN SI ON
SI.BSMJCD = osm.MAJS
AND SI.BSMNCD = osm.MINS
LEFT OUTER JOIN SJ ON
SJ.BSMJCD = osm.MAJS
LEFT OUTER JOIN LGDAT.MAJG ON
BQGRP = osm.MAJG
LEFT OUTER JOIN LGDAT.MMGP ON
BRMGRP = osm.MING
AND BRGRP = osm.MAJG
WHERE
osm.fs_line = '41010'
AND osm.calc_status <> 'CANCELED'
AND SUBSTRING(osm.glec,1,1) <= '2'
AND osm.version = 'ACTUALS'
AND osm.adj_orderdate BETWEEN '2017-06-01' AND '2018-05-31'
*/
)
SELECT * FROM UN
);
COMMIT;

243
offline/change_shipdate.sql Normal file
View File

@ -0,0 +1,243 @@
----change shipdates for a specific order/shipping month combination in the first where clause below
----generally drawn from monthly slots on a order/ship matrix to target those whole selecctions for a different shipping date
WITH
---------collapse iterations--------------------------------------
collapse AS (
SELECT
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
WHERE
--all June and earlier orders originally shipping in October now move back to June ship date
ship_season||substring(ship_month,1,2) = '202208'
and order_season||substring(order_month,1,2) <= '202112'
and segm = 'Greenhouse'
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
--HAVING
-- sum(o.units) <> 0
)
--SELECT
-- sum(value_usd)
--FROM
-- collapse
------------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"GH sales smooth",
"tag":"sales smooth",
"type":"build"
}$$::jsonb doc
RETURNING *
)
,rem AS (
SELECT
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,-units units
,-value_loc value_loc
,-value_usd value_usd
------exclude any prior pricing adjustments from the "current" price in the forecast------
,-cost_loc cost_loc
,-cost_usd 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
,log
)
,ins AS (
SELECT
'2201' fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,units
,value_loc
,value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,cost_loc
,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
,'2020-06-01'::date ship_date --history date mix
,'01 - Jun' ship_month
,2022 ship_season
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
,log
)
INSERT INTO
rlarp.osm_pool
SELECT * FROM rem
UNION ALL
SELECT * FROM ins;

File diff suppressed because it is too large Load Diff

View File

@ -1,547 +0,0 @@
BEGIN;
--\timing
truncate table rlarp.osmfs;
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
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
--SELECT * FROM gld
,price AS (
SELECT
part
,bill_cust
,ship_cust
,round(fb_val_loc/fb_qty,10) AS price
,orderdate
,row_number() OVER (PARTITION BY part,bill_cust, ship_cust ORDER BY orderdate DESC) rn
FROM
rlarp.osm
WHERE
fs_line = '41010'
AND calc_status <> 'CANCELED'
AND COALESCE(part,'') <> ''
AND fb_qty <> 0
AND orderdate IS NOT NULL
ORDER BY
part
,bill_cust
,ship_cust
,orderdate
)
,cop AS (
SELECT
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,o.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,sum(o.fb_qty) fb_qty
,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
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,o.calc_status
,o.flag
,o.orderdate
,o.requestdate
,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate
,o.adj_orderdate
,o.adj_requestdate
,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate
,'9p3' "version"
,'actual' iter
FROM
rlarp.osm o
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
WHERE
(
o.adj_orderdate BETWEEN '2018-06-01' AND '2019-03-24'
OR o.calc_status IN ('OPEN','BACKORDER')
OR o.fspr BETWEEN '1901' AND '1910'
)
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
--AND adj_orderdate <= adj_shipdate --exclude poorly date
GROUP BY
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,o.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.calc_status
,o.flag
,o.orderdate
,o.requestdate
,greatest(least(o.shipdate,gld.edat),gld.sdat)
,o.adj_orderdate
,o.adj_requestdate
,greatest(least(o.adj_shipdate,gld.edat),gld.sdat)
UNION ALL
SELECT
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,o.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,sum(o.fb_qty) fb_qty
,sum(COALESCE(o.fb_qty * coalesce(m.cgstcs, pr.chstcs, r.y0stcs),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
,sum(COALESCE(o.fb_qty * p.price,o.fb_val_loc)) fb_val_loc
,sum(o.fb_val_loc) fb_val_loc_pl
,'CLOSED' calc_status
,'SHIPMENT' flag
,o.orderdate
,o.requestdate
,greatest(least(o.shipdate,gld.edat),gld.sdat) shipdate
,o.adj_orderdate
,o.adj_requestdate
,greatest(least(o.adj_shipdate,gld.edat),gld.sdat) adj_shipdate
,'9p3' "version"
,'forecast' iter
FROM
rlarp.osmf o
LEFT OUTER JOIN gld ON
gld.fspr = o.fspr
LEFT OUTER JOIN price p ON
p.part = o.part
AND p.bill_cust = o.bill_cust
AND p.ship_cust = o.ship_cust
AND p.rn = 1
LEFT OUTER JOIN lgdat.icstm m ON
m.cgpart = o.part
AND m.cgplnt = o.plnt
LEFT OUTER JOIN lgdat.icstp pr ON
pr.chpart = o.part
AND pr.chplnt = o.plnt
LEFT OUTER JOIN lgdat.icstr r ON
y0part = o.part
AND y0plnt = o.plnt
WHERE
o.adj_orderdate BETWEEN '2019-03-25' AND '2019-05-31'
AND fs_line = '41010'
AND calc_status <> 'CANCELED'
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate --exclude bogus ship date orders
GROUP BY
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,o.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.orderdate
,o.requestdate
,greatest(least(o.shipdate,gld.edat),gld.sdat)
,o.adj_orderdate
,o.adj_requestdate
,greatest(least(o.adj_shipdate,gld.edat),gld.sdat)
)
,incr AS (
SELECT
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,gld.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,sum(o.fb_qty) fb_qty
,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
,sum(o.fb_val_loc) fb_val_loc
,sum(o.fb_val_loc_pl) fb_val_loc_pl
,o.calc_status
,o.flag
----these dates should already be snapped since the derive from the adjusted set
,o.orderdate + interval '1 year' orderdate
,o.requestdate + interval '1 year' requestdate
,o.shipdate + interval '1 year' shipdate
,o.adj_orderdate + interval '1 year' adj_orderdate
,o.adj_requestdate + interval '1 year' adj_requestdate
,o.adj_shipdate + interval '1 year' adj_shipdate
,'b20' "version"
,'copy' iter
FROM
cop o
LEFT OUTER JOIN gld ON
(o.adj_shipdate + interval '1 year') BETWEEN gld.sdat AND gld.edat
WHERE
adj_orderdate BETWEEN '2018-06-01' AND '2019-05-31'
GROUP BY
o.plnt
,o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.dhincr
,o.diext
,o.ditdis
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.dhidat
,gld.fspr
,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.quota_rep
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.ord_gldc
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.calc_status
,o.flag
,o.orderdate + interval '1 year'
,o.requestdate + interval '1 year'
,o.shipdate + interval '1 year'
,o.adj_orderdate + interval '1 year'
,o.adj_requestdate + interval '1 year'
,o.adj_shipdate + interval '1 year'
)
INSERT INTO rlarp.osmfs
SELECT * FROM incr
UNION ALL
SELECT * FROM cop;
---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;
COMMIT;

View File

@ -300,7 +300,7 @@ GROUP BY
s.account = o.billto_group s.account = o.billto_group
CROSS JOIN log CROSS JOIN log
WHERE WHERE
o.order_season = 2022 o.ship_season = 2022
AND o.segm = s.segment AND o.segm = s.segment
GROUP BY GROUP BY
o.fspr o.fspr

View File

@ -10,42 +10,42 @@ incr AS (
('110','PP','M',0.45), ('110','PP','M',0.45),
('110','PP','P',0.45), ('110','PP','P',0.45),
('110','PP','C',0.45), ('110','PP','C',0.45),
('210','PE','B',0.4), ('210','PE','B',0.40),
('210','PE','T',0.4), ('210','PE','T',0.40),
('210','PE','L',0.4), ('210','PE','L',0.40),
('210','PE','M',0.4), ('210','PE','M',0.40),
('210','PE','P',0.4), ('210','PE','P',0.40),
('210','PE','C',0.4), ('210','PE','C',0.40),
('310','PE','B',0.3), ('310','PE','B',0.30),
('310','PE','T',0.35), ('310','PE','T',0.35),
('310','PE','L',0.35), ('310','PE','L',0.35),
('310','PE','M',0.35), ('310','PE','M',0.35),
('310','PE','P',0.35), ('310','PE','P',0.35),
('310','PE','C',0.35), ('310','PE','C',0.35),
('310','PP','B',0.3), ('310','PP','B',0.30),
('310','PP','T',0.3), ('310','PP','T',0.30),
('310','PP','L',0.3), ('310','PP','L',0.30),
('310','PP','M',0.3), ('310','PP','M',0.30),
('310','PP','P',0.3), ('310','PP','P',0.30),
('310','PP','C',0.3), ('310','PP','C',0.30),
('310','PS','B',0.3), ('310','PS','B',0.30),
('310','PS','T',0.3), ('310','PS','T',0.30),
('310','PS','L',0.3), ('310','PS','L',0.30),
('310','PS','M',0.3), ('310','PS','M',0.30),
('310','PS','P',0.3), ('310','PS','P',0.30),
('310','PS','C',0.3), ('310','PS','C',0.30),
('320','PE','B',0.3), ('320','PE','B',0.30),
('320','PE','T',0.3), ('320','PE','T',0.30),
('320','PE','L',0.3), ('320','PE','L',0.30),
('320','PE','M',0.3), ('320','PE','M',0.30),
('320','PE','P',0.3), ('320','PE','P',0.30),
('320','PE','C',0.3), ('320','PE','C',0.30),
('320','PP','B',0.3), ('320','PP','B',0.30),
('320','PP','T',0.3), ('320','PP','T',0.30),
('320','PP','L',0.3), ('320','PP','L',0.30),
('320','PP','M',0.3), ('320','PP','M',0.30),
('320','PP','P',0.3), ('320','PP','P',0.30),
('320','PP','C',0.3), ('320','PP','C',0.30),
('910','PE','B',0.25), ('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),
@ -55,17 +55,17 @@ incr AS (
('910','PS','B',0.25), ('910','PS','B',0.25),
('910','PS','D',0.25), ('910','PS','D',0.25),
('910','PS','F',0.25), ('910','PS','F',0.25),
('910','','B',0.25), ('910', '','B',0.25),
('910','','T',0.25), ('910', '','T',0.25),
('910','','L',0.25), ('910', '','L',0.25),
('910','','M',0.25), ('910', '','M',0.25),
('910','','P',0.25), ('910', '','P',0.25),
('910','','D',0.25), ('910', '','D',0.25),
('910','','F',0.25), ('910', '','F',0.25),
('910','','C',0.25), ('910', '','C',0.25),
('610','','B',0.02), ('610', '','B',0.02),
('610','','S',0.02), ('610', '','S',0.02),
('610','','W',0.02) ('610', '','W',0.02)
) x(MAJG,ASSC,COLTIER,RATE) ) x(MAJG,ASSC,COLTIER,RATE)
) )
,chx AS ( ,chx AS (
@ -350,17 +350,17 @@ GROUP BY
--) --)
--SELECT * FROM test_unique where cnt > 1 --SELECT * FROM test_unique where cnt > 1
----------------create a log entry-------------------- ----------------create a log entry--------------------
,log AS ( --,log AS (
INSERT INTO -- INSERT INTO
rlarp.osm_log(doc) -- rlarp.osm_log(doc)
SELECT -- SELECT
$${ -- $${
"message":"application of last price and target increases to all forecast orders", -- "message":"application of last price and target increases to all forecast orders",
"tag":"last price r1", -- "tag":"last price r1",
"type":"build" -- "type":"build"
}$$::jsonb doc -- }$$::jsonb doc
RETURNING * -- RETURNING *
) --)
---------collapse iterations----------------------- ---------collapse iterations-----------------------
,collapse AS ( ,collapse AS (
SELECT SELECT
@ -518,23 +518,23 @@ SELECT
)*o.units - o.value_loc) * r_rate,2) )*o.units - o.value_loc) * r_rate,2)
,0) AS value_usd ,0) AS value_usd
----debug--- ----debug---
--,least( ,a.py_asp
-- least( ,a.py_gasp
-- COALESCE(a.py_asp,o.price_loc), ,a.last_price
-- COALESCE(a.py_gasp,a.target) ,o.price_loc
-- ) * (1 + a.rate), ,a.target
-- (a.jcprice->>0)::numeric ,a.jcprice
--) AS rev_price ,a.rate
--,o.units ,least(
--,o.value_loc least(
--,o.value_usd COALESCE(a.py_asp,o.price_loc),
--,o.price_loc COALESCE(a.py_gasp,a.target)
--,a.last_price ) * (1 + a.rate),
--,a.target (a.jcprice->>0)::numeric
--,a.jcprice ) AS rev_price
--,a.rate ,o.units qorig
--,a.py_asp ,o.value_loc vl_orig
--,a.py_gasp ,o.value_usd vu_orig
----------- -----------
,0::numeric cost_loc ,0::numeric cost_loc
,0::numeric cost_usd ,0::numeric cost_usd
@ -553,16 +553,16 @@ SELECT
---this iteration has to be listed in the master template file in order to be effectively included--- ---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version ,'b22' AS version
,'upload price' iter ,'upload price' iter
,log.id --,log.id
,COALESCE(log.doc->>'tag','') "tag" --,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment" --,log.doc->>'message' "comment"
,log.doc->>'type' module --,log.doc->>'type' module
FROM FROM
collapse o collapse o
--need to join to itemm to get the product from osm_pool --need to join to itemm to get the product from osm_pool
--,fcp a --,fcp a
,pivot a ,pivot a
,log --,log
WHERE WHERE
-- a.productt = o.productt -- a.productt = o.productt
--AND a.quota_rep_descr = o.quota_rep_descr --AND a.quota_rep_descr = o.quota_rep_descr
@ -579,10 +579,11 @@ WHERE
AND a.shipgrp = o.shipto_group AND a.shipgrp = o.shipto_group
AND o.order_date >= '2021-06-01' AND o.order_date >= '2021-06-01'
AND o.segm <> 'Retail' AND o.segm <> 'Retail'
AND o.dsm = 'PW'
) )
--SELECT order_season, order_month, sum(value_usd) from ins group by order_season, order_month; --SELECT order_season, order_month, sum(value_usd) from ins group by order_season, order_month;
INSERT INTO rlarp.osm_pool SELECT * FROM ins; --INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'A&W ANNUALS' and part = 'TWA10200G18B027'; SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------ -----------aggregate the impact------------
--COMMIT; --COMMIT;

File diff suppressed because it is too large Load Diff

View File

@ -1,803 +0,0 @@
WITH
/*
the volume must be expressed in terms of units, since that is what it will be scaling
*/
target AS (select $$replace_request$$::json def)
--select * from newm
,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
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
,mseq AS (
SELECT * FROM
(
VALUES
('Jun',1,6,-1)
,('Jul',2,7,-1)
,('Aug',3,8,-1)
,('Sep',4,9,-1)
,('Oct',5,10,-1)
,('Nov',6,11,-1)
,('Dec',7,12,-1)
,('Jan',8,1,0)
,('Feb',9,2,0)
,('Mar',10,3,0)
,('Apr',11,4,0)
,('May',12,5,0)
) x(m,s,cal,yr)
)
,SEG AS (
SELECT
x.GLEC
,x.SEGM
FROM
(
VALUES
('1CU','Retail'),
('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)
)
,MG AS (
SELECT
X.F1, X.MING, X.GRP
FROM
(
VALUES
('L','000','Branded'),
('L','B10','Branded'),
('L','B11','Branded'),
('L','B52','Branded'),
('L','C10','Branded'),
('L','D12','Branded'),
('L','M11','Branded'),
('L','P12','Branded'),
('L','P13','Branded'),
('L','S10','Branded'),
('L','Y10','Branded'),
('L','Y11','Branded'),
('L','Y12','Branded'),
('P','B10','Branded'),
('P','B11','Branded'),
('P','B52','Branded')
) AS X (F1, MING, GRP)
)
,REPC AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,LTRIM(RTRIM(C.A9)) || ' - ' || 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'
)
--MOLD DESCRIPTIONS
,MD AS (
SELECT
U.MOLD
,MAX(U.DESCR) DESCR
FROM
(
SELECT
SUBSTR(M.AVPART,1,8) MOLD
,MAX(M.AVDES1) DESCR
FROM
LGDAT.STKMM M
WHERE
LENGTH(M.AVPART) > 8
AND SUBSTR(M.AVGLED,1,1) <= '2'
GROUP BY
SUBSTR(M.AVPART,1,8)
UNION ALL
SELECT
SUBSTR(P.AWPART,1,8) MOLD
,MAX(P.AWDES1) DESCR
FROM
LGDAT.STKMP P
WHERE
LENGTH(P.AWPART) > 8
AND SUBSTR(P.AWGLED,1,1) <= '2'
GROUP BY
SUBSTR(P.AWPART,1,8)
) U
GROUP BY
U.MOLD
)
----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
)
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
,copr AS (
SELECT
LTRIM(RTRIM(A9)) AS COMP,
A30 AS DESCR,
SUBSTR(C.A249,242,2) CURR,
SUBSTR(C.A249,32,4) AS GL,
SUBSTR(C.A249,190,4) AS AR,
SUBSTR(C.A249,182,4) AS AP,
SUBSTR(C.A249,198,4) AS FA,
SUBSTR(C.A249,238,4) AS IC ,
SUBSTR(D.A249,9,5) CONS_FUNC
FROM
LGDAT.CODE
LEFT OUTER JOIN LGDAT.NAME C ON
'C0000'||LTRIM(RTRIM(A9)) = C.A7
LEFT OUTER JOIN LGDAT.NAME D ON
'D0000'||LTRIM(RTRIM(A9)) = D.A7
WHERE
A2 = 'AA' OR A2 IS NULL
)
,alldates AS (
SELECT
promo
,terms
,order_month
,mseq.s seq
,orderdate
,requestdate
,shipdate
,sum(value_usd) value_usd
FROM
rlarp.osm_fcpool
LEFT OUTER JOIN mseq ON
mseq.m = order_month
WHERE
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate
GROUP BY
promo
,terms
,order_month
,mseq.s
,orderdate
,requestdate
,shipdate
HAVING
sum(value_usd) <> 0
)
--select * from alldates
,dom AS (
SELECT
extract(day FROM orderdate) DOM
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
extract(day FROM orderdate)
)
---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)---------------
,mmix AS (
SELECT
to_char(orderdate,'Mon') _month
,seq
,promo
,sum(extract(day from orderdate)*value_usd) dom_wa
--,requestdate-orderdate rlag
,sum((requestdate-orderdate)*(value_usd)) rlag_wa
--,shipdate - requestdate slag
,sum((shipdate - requestdate)*(value_usd)) slag_wa
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
to_char(orderdate,'Mon')
,seq
,promo
--,extract(day from orderdate)
--,requestdate-orderdate
--,shipdate - requestdate
)
,targm AS (
SELECT
je.key as month
,r.amount
,r.qty
,s
,cal
,yr
FROM
target
JOIN LATERAL json_each(def->'months') je ON true
JOIN LATERAL json_to_record(je.value) as r (amount numeric, qty numeric) ON TRUE
LEFT OUTER JOIN mseq ON
mseq.m = je.key
)
,mmixp AS (
SELECT
_month
,seq
,promo
,round((dom_wa/value_usd)::numeric,0)::int odom
,round((rlag_wa/value_usd)::numeric,0)::int rlag
,round((slag_wa/value_usd)::numeric,0)::int slag
,value_usd/sum(value_usd) over (partition by _month) momix
--,value_usd/SUM(value_usd) over (PARTITION BY _month) vperc
FROM
mmix
)
--month cross join mix
,mxm AS (
SELECT
t.month
,t.amount
,t.qty
,t.s
,t.cal
,t.yr
,x.momix
,x.odom
,x.rlag
,x.slag
,x.promo
FROM
targm t
LEFT OUTER JOIN mmixp x ON
x._month = (
SELECT
_month
FROM
mmixp
ORDER BY
abs(seq - t.s) ASC
LIMIT 1
)
)
--SELECT * FROM mmixp order by seq asc, vperc desc
,basemix AS (
SELECT
null::text plnt ---master data
,0::numeric(11,0) "ddord#"
,0::numeric(11,0) "dditm#"
,0::numeric(11,0) "fgbol#"
,0::numeric(11,0) "fgent#"
,0::numeric(9,0) "diinv#"
,0::numeric(3,0) "dilin#"
,null::text promo --history
,null::text return_reas
,null::text terms
,''::text custpo
,'I' dhincr
,null::numeric diext
,null::numeric ditdis
,null::date dcodat
,null::date ddqdat
,null::date dcmdat
,null::date dhidat
,null::text fspr
,remit_to
,bill_class
,bill_cust
,bill_rep
,bill_terr
,ship_class
,ship_cust
,ship_rep
,ship_terr
,quota_rep
,account
,shipgrp
,geo
,chan
,orig_ctry
,orig_prov
,orig_post
,dest_ctry
,dest_prov
,dest_post
,null::text part --history part mix
,null::text ord_gldc
,null::text majg
,null::text ming
,null::text majs
,null::text mins
,null::text gldc
,null::text glec
,null::text harm
,null::text clss
,null::text brand
,null::text assc
,fs_line
,r_currency
,r_rate
,null::text c_currency
,null::numeric c_rate
,0::numeric(15,5) ddqtoi
,0::numeric(15,5) ddqtsi
,0::numeric(15,5) fgqshp
,0::numeric(15,5) diqtsh
,0::numeric fb_qty --history value
,0::numeric fb_cst_loc --history part mix
,0::numeric fb_cst_loc_cur
,0::numeric fb_cst_loc_fut
,0::numeric fb_val_loc --history value
,0::numeric fb_val_loc_pl
,calc_status
,flag
,null::date orderdate --history
,null::date requestdate --history
,null::date shipdate --history
,null::date adj_orderdate --history
,null::date adj_requestdate --history
,null::date adj_shipdate --history
,null::text "version"
,null::text iter
---------------ui columns-------------------------
,null::numeric order_season
,null::text order_month
,null::numeric ship_season
,null::text ship_month
,null::numeric request_season
,null::text request_month
,null::text part_descr
,null::text part_family
,null::text part_group
,null::text branding
,null::text color
,null::text segm
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,null::text majg_descr
,null::text ming_descr
,null::text majs_descr
,null::text mins_descr
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
,null::numeric value_loc
,null::numeric value_usd
,null::numeric cost_loc
,null::numeric cost_usd
,null::numeric units
,sum(coalesce(fb_val_loc * r_rate,0)) agg
FROM
rlarp.osm_fcpool
WHERE
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate
GROUP BY
remit_to
,bill_class
,bill_cust
,bill_rep
,bill_terr
,ship_class
,ship_cust
,ship_rep
,ship_terr
,quota_rep
,account
,shipgrp
,geo
,chan
,orig_ctry
,orig_prov
,orig_post
,dest_ctry
,dest_prov
,dest_post
,fs_line
,r_currency
,r_rate
,calc_status
,flag
,bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,mod_chan
,mod_chansub
,quota_rep_descr
,director_descr
)
,applyx AS (
SELECT
b.plnt --master data
,b."ddord#"
,b."dditm#"
,b."fgbol#"
,b."fgent#"
,b."diinv#"
,b."dilin#"
,mxm.promo --history
,b.return_reas
,b.terms --history cust mix
,b.custpo
,b.dhincr
,b.diext
,b.ditdis
,b.dcodat
,b.ddqdat
,b.dcmdat
,b.dhidat
,sd.fspr fspr
,b.remit_to --master data
,b.bill_class --master data
,b.bill_cust --history cust mix
,b.bill_rep --master data
,b.bill_terr --master data
,b.ship_class --master data
,b.ship_cust --history cust mix
,b.ship_rep --master data
,b.ship_terr --master data
,b.quota_rep --master data
,b.account --master data
,b.shipgrp --master data
,b.geo --master data
,b.chan --master data
,b.orig_ctry --master data
,b.orig_prov --master data
,b.orig_post --master data
,b.dest_ctry --master data
,b.dest_prov --master data
,b.dest_post --master data
,(SELECT (regexp_match(def->>'newpart','(.*) - '::text))[1] FROM target) part --history part mix
,b.ord_gldc --master data
,b.majg --master data
,b.ming --master data
,b.majs --master data
,b.mins --master data
,b.gldc --master data
,b.glec --master data
,b.harm --master data
,b.clss --master data
,b.brand --master data
,b.assc --master data
,b.fs_line --master data
,b.r_currency --history cust mix
,b.r_rate --master data
,b.c_currency --master data
,b.c_rate --master data
,b.ddqtoi
,b.ddqtsi
,b.fgqshp
,b.diqtsh
,(b.agg/sum(b.agg) OVER (partition by mxm.*))*mxm.momix*mxm.qty fb_qty
,b.fb_cst_loc
,b.fb_cst_loc_cur
,b.fb_cst_loc_fut
,(b.agg/sum(b.agg) OVER (partition by mxm.*))*mxm.momix*mxm.amount fb_val_loc
,b.fb_val_loc_pl
,b.calc_status
,b.flag
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) orderdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag requestdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag shipdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) adj_orderdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag adj_requestdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate
,'replace_version' "version"
,'replace_source'||' volume' iter
-----------------------ui columns--------------------------------
,'replace_iterdet' iterdet
,$$replace_iterdef$$::json iterdef
,od.ssyr order_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month
,sd.ssyr ship_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month
,rd.ssyr request_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month
,b.part_descr
,b.part_family
,b.part_group
,b.branding
,b.color
,b.segm
,b.bill_cust_descr
,b.billto_group
,b.ship_cust_descr
,b.shipto_group
,b.majg_descr
,b.ming_descr
,b.majs_descr
,b.mins_descr
,b.mod_chan
,b.mod_chansub
,b.quota_rep_descr
,b.director_descr
,b.value_loc
,b.value_usd
,b.cost_loc
,b.cost_usd
,b.units
--,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix
--,mxm.*
FROM
basemix b
CROSS JOIN mxm
LEFT OUTER JOIN gld od ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat
LEFT OUTER JOIN gld rd ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag BETWEEN rd.sdat AND rd.edat
LEFT OUTER JOIN gld sd ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
)
,final AS (
SELECT
COALESCE(m.avdplt,p.awdplt) plnt --master data
,b."ddord#"
,b."dditm#"
,b."fgbol#"
,b."fgent#"
,b."diinv#"
,b."dilin#"
,b.promo --history
,b.return_reas
,bc.bvterm terms --history cust mix
,b.custpo
,b.dhincr
,b.diext
,b.ditdis
,b.dcodat
,b.ddqdat
,b.dcmdat
,b.dhidat
,b.fspr
,b.remit_to --master data
,b.bill_class --master data
,b.bill_cust --history cust mix
,b.bill_rep --master data
,b.bill_terr --master data
,b.ship_class --master data
,b.ship_cust --history cust mix
,b.ship_rep --master data
,b.ship_terr --master data
,b.quota_rep --master data
,b.account --master data
,b.shipgrp --master data
,b.geo --master data
,b.chan --master data
,b.orig_ctry --master data
,b.orig_prov --master data
,b.orig_post --master data
,b.dest_ctry --master data
,b.dest_prov --master data
,b.dest_post --master data
,(SELECT (regexp_match(def->>'newpart','(.*) - '::text))[1] FROM target) part --history part mix
,COALESCE(m.avglcd, p.awgldc) ord_gldc --master data
,COALESCE(m.avmajg,p.awmajg) majg --master data
,COALESCE(m.avming,p.awming) ming --master data
,COALESCE(m.avmajs,p.awmajs) majs --master data
,COALESCE(m.avmins,p.awmins) mins --master data
,COALESCE(m.avglcd,p.awgldc) gldc --master data
,COALESCE(m.avgled,p.awgled) glec --master data
,COALESCE(m.avharm,p.awharm) harm --master data
,COALESCE(m.avclss,p.awclss) clss --master data
,SUBSTR(COALESCE(m."avcpt#",p."awvpt#"),1,1) brand --master data
,COALESCE(m.avassc,p.awassc) assc --master data
,b.fs_line --master data
,b.r_currency --history cust mix
,b.r_rate --master data
,copr.curr c_currency --master data
,cx.rate c_rate --master data
,b.ddqtoi
,b.ddqtsi
,b.fgqshp
,b.diqtsh
,b.fb_qty
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.fb_qty fb_cst_loc
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.fb_qty fb_cst_loc_cur
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.fb_qty fb_cst_loc_fut
,b.fb_val_loc
,b.fb_val_loc_pl
,b.calc_status
,b.flag
,b.orderdate
,b.requestdate
,b.shipdate
,b.adj_orderdate
,b.adj_requestdate
,b.adj_shipdate
,b."version"
,b.iter
-----------------------ui columns--------------------------------
,b.iterdet
,b.iterdef
,b.order_season
,b.order_month
,b.ship_season
,b.ship_month
,b.request_season
,b.request_month
,b.part ||' - '|| RTRIM(COALESCE(m.AVDES1, p.AWDES1)) part_descr
,F.F3||' - '||F.DESCR part_family
,SUBSTRING(b.part,1,8)||' - '||MD.DESCR part_group
,COALESCE(MG.GRP,'Unbranded') branding
,CASE WHEN substring(COALESCE(m.avgled, p.awgled),1,1) <= '2' THEN SUBSTRING(b.part,9,3) ELSE '' END color
,COALESCE(SEG.SEGM,'Other') segm
,b.bill_cust_descr
,b.billto_group
,b.ship_cust_descr
,b.shipto_group
,COALESCE(m.avmajg,p.awmajg)||COALESCE(' - '||BQDES,'') majg_descr
,COALESCE(m.avming,p.awming)||COALESCE(' - '||BRDES,'') ming_descr
,COALESCE(m.avmajs,p.awmajs)||COALESCE(' - '||SJ.BSDES1,'') majs_descr
,COALESCE(m.avmins,p.awmins)||COALESCE(' - '||SI.BSDES1,'') mins_descr
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN L.RETAIL
ELSE b.CHAN
END mod_chan
,CASE COALESCE(SEG.SEGM,'Other')
WHEN 'Retail' THEN L.RETAILSUB
ELSE ''
END mod_chansub
,b.quota_rep_descr
,b.director_descr
,b.fb_val_loc value_loc
,b.fb_val_loc *r_rate value_usd
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.fb_qty cost_loc
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.fb_qty*cx.rate cost_usd
,b.fb_qty units
FROM
applyx b
LEFT OUTER JOIN LGDAT.STKMM M ON
M.AVPART = b.PART
LEFT OUTER JOIN LGDAT.STKMP P ON
P.AWPART = b.PART
LEFT OUTER JOIN lgdat.stka a ON
v6part = b.part
AND v6plnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN SEG ON
SEG.GLEC = COALESCE(m.avgled,p.awgled)
--AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN MG ON
MG.F1 = SUBSTR(b.PART,16,1)
AND MG.MING = COALESCE(m.avming,p.awming)
LEFT OUTER JOIN RLARP.FAMILY F ON
F.F3 = SUBSTR(b.PART,1,3)
LEFT OUTER JOIN MD ON
MD.MOLD = SUBSTR(b.PART,1,8)
LEFT OUTER JOIN RLARP.FFCHNL L ON
L.BILL = b.BILL_CLASS
AND L.SHIP = b.SHIP_CLASS
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = b.BILL_CUST
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = b.BILL_CUST
LEFT OUTER JOIN REPC ON
REPC.RCODE = b.QUOTA_REP
LEFT OUTER JOIN SI ON
SI.BSMJCD = COALESCE(m.avmajs,p.awmajs)
AND SI.BSMNCD = COALESCE(m.avmins,p.awmins)
LEFT OUTER JOIN SJ ON
SJ.BSMJCD = COALESCE(m.avmajs,p.awmajs)
LEFT OUTER JOIN LGDAT.MAJG ON
BQGRP = COALESCE(m.avmajg,p.awmajg)
LEFT OUTER JOIN LGDAT.MMGP ON
BRMGRP = COALESCE(m.avming,p.awming)
AND BRGRP = COALESCE(m.avmajg,p.awmajg)
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = b.part
AND im.cgplnt = a.v6plnt
LEFT OUTER JOIN lgdat.icstp ip ON
ip.chpart = b.part
AND ip.chplnt = a.v6plnt
LEFT OUTER JOIN lgdat.icstr ir ON
ir.y0part = b.part
AND ir.y0plnt = a.v6plnt
LEFT OUTER JOIN lgdat.plnt ON
yaplnt = a.v6plnt
LEFT OUTER JOIN copr ON
copr.comp = yacomp::text
LEFT OUTER JOIN rlarp.ffcret cx ON
cx.perd = '1910'
AND cx.rtyp = 'MA'
AND cx.fcur = copr.curr
AND cx.tcur = 'US'
)
,ins AS (
INSERT INTO rlarp.osm_fcpool SELECT * FROM final RETURNING *
)
,insagg AS (
SELECT
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,sum(value_loc) value_loc
,sum(value_usd) value_usd
,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd
,sum(units) units
FROM
ins
GROUP BY
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
)
--SELECT * FROM insagg
SELECT json_agg(row_to_json(insagg)) x from insagg

View File

@ -1,96 +0,0 @@
BEGIN;
WITH
mseq AS (
SELECT * FROM
(
VALUES
('Jun',1,6,-1)
,('Jul',2,7,-1)
,('Aug',3,8,-1)
,('Sep',4,9,-1)
,('Oct',5,10,-1)
,('Nov',6,11,-1)
,('Dec',7,12,-1)
,('Jan',8,1,0)
,('Feb',9,2,0)
,('Mar',10,3,0)
,('Apr',11,4,0)
,('May',12,5,0)
) x(m,s,cal,yr)
)
,alldates AS (
SELECT
shipgrp
,to_char(orderdate,'Mon') orderm
,extract(month FROM age(requestdate,orderdate)) rmo
,(sum((shipdate - requestdate)* (fb_val_loc * r_rate))/sum(fb_val_loc * r_rate))::int wav_lag
--,sum(fb_val_loc * r_rate) value_usd
FROM
rlarp.osm
WHERE
adj_orderdate >= '2017-06-01'
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate
AND fspr IS NOT NULL
GROUP BY
shipgrp
,to_char(orderdate,'Mon')
,extract(month FROM age(requestdate,orderdate))
HAVING
sum(fb_val_loc * r_rate) <> 0
)
,lag AS (
SELECT
shipgrp
,orderm
,rmo
,wav_lag
FROM
alldates
ORDER BY
shipgrp
,orderm
,rmo
)
,opord AS (
SELECT
shipgrp
,orderdate
,requestdate
,shipdate
,to_char(orderdate,'Mon') orderm
,extract(month FROM age(requestdate,orderdate)) rmo
,sum(fb_val_loc * r_rate) value_usd
FROM
rlarp.osm
WHERE
calc_status NOT IN ('CANCELED','CLOSED')
GROUP BY
shipgrp
,orderdate
,requestdate
,shipdate
,to_char(orderdate,'Mon')
,extract(month FROM age(requestdate,orderdate))
ORDER BY
value_usd DESC
)
,upd AS (
UPDATE rlarp.osmfs o SET
adj_shipdate = greatest(shipdate + lag.wav_lag + 5 ,shipdate)
FROM
lag
WHERE
lag.shipgrp = o.shipgrp
AND lag.orderm = to_char(o.orderdate,'Mon')
AND lag.rmo = extract(month FROM age(o.requestdate,o.orderdate))
AND o.calc_status in ('BACKORDER','OPEN')
RETURNING *
)
SELECT
sum(fb_val_loc *r_rate) filter (where adj_shipdate < '2019-04-01')
,sum(fb_val_loc *r_rate) filter (where adj_shipdate >= '2019-04-01')
from upd;
COMMIT;

View File

@ -1,27 +0,0 @@
WITH
act As (
SELECT DISTINCT
v6part part
FROM
lgdat.stka
WHERE
(
v6stat = 'A'
OR v6part in ('TWA10203G18B027','TWA10203G18D050','MIH09000G18E001')
)
OR v6type = 'C'
)
,stkm AS (
SELECT
act.part || ' - ' ||rtrim(COALESCE(m.avdes1,p.awdes1)) part
--,COALESCE(m.avgled, p.awgled) glec
FROM
act
LEFT OUTER JOIN lgdat.stkmm m ON
m.avpart = act.part
LEFT OUTER JOIN lgdat.stkmp p ON
p.awpart = act.part
WHERE
substr(COALESCE(m.avgled, p.awgled),1,1) <= '2'
)
select part from stkm

194
offline/scale_cost.sql Normal file
View File

@ -0,0 +1,194 @@
--BEGIN;
WITH
req AS (
SELECT
23400000 cost_d
)
---------collapse iterations-----------------------
,collapse AS (
SELECT
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
WHERE
o.segm = 'Retail'
AND o.ship_season = '2022'
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
--HAVING
-- sum(o.units) <> 0
)
,adj AS (
SELECT
(SELECT cost_d FROM req) - SUM(o.cost_usd) adj_d
,((SELECT cost_d FROM req) - SUM(o.cost_usd))/SUM(o.cost_usd) adj_p
FROM
collapse o
)
--SELECT * from adj;
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"plug retail sales",
"tag":"retail plug",
"type":"build"
}$$::jsonb doc
RETURNING *
)
-------------build the iteration rows----------------
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,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
--,o.units
,units*adj.adj_p units
,0::numeric value_loc
,0::numeric value_usd
,cost_loc*adj.adj_p cost_loc
,cost_usd*adj.adj_p 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
--need to join to itemm to get the product from osm_pool
,log
,adj
)
--SELECT *, SUM(VALUE_USD) OVER() FROM (SELECT order_season, order_month, sum(value_usd) value_usd from ins group by order_season, order_month) as x;
INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------
--COMMIT;

194
offline/scale_price.sql Normal file
View File

@ -0,0 +1,194 @@
--BEGIN;
WITH
req AS (
SELECT
36600000 value_d
)
---------collapse iterations-----------------------
,collapse AS (
SELECT
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
WHERE
o.segm = 'Retail'
AND o.ship_season = '2022'
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
--HAVING
-- sum(o.units) <> 0
)
,adj AS (
SELECT
(SELECT value_d FROM req) - SUM(o.value_usd) adj_d
,((SELECT value_d FROM req) - SUM(o.value_usd))/SUM(o.value_usd) adj_p
FROM
collapse o
)
--SELECT * from adj;
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"plug retail sales",
"tag":"retail plug",
"type":"build"
}$$::jsonb doc
RETURNING *
)
-------------build the iteration rows----------------
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,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
--,o.units
,0::numeric units
,value_loc*adj.adj_p value_loc
,value_usd*adj.adj_p 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
--need to join to itemm to get the product from osm_pool
,log
,adj
)
--SELECT *, SUM(VALUE_USD) OVER() FROM (SELECT order_season, order_month, sum(value_usd) value_usd from ins group by order_season, order_month) as x;
INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------
--COMMIT;

View File

@ -0,0 +1,191 @@
--BEGIN;
WITH
----------------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 r1",
"type":"build"
}$$::jsonb doc
RETURNING *
)
---------collapse iterations-----------------------
,collapse AS (
SELECT
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.value_loc) FILTER (WHERE iter <> 'upload price')/SUM(o.units) FILTER (WHERE iter <> 'upload price') price_loc
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
HAVING
sum(o.units) <> 0
)
-------------build the iteration rows----------------
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,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
--,o.units
,0::numeric units
,value_loc*.45 value_loc
,value_usd*.45 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload price' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
--need to join to itemm to get the product from osm_pool
,log
WHERE
-- a.productt = o.productt
--AND a.quota_rep_descr = o.quota_rep_descr
--AND a.majg = SUBSTRING(o.majg_descr,1,3)
--AND a.billto_group = o.billto_group
--AND a.shipto_group = o.shipto_group
--AND a.chgrp = substring(o.chan,1,1)
---only apply to 2022 orders----
---join p
o.order_date >= '2021-06-01'
AND o.segm <> 'Retail'
AND o.dsm = 'PW'
)
--SELECT *, SUM(VALUE_USD) OVER() FROM (SELECT order_season, order_month, sum(value_usd) value_usd from ins group by order_season, order_month) as x;
INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------
--COMMIT;

View File

@ -0,0 +1,208 @@
--BEGIN;
WITH
--------for this script the req values are just the increment---------
req AS (
SELECT
-1000000 value_d
,-582302 cost_d
)
---------collapse iterations--------------------------------------
,collapse AS (
SELECT
o.fspr
,o.plnt
,o.promo
,o.terms
,o.bill_cust_descr
,o.ship_cust_descr
,o.dsm
,o.quota_rep_descr
,o.director
,o.billto_group
,o.shipto_group
,o.chan
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
WHERE
o.segm = 'Greenhouse'
AND o.ship_season = '2022'
AND o.ship_month = '08 - Jan'
AND o.order_month = '08 - Jan'
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
--HAVING
-- sum(o.units) <> 0
)
------------------cost/units adjustment percent------------------
,adj_c AS (
SELECT
(SELECT cost_d FROM req) adj_d
,(SELECT cost_d FROM req)/SUM(o.cost_usd) adj_p
FROM
collapse o
)
-----------------sales value adjustment percent-----------------
,adj_v AS (
SELECT
(SELECT value_d FROM req) adj_d
,(SELECT value_d FROM req)/SUM(o.value_usd) adj_p
FROM
collapse o
)
--SELECT * from adj;
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"sales smooth",
"tag":"sales smooth",
"type":"build"
}$$::jsonb doc
RETURNING *
)
-------------build the iteration rows----------------
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,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
--,o.units
,units *adj_c.adj_p units
,value_loc *adj_v.adj_p value_loc
,value_usd *adj_v.adj_p value_usd
,cost_loc *adj_c.adj_p cost_loc
,cost_usd *adj_c.adj_p 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
--need to join to itemm to get the product from osm_pool
,log
,adj_c
,adj_v
)
--SELECT *, SUM(VALUE_USD) OVER() FROM (SELECT order_season, order_month, sum(value_usd) value_usd from ins group by order_season, order_month) as x;
INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------
--COMMIT;

195
offline/scale_volume.sql Normal file
View File

@ -0,0 +1,195 @@
--BEGIN;
WITH
req AS (
SELECT
5047782 value_d
)
---------collapse iterations-----------------------
,collapse AS (
SELECT
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
-----exclude any prior pricing adjustments from the "current" price in the forecast------
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) 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
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
WHERE
o.segm = 'Sustainable'
AND substring(o.mins_descr,1,3) = 'A04'
AND o.ship_season = '2022'
GROUP BY
o.fspr
,o.plnt ---master data
,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
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,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
--HAVING
-- sum(o.units) <> 0
)
,adj AS (
SELECT
(SELECT value_d FROM req) - SUM(o.value_usd) adj_d
,((SELECT value_d FROM req) - SUM(o.value_usd))/SUM(o.value_usd) adj_p
FROM
collapse o
)
--SELECT * from adj;
----------------create a log entry--------------------
,log AS (
INSERT INTO
rlarp.osm_log(doc)
SELECT
$${
"message":"top level adjust",
"tag":"top level adjust",
"type":"build"
}$$::jsonb doc
RETURNING *
)
-------------build the iteration rows----------------
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,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
--,o.units
,units*adj.adj_p units
,value_loc*adj.adj_p value_loc
,value_usd*adj.adj_p value_usd
,cost_loc*adj.adj_p cost_loc
,cost_usd*adj.adj_p 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
---this iteration has to be listed in the master template file in order to be effectively included---
,'b22' AS version
,'upload volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
collapse o
--need to join to itemm to get the product from osm_pool
,log
,adj
)
--SELECT *, SUM(VALUE_USD) OVER() FROM (SELECT order_season, order_month, sum(value_usd) value_usd from ins group by order_season, order_month) as x;
INSERT INTO rlarp.osm_pool SELECT * FROM ins;
--SELECT * FROM ins WHERE shipto_group = 'BATTLEFIELD FARMS' and part = 'XNT0TQT3X56B220PYDKP';
-----------aggregate the impact------------
--COMMIT;

View File

@ -1,38 +0,0 @@
BEGIN;
WITH
plist AS (
SELECT DISTINCT
part
,plnt
FROM
rlarp.osm_fcpool
)
,clist AS (
SELECT
p.part
,p.plnt
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs) stdcost
FROM
plist p
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = p.part
AND im.cgplnt = p.plnt
LEFT OUTER JOIN lgdat.icstp ip ON
ip.chpart = p.part
AND ip.chplnt = p.plnt
LEFT OUTER JOIN lgdat.icstr ir ON
ir.y0part = p.part
AND ir.y0plnt = p.plnt
)
UPDATE
rlarp.osm_fcpool o
SET
fb_cst_loc_cur = c.stdcost * o.fb_qty
FROM
clist c
WHERE
c.part = o.part
AND c.plnt = o.plnt;
commit;

View File

@ -1,38 +0,0 @@
BEGIN;
WITH
plist AS (
SELECT DISTINCT
part
,plnt
FROM
rlarp.osm_fcpool
)
,clist AS (
SELECT
p.part
,p.plnt
,COALESCE(im.cnstcs,ip.costcs, ir.y3stcs) stdcost
FROM
plist p
LEFT OUTER JOIN lgdat.ftcstm im ON
im.cnpart = p.part
AND im.cnplnt = p.plnt
LEFT OUTER JOIN lgdat.ftcstp ip ON
ip.copart = p.part
AND ip.coplnt = p.plnt
LEFT OUTER JOIN lgdat.ftcstr ir ON
ir.y3part = p.part
AND ir.y3plnt = p.plnt
)
UPDATE
rlarp.osm_fcpool o
SET
fb_cst_loc_fut = c.stdcost * o.fb_qty
FROM
clist c
WHERE
c.part = o.part
AND c.plnt = o.plnt;
commit;

View File

@ -1,29 +0,0 @@
UPDATE
rlarp.osmfs 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.adj_shipdate BETWEEN gld.sdat AND gld.edat

View File

@ -1,336 +0,0 @@
BEGIN;
WITH
repl AS (
SELECT * FROM
(
VALUES
('MOW24000A47E880','MOW24000A47E880')
) as x(orig,fix)
)
,SEG AS (
SELECT
x.GLEC
,x.SEGM
FROM
(
VALUES
('1CU','Retail'),
('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)
)
,MG AS (
SELECT
X.F1, X.MING, X.GRP
FROM
(
VALUES
('L','000','Branded'),
('L','B10','Branded'),
('L','B11','Branded'),
('L','B52','Branded'),
('L','C10','Branded'),
('L','D12','Branded'),
('L','M11','Branded'),
('L','P12','Branded'),
('L','P13','Branded'),
('L','S10','Branded'),
('L','Y10','Branded'),
('L','Y11','Branded'),
('L','Y12','Branded'),
('P','B10','Branded'),
('P','B11','Branded'),
('P','B52','Branded')
) AS X (F1, MING, GRP)
)
,REPC AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,LTRIM(RTRIM(C.A9)) || ' - ' || 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'
)
--MOLD DESCRIPTIONS
,MD AS (
SELECT
U.MOLD
,MAX(U.DESCR) DESCR
FROM
(
SELECT
SUBSTR(M.AVPART,1,8) MOLD
,MAX(M.AVDES1) DESCR
FROM
LGDAT.STKMM M
WHERE
LENGTH(M.AVPART) > 8
AND SUBSTR(M.AVGLED,1,1) <= '2'
GROUP BY
SUBSTR(M.AVPART,1,8)
UNION ALL
SELECT
SUBSTR(P.AWPART,1,8) MOLD
,MAX(P.AWDES1) DESCR
FROM
LGDAT.STKMP P
WHERE
LENGTH(P.AWPART) > 8
AND SUBSTR(P.AWGLED,1,1) <= '2'
GROUP BY
SUBSTR(P.AWPART,1,8)
) U
GROUP BY
U.MOLD
)
----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
)
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
,copr AS (
SELECT
LTRIM(RTRIM(A9)) AS COMP,
A30 AS DESCR,
SUBSTR(C.A249,242,2) CURR,
SUBSTR(C.A249,32,4) AS GL,
SUBSTR(C.A249,190,4) AS AR,
SUBSTR(C.A249,182,4) AS AP,
SUBSTR(C.A249,198,4) AS FA,
SUBSTR(C.A249,238,4) AS IC ,
SUBSTR(D.A249,9,5) CONS_FUNC
FROM
LGDAT.CODE
LEFT OUTER JOIN LGDAT.NAME C ON
'C0000'||LTRIM(RTRIM(A9)) = C.A7
LEFT OUTER JOIN LGDAT.NAME D ON
'D0000'||LTRIM(RTRIM(A9)) = D.A7
WHERE
A2 = 'AA' OR A2 IS NULL
)
,x AS (
SELECT
o.part origpart
,repl.fix as part
,COALESCE(m.avdplt,p.awdplt) plnt
,COALESCE(m.avglcd, p.awgldc) ord_gldc
,COALESCE(m.avmajg,p.awmajg) majg
,COALESCE(m.avming,p.awming) ming
,COALESCE(m.avmajs,p.awmajs) majs
,COALESCE(m.avmins,p.awmins) mins
,COALESCE(m.avglcd,p.awgldc) gldc
,COALESCE(m.avgled,p.awgled) glec
,COALESCE(m.avharm,p.awharm) harm
,COALESCE(m.avclss,p.awclss) clss
,SUBSTR(COALESCE(m."avcpt#",p."awvpt#"),1,1) brand
,COALESCE(m.avassc,p.awassc) assc
,COALESCE(cm.cgstcs, cp.chstcs, cr.y0stcs) stdcost
,copr.curr c_currency
,cx.rate c_rate
,SUBSTR(repl.fix,1,3)||coalesce(' - '||F.DESCR,'') part_family
,SUBSTRING(repl.fix,1,8)||coalesce(' - '||MD.DESCR,'') part_group
,COALESCE(MG.GRP,'Unbranded') branding
,CASE WHEN substring(COALESCE(m.avgled, p.awgled),1,1) <= '2' THEN SUBSTRING(repl.fix,9,3) ELSE '' END color
,COALESCE(SEG.SEGM,'Other') segm
,COALESCE(m.avmajg,p.awmajg)||COALESCE(' - '||BQDES,'') majg_descr
,COALESCE(m.avming,p.awming)||COALESCE(' - '||BRDES,'') ming_descr
,COALESCE(m.avmajs,p.awmajs)||COALESCE(' - '||SJ.BSDES1,'') majs_descr
,COALESCE(m.avmins,p.awmins)||COALESCE(' - '||SI.BSDES1,'') mins_descr
--quota rep--------------------------------------------
,RTRIM(
COALESCE(
--retail items go to currep, or if null go to 90005
CASE WHEN COALESCE(m.avgled,p.awgled) IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--retail chain got to Mark Wilkinson
CASE COALESCE(cg.cgrp,bc.bvname)
WHEN 'THE HOME DEPOT' THEN '90005'
WHEN 'DO IT BEST' THEN '90005'
WHEN 'ACE HARDWARE' THEN '90005'
WHEN 'ALDI' THEN '90005'
WHEN 'AMAZON.COM' THEN '90005'
WHEN 'GARDEN RIDGE CORP' THEN '90005'
--all other retail goes to Doran Marable
ELSE '50200'
END
END
ELSE
CASE WHEN COALESCE(m.avming,p.awming) = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm
END
END
END
,''
)
) quota_rep
,repc.repp quota_rep_descr
,repc.director director_descr
FROM
repl
INNER JOIN rlarp.osm_fcpool o ON
o.part = repl.orig
LEFT OUTER JOIN lgdat.stkmm m ON
m.avpart = repl.fix
LEFT OUTER JOIN lgdat.stkmp p ON
p.awpart = repl.fix
LEFT OUTER JOIN lgdat.stka a ON
a.v6part = repl.fix
AND a.v6plnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN lgdat.icstm cm ON
cm.cgpart = repl.fix
AND cm.cgplnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN lgdat.icstp cp ON
cp.chpart = repl.fix
AND cp.chplnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN lgdat.icstr cr ON
cr.y0part = repl.fix
AND cr.y0plnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN SEG ON
SEG.GLEC = COALESCE(m.avgled,p.awgled)
--AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN MG ON
MG.F1 = SUBSTR(repl.fix,16,1)
AND MG.MING = COALESCE(m.avming,p.awming)
LEFT OUTER JOIN rlarp.family F ON
F.F3 = SUBSTR(repl.fix,1,3)
LEFT OUTER JOIN MD ON
MD.MOLD = SUBSTR(repl.fix,1,8)
LEFT OUTER JOIN SI ON
SI.BSMJCD = COALESCE(m.avmajs,p.awmajs)
AND SI.BSMNCD = COALESCE(m.avmins,p.awmins)
LEFT OUTER JOIN SJ ON
SJ.BSMJCD = COALESCE(m.avmajs,p.awmajs)
LEFT OUTER JOIN LGDAT.MAJG ON
BQGRP = COALESCE(m.avmajg,p.awmajg)
LEFT OUTER JOIN LGDAT.MMGP ON
BRMGRP = COALESCE(m.avming,p.awming)
AND BRGRP = COALESCE(m.avmajg,p.awmajg)
LEFT OUTER JOIN lgdat.plnt ON
yaplnt = a.v6plnt
LEFT OUTER JOIN copr ON
copr.comp = yacomp::text
LEFT OUTER JOIN rlarp.ffcret cx ON
cx.perd = '1910'
AND cx.rtyp = 'MA'
AND cx.fcur = copr.curr
AND cx.tcur = 'US'
------for quota rep------------
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 RLARP.FFCUST CG ON
CG.CUSTN = BC.BVCUST
LEFT OUTER JOIN RLARP.FFCUST SG ON
SG.CUSTN = SC.BVCUST
LEFT OUTER JOIN REPC ON
REPC.RCODE = RTRIM(
COALESCE(
--retail items go to currep, or if null go to 90005
CASE WHEN COALESCE(m.avgled,p.awgled) IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--retail chain got to Mark Wilkinson
CASE COALESCE(cg.cgrp,bc.bvname)
WHEN 'THE HOME DEPOT' THEN '90005'
WHEN 'DO IT BEST' THEN '90005'
WHEN 'ACE HARDWARE' THEN '90005'
WHEN 'ALDI' THEN '90005'
WHEN 'AMAZON.COM' THEN '90005'
WHEN 'GARDEN RIDGE CORP' THEN '90005'
--all other retail goes to Doran Marable
ELSE '50200'
END
END
ELSE
CASE WHEN COALESCE(m.avming,p.awming) = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm
END
END
END
,''
)
)
)
,u AS (
UPDATE
rlarp.osm_fcpool o
SET
part = x.part
,plnt = x.plnt
,ord_gldc = x.ord_gldc
,majg = x.majg
,ming = x.ming
,majs = x.majs
,mins = x.mins
,gldc = x.gldc
,glec = x.glec
,harm = x.harm
,clss = x.clss
,assc = x.assc
,brand = x.brand
,fb_cst_loc = o.fb_qty * x.stdcost
--,fb_cst_loc_cur = o.fb_qty * x.stdcost
--,fb_cst_loc_fut = o.fb_qty * x.stdcost
,cost_loc = o.fb_qty * x.stdcost
,cost_usd = o.fb_qty * x.stdcost * x.c_rate
,c_currency = x.c_currency
,c_rate = x.c_rate
,part_family = x.part_family
,part_group = x.part_group
,branding = x.branding
,color = x.color
,segm = x.segm
,majg_descr = x.majg_descr
,ming_descr = x.ming_descr
,majs_descr = x.majs_descr
,mins_descr = x.mins_descr
,quota_rep = x.quota_rep
,quota_rep_descr = x.quota_rep_descr
,director_descr = x.director_descr
FROM
x
WHERE
x.origpart = o.part
RETURNING o.*
)
SELECT * FROM u;
--ROLLBACK;
--COMMIT;

View File

@ -1,112 +0,0 @@
With
REPC AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,LTRIM(RTRIM(C.A9)) || ' - ' || 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'
)
,r AS (
SELECT
b.quota_rep base_rep
--quota rep--------------------------------------------
,RTRIM(
COALESCE(
--retail items go to currep, or if null go to 90005
CASE WHEN COALESCE(b.glec) IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--retail chain got to Mark Wilkinson
CASE COALESCE(cg.cgrp,bc.bvname)
WHEN 'THE HOME DEPOT' THEN '90005'
WHEN 'DO IT BEST' THEN '90005'
WHEN 'ACE HARDWARE' THEN '90005'
WHEN 'ALDI' THEN '90005'
WHEN 'AMAZON.COM' THEN '90005'
WHEN 'GARDEN RIDGE CORP' THEN '90005'
--all other retail goes to Doran Marable
ELSE '50200'
END
END
ELSE
CASE WHEN COALESCE(b.ming) = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm
END
END
END
,''
)
) revised_rep
,sum(fb_val_loc * r_rate) sales
FROM
rlarp.osm_fcpool b
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = b.BILL_CUST
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = b.SHIP_CUST
LEFT OUTER JOIN RLARP.FFCUST CG ON
CG.CUSTN = BC.BVCUST
LEFT OUTER JOIN RLARP.FFCUST SG ON
SG.CUSTN = SC.BVCUST
GROUP BY
b.quota_rep
--quota rep--------------------------------------------
,RTRIM(
COALESCE(
--retail items go to currep, or if null go to 90005
CASE WHEN COALESCE(b.glec) IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--retail chain got to Mark Wilkinson
CASE COALESCE(cg.cgrp,bc.bvname)
WHEN 'THE HOME DEPOT' THEN '90005'
WHEN 'DO IT BEST' THEN '90005'
WHEN 'ACE HARDWARE' THEN '90005'
WHEN 'ALDI' THEN '90005'
WHEN 'AMAZON.COM' THEN '90005'
WHEN 'GARDEN RIDGE CORP' THEN '90005'
--all other retail goes to Doran Marable
ELSE '50200'
END
END
ELSE
CASE WHEN COALESCE(b.ming) = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm
END
END
END
,''
)
)
)
SELECT
b.repp
,v.repp
,r.sales
FROM
r
LEFT OUTER JOIN REPC b on
b.rcode = r.base_rep
LEFT OUTER JOIN REPC v on
v.rcode = r.revised_rep

View File

@ -1,39 +0,0 @@
WITH nc AS (
select
part
,plnt
, to_char(sum(fb_val_loc * r_rate),'9,999,999') sales
, to_char(sum(fb_qty),'9,999,999') units
from
rlarp.osm_fcpool
where
(
(
fb_qty <> 0
and coalesce(fb_cst_loc,0) = 0
)
or plnt is null
)
and version = 'b20'
group by
part
,plnt
)
SELECT
nc.*
,v6stat
,coalesce(cgstcs, chstcs,y0stcs) stcs
FROM
nc
left outer join lgdat.stka ON
v6part = part
and v6plnt = plnt
left outer join lgdat.icstm ON
cgpart = part
and cgplnt = plnt
left outer join lgdat.icstp ON
chpart = part
and chplnt = plnt
left outer join lgdat.icstr ON
y0part = part
and y0plnt = plnt