diff --git a/.gitignore b/.gitignore
index 87b7008..590eb27 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,9 @@ npm-debug.log
*.swp
/node_modules
.env
-/.vscode
\ No newline at end of file
+/.vscode
+logs.html
+stat.html
+*.pem
+node.log
+changes.html
diff --git a/index.js b/index.js
index b95aaca..6702861 100644
--- a/index.js
+++ b/index.js
@@ -1,5 +1,8 @@
+#!/usr/bin/env node
+
require('dotenv').config();
const express = require('express');
+var https = require('https');
var bodyParser = require('body-parser');
const server = express();
const pg = require('pg');
@@ -9,6 +12,17 @@ var fs = require('fs');
var readline = require('readline');
//-------------------------------------------------------
+var options = {
+ key: fs.readFileSync('//var/www/forecast_api/key.pem'),
+ cert: fs.readFileSync('//var/www/forecast_api/cert.pem'),
+ passprase: []
+};
+
+https.createServer(options,server).listen(3000, () => {
+ console.log('started on 3000')
+});
+//server.listen(3000, () => console.log('started'))
+
var Postgres = new pg.Client({
user: process.env.user,
password: process.env.password,
@@ -28,13 +42,14 @@ Postgres.FirstRow = function (inSQL, args, inResponse) {
}
console.log(err.stack);
inResponse.json(err.stack);
- });
+ });
};
server.get('/', (req, res) => res.send('node.js express is up and running'))
+server.get('/login', (req, res) => res.sendFile('//var/www/forecast_api/msauth.html'))
-server.get('/logs', (req, res) => res.sendFile('~/forecast_api/logs.html'))
+server.get('/logs', (req, res) => res.sendFile('//var/www/forecast_api/logs.html'))
server.get('/test_sql', function(req, res){
@@ -59,7 +74,7 @@ server.get('/get_pool', bodyParser.json(), function (req, res) {
var path = './route_sql/get_pool.sql';
var callback = function(arg){
sql = arg;
- console.log("-------------------------get pool:----------------------------");
+ console.log(new Date().toISOString() + "-------------------------get pool:----------------------------");
console.log(req.body.quota_rep);
sql = sql.replace("rep_replace",req.body.quota_rep);
Postgres.FirstRow(sql,[],res)
@@ -105,7 +120,7 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("-------------------------get scenario:------------------------------")
+ console.log(new Date().toISOString() + "-------------------------get scenario:------------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("where_clause",'g'),w)
@@ -115,6 +130,37 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) {
};
})
+server.get('/list_changes', bodyParser.json(), function (req, res) {
+
+ var sql = "";
+ var w = "";
+ var c = 1;
+ var d = 1;
+ var args = [];
+ var path = './route_sql/list_changes.sql';
+
+ fs.readFile(path, 'utf8', function(err, data){
+ if (!err){
+ callback(data);
+ } else {
+ console.log("fatal error pulling sql file")
+ callback(err);
+ }
+ });
+
+ var callback = function(arg){
+ sql = arg;
+
+ console.log(new Date().toISOString() +"-------------------------get scenario:------------------------------")
+ console.log(req.body);
+ //parse the where clause into the main sql statement
+ sql = sql.replace(new RegExp("replace_user",'g'),JSON.stringify(req.body))
+ //execute the sql and send the result
+ //console.log(sql);
+ Postgres.FirstRow(sql,[],res)
+ };
+})
+
server.post('/addmonth_v', bodyParser.json(), function (req, res) {
var sql = "";
@@ -142,7 +188,8 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("-----------------------------add month volume:---------------------------------");
+ console.log(new Date().toISOString() +"-----------------------------add month volume:---------------------------------");
+ req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w);
@@ -151,7 +198,6 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
- //console.log(sql);
console.log(sql)
Postgres.FirstRow(sql,[],res)
}
@@ -175,7 +221,8 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("------------------add month volume and price:-------------------");
+ console.log(new Date().toISOString() +"------------------add month volume and price:-------------------");
+ req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
@@ -185,7 +232,7 @@ server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
- //console.log(sql);
+ console.log(sql);
Postgres.FirstRow(sql,[],res)
}
@@ -217,7 +264,8 @@ server.post('/scale_v', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("-----------------------scale volume:------------------------------");
+ console.log(new Date().toISOString() +"-----------------------scale volume:------------------------------");
+ req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
@@ -225,7 +273,7 @@ server.post('/scale_v', bodyParser.json(), function (req, res) {
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
- //console.log(sql);
+ console.log(sql);
Postgres.FirstRow(sql,[],res)
}
@@ -257,7 +305,8 @@ server.post('/scale_p', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("--------------------scale price:-------------------");
+ console.log(new Date().toISOString() +"--------------------scale price:-------------------");
+ req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
@@ -265,7 +314,7 @@ server.post('/scale_p', bodyParser.json(), function (req, res) {
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
- //console.log(sql);
+ console.log(sql);
Postgres.FirstRow(sql,[],res)
}
@@ -297,7 +346,8 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
res.send("no body was sent");
return;
}
- console.log("--------------------scale volume & price:-------------------");
+ console.log(new Date().toISOString() +"--------------------scale volume & price:-------------------");
+ req.body.stamp = new Date().toISOString()
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
@@ -306,7 +356,93 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
- //console.log(sql);
+ console.log(sql);
+ Postgres.FirstRow(sql,[],res)
+ }
+
+ fs.readFile(path, 'utf8', function(err, data){
+ if (!err){
+ callback(data);
+ } else {
+ console.log("fatal error pulling sql file")
+ callback(err);
+ }
+ });
+})
+
+server.post('/new_part', bodyParser.json(), function (req, res) {
+
+ var sql = "";
+ var w = "";
+ var c = 1;
+ var d = 1;
+ var args = [];
+ var path = './route_sql/new_part.sql';
+
+ var callback = function(arg){
+ sql = arg;
+
+ ({ c, w, d } = build_where(req, c, w, d, args));
+
+ if (c == 1) {
+ res.send("no body was sent");
+ return;
+ }
+ console.log(new Date().toISOString() +"--------------------new part:-------------------");
+ req.body.stamp = new Date().toISOString()
+ console.log(req.body);
+ //console.log(args);
+ sql = sql.replace(new RegExp("where_clause",'g'),w);
+ sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
+ sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
+ sql = sql.replace(new RegExp("replace_request",'g'),JSON.stringify(req.body));
+ sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
+ sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
+ sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
+ console.log(sql);
+ Postgres.FirstRow(sql,[],res)
+ }
+
+ fs.readFile(path, 'utf8', function(err, data){
+ if (!err){
+ callback(data);
+ } else {
+ console.log("fatal error pulling sql file")
+ callback(err);
+ }
+ });
+})
+
+server.post('/new_basket', bodyParser.json(), function (req, res) {
+
+ var sql = "";
+ var w = "";
+ var c = 1;
+ var d = 1;
+ var args = [];
+ var path = './route_sql/new_basket.sql';
+
+ var callback = function(arg){
+ sql = arg;
+
+ ({ c, w, d } = build_where(req, c, w, d, args));
+
+ if (c == 1) {
+ res.send("no body was sent");
+ return;
+ }
+ console.log(new Date().toISOString() +"--------------------new basket:-------------------");
+ req.body.stamp = new Date().toISOString()
+ console.log(req.body);
+ //console.log(args);
+ sql = sql.replace(new RegExp("where_clause",'g'),w);
+ sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty);
+ sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount);
+ sql = sql.replace(new RegExp("replace_request",'g'),JSON.stringify(req.body));
+ sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version);
+ sql = sql.replace(new RegExp("replace_source",'g'),req.body.source);
+ sql = sql.replace(new RegExp("replace_iterdef",'g'),JSON.stringify(req.body));
+ console.log(sql);
Postgres.FirstRow(sql,[],res)
}
@@ -351,5 +487,3 @@ function build_where(req, c, w, d, args) {
;
return { c, w, d };
}
-
-server.listen(3000, () => console.log('started'))
\ No newline at end of file
diff --git a/msauth.html b/msauth.html
new file mode 100644
index 0000000..ee99f2f
--- /dev/null
+++ b/msauth.html
@@ -0,0 +1,137 @@
+
+
+
+ Microsoft Authentication
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/route_meta/addmonth_pd.sql b/route_meta/addmonth_pd.sql
deleted file mode 100644
index d20833e..0000000
--- a/route_meta/addmonth_pd.sql
+++ /dev/null
@@ -1,438 +0,0 @@
-WITH
-target AS (select 50000 incr)
-,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)
-)
-,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----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- 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
-)
-,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
-)
---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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,null::date orderdate --history date mix
- ,null::date requestdate --history date mix
- ,null::date shipdate --history date mix
- ,null::date adj_orderdate --history
- ,null::date adj_requestdate --history
- ,null::date adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------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_fcpool
- WHERE
- -----------------scenario----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- 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(fb_val_loc *r_rate) FROM basemix) base
- ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
- ,b.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
- ,0 fb_qty
- ,0 fb_cst_loc
- ,0 fb_cst_loc_cur
- ,0 fb_cst_loc_fut
- ,b.fb_val_loc*s.factor*m.momix fb_val_loc
- ,0 fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'adjust price' iterdet
- ,null::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.fb_val_loc*s.factor*m.momix value_loc
- ,b.fb_val_loc*s.factor*m.momix*r_rate value_usd
- ,0 cost_loc
- ,0 cost_usd
- ,0 units
-FROM
- basemix b
- CROSS JOIN scale s
- CROSS JOIN mmixp m
- LEFT OUTER JOIN mseq ON
- mseq.m = m._month
- 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 = 'Jul'
-)
-INSERT INTO rlarp.osm_fcpool SELECT * FROM final RETURNING *
\ No newline at end of file
diff --git a/route_meta/addmonth_vd.sql b/route_meta/addmonth_vd.sql
deleted file mode 100644
index 575dccb..0000000
--- a/route_meta/addmonth_vd.sql
+++ /dev/null
@@ -1,522 +0,0 @@
-WITH
-target AS (select 50000 incr)
-,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)
-)
-,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----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- 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 s, m from mseq where m = 'Jul' )
-,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
-)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,null::date orderdate --history date mix
- ,null::date requestdate --history date mix
- ,null::date shipdate --history date mix
- ,null::date adj_orderdate --history
- ,null::date adj_requestdate --history
- ,null::date adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------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_fcpool
- WHERE
- -----------------scenario----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- AND adj_orderdate <= adj_shipdate
- 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(fb_val_loc *r_rate) FROM basemix) base
- ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
- ,b.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.fb_qty*s.factor*m.momix fb_qty
- ,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.fb_val_loc*s.factor*m.momix fb_val_loc
- ,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'adjust volume' iterdet
- ,null::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.fb_val_loc*s.factor*m.momix)::numeric value_loc
- ,(b.fb_val_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.fb_qty*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_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 json_agg(row_to_json(insagg)) x from insagg
\ No newline at end of file
diff --git a/route_meta/addmonth_vupd.sql b/route_meta/addmonth_vupd.sql
deleted file mode 100644
index 8070f1d..0000000
--- a/route_meta/addmonth_vupd.sql
+++ /dev/null
@@ -1,647 +0,0 @@
-WITH
-/*
-the volume must be expressed in terms of units, since that is what it will be scaling
-*/
-target AS (select -50000 vincr, 20000 pincr)
-,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)
-)
-,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----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- 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 s, m from mseq where m = 'Jul' )
-,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
-)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,null::date orderdate --history date mix
- ,null::date requestdate --history date mix
- ,null::date shipdate --history date mix
- ,null::date adj_orderdate --history
- ,null::date adj_requestdate --history
- ,null::date adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------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_fcpool
- WHERE
- -----------------scenario----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- AND adj_orderdate <= adj_shipdate
- 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
-)
-,vscale AS (
- SELECT
- (SELECT vincr::numeric FROM target) incr
- ,(SELECT sum(fb_qty) FROM basemix) base
- ,(SELECT vincr::numeric FROM target)/(SELECT sum(fb_qty) FROM basemix) factor
-)
---select * from SCALE
-,volume AS (
-SELECT
- b.plnt --master data
- ,b."ddord#" --0
- ,b."dditm#" --0
- ,b."fgbol#" --0
- ,b."fgent#" --0
- ,b."diinv#" --0
- ,b."dilin#" --0
- ,b.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.fb_qty*s.factor*m.momix fb_qty
- ,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.fb_val_loc*s.factor*m.momix fb_val_loc
- ,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'adjust volume' iterdet
- ,null::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.fb_val_loc*s.factor*m.momix value_loc
- ,b.fb_val_loc*s.factor*m.momix*r_rate value_usd
- ,b.fb_cst_loc*s.factor*m.momix cost_loc
- ,b.fb_cst_loc*s.factor*m.momix*c_rate cost_usd
- ,(b.fb_qty*s.factor*m.momix) units
-FROM
- basemix b
- CROSS JOIN vscale 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)
-)
-,pscale AS (
- SELECT
- (SELECT pincr::numeric FROM target) incr
- ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
- ,(SELECT pincr::numeric FROM target)/(SELECT sum(fb_val_loc) FROM volume) factor
-)
-,price AS (
-SELECT
- b.plnt --master data
- ,b."ddord#" --0
- ,b."dditm#" --0
- ,b."fgbol#" --0
- ,b."fgent#" --0
- ,b."diinv#" --0
- ,b."dilin#" --0
- ,b.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.fb_qty
- ,b.fb_cst_loc
- ,b.fb_cst_loc_cur
- ,b.fb_cst_loc_fut
- ,b.fb_val_loc*p.factor fb_val_loc
- ,b.fb_val_loc_pl*p.factor fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,b.orderdate
- ,b.requestdate
- ,b.shipdate
- ,b.adj_orderdate
- ,b.adj_requestdate
- ,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'adjust p & v' iterdet
- ,null::jsonb iterdef
- ,b.order_season
- ,b.order_month
- ,b.ship_season
- ,b.ship_month
- ,b.request_season
- ,b.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.fb_val_loc*p.factor value_loc
- ,b.fb_val_loc*p.factor*r_rate value_usd
- ,b.fb_cst_loc cost_loc
- ,b.fb_cst_loc*c_rate cost_usd
- ,b.units
-FROM
- volume b
- CROSS JOIN pscale p
-)
-, ins AS (
- INSERT INTO rlarp.osm_fcpool SELECT * FROM price 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
diff --git a/route_meta/get_pool.sql b/route_meta/get_pool.sql
deleted file mode 100644
index 4616d95..0000000
--- a/route_meta/get_pool.sql
+++ /dev/null
@@ -1,80 +0,0 @@
-WITH rows AS (
-SELECT
- ---------customer info-----------------
- bill_cust_descr
- ,billto_group
- ,ship_cust_descr
- ,shipto_group
- ,quota_rep_descr
- ,director_descr
- ,segm
- ,mod_chan
- ,mod_chansub
- ---------product info------------------
- ,majg_descr
- ,ming_descr
- ,majs_descr
- ,mins_descr
- ,brand
- ,part_family
- ,part_group
- ,branding
- ,color
- ,part_descr
- ---------dates-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,request_month
- ,promo
- ,version
- ,iter
- --------values-------------------------
- ,sum(value_loc) value_loc
- ,sum(value_usd) value_usd
- ,sum(cost_loc) cost_loc
- ,sum(cost_usd) cost_usd
- ,sum(units) units
-FROM
- rlarp.osm_fcpool
-WHERE
- quota_rep_descr = '13025 - JAMES REGER'
-GROUP BY
- ---------customer info-----------------
- bill_cust_descr
- ,billto_group
- ,ship_cust_descr
- ,shipto_group
- ,quota_rep_descr
- ,director_descr
- ,segm
- ,mod_chan
- ,mod_chansub
- ---------product info------------------
- ,majg_descr
- ,ming_descr
- ,majs_descr
- ,mins_descr
- ,brand
- ,part_family
- ,part_group
- ,branding
- ,color
- ,part_descr
- ---------dates-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,request_month
- ,promo
- ,version
- ,iter
-)
-SELECT
- json_agg(row_to_json(rows))
-FROM
- rows
\ No newline at end of file
diff --git a/route_meta/scale_pd.sql b/route_meta/scale_pd.sql
deleted file mode 100644
index 64430bc..0000000
--- a/route_meta/scale_pd.sql
+++ /dev/null
@@ -1,390 +0,0 @@
-WITH
-target AS (select 50000 incr)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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_fcpool
- WHERE
- -----------------scenario----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- AND adj_orderdate <= adj_shipdate
- 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 --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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
-)
-,scale AS (
- SELECT
- (SELECT incr::numeric FROM target) incr
- ,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base
- ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
- ,b.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
- ,0 fb_qty
- ,0 fb_cst_loc
- ,0 fb_cst_loc_cur
- ,0 fb_cst_loc_fut
- ,b.fb_val_loc*s.factor fb_val_loc
- ,b.fb_val_loc_pl*s.factor fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,b.orderdate
- ,b.requestdate
- ,b.shipdate
- ,b.adj_orderdate
- ,b.adj_requestdate
- ,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'scale volume' iterdet
- ,null::jsonb iterdef
- ,b.order_season
- ,b.order_month
- ,b.ship_season
- ,b.ship_month
- ,b.request_season
- ,b.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.fb_val_loc*s.factor)::numeric value_loc
- ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
- ,0 cost_loc
- ,0 cost_usd
- ,0 units
-FROM
- basemix b
- CROSS JOIN scale s
-)
---select sum(value_usd), count(*) from final
-, 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 json_agg(row_to_json(insagg)) x from insagg
\ No newline at end of file
diff --git a/route_meta/scale_vupd.sql b/route_meta/scale_vupd.sql
deleted file mode 100644
index 1bac1ba..0000000
--- a/route_meta/scale_vupd.sql
+++ /dev/null
@@ -1,513 +0,0 @@
-WITH
-target AS (select -50000 vincr, 20000 pincr)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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_fcpool
- WHERE
- -----------------scenario----------------------------
- quota_rep_descr = '10032 - BRYAN HILL'
- AND segm = 'Greenhouse'
- AND mod_chan = 'DISTRIB DROP SHIP'
- AND billto_group = 'BWI'
- --AND order_month = 'May'
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- AND adj_orderdate <= adj_shipdate
- 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 --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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
-)
-,vscale AS (
- SELECT
- (SELECT vincr::numeric FROM target) incr
- ,(SELECT sum(fb_qty)::numeric FROM basemix) base
- ,(SELECT vincr::numeric FROM target)/(SELECT sum(fb_qty)::numeric FROM basemix) factor
-)
---select * from vscale
-,volume AS (
- SELECT
- b.plnt --master data
- ,b."ddord#" --0
- ,b."dditm#" --0
- ,b."fgbol#" --0
- ,b."fgent#" --0
- ,b."diinv#" --0
- ,b."dilin#" --0
- ,b.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.fb_qty*s.factor fb_qty
- ,b.fb_cst_loc*s.factor fb_cst_loc
- ,b.fb_cst_loc_cur*s.factor fb_cst_loc_cur
- ,b.fb_cst_loc_fut*s.factor fb_cst_loc_fut
- ,b.fb_val_loc*s.factor fb_val_loc
- ,b.fb_val_loc_pl*s.factor fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,b.orderdate
- ,b.requestdate
- ,b.shipdate
- ,b.adj_orderdate
- ,b.adj_requestdate
- ,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'scale vp' iterdet
- ,null::jsonb iterdef
- ,b.order_season
- ,b.order_month
- ,b.ship_season
- ,b.ship_month
- ,b.request_season
- ,b.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.fb_val_loc*s.factor)::numeric value_loc
- ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
- ,(b.fb_cst_loc*s.factor)::numeric cost_loc
- ,(b.fb_cst_loc*s.factor*c_rate)::numeric cost_usd
- ,(b.fb_qty*s.factor)::numeric units
- FROM
- basemix b
- CROSS JOIN vscale s
-)
-,pscale AS (
- SELECT
- (SELECT pincr::numeric FROM target) incr
- ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
- ,(SELECT pincr::numeric FROM target)/(SELECT sum(fb_val_loc) FROM volume) factor
- ,(select SUM(fb_qty) from volume) tvol
-)
---select * from pscale
-,pricing AS (
- SELECT
- b.plnt --master data
- ,b."ddord#" --0
- ,b."dditm#" --0
- ,b."fgbol#" --0
- ,b."fgent#" --0
- ,b."diinv#" --0
- ,b."dilin#" --0
- ,b.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.fb_qty
- ,b.fb_cst_loc
- ,b.fb_cst_loc_cur
- ,b.fb_cst_loc_fut
- ,b.fb_val_loc*s.factor fb_val_loc
- ,b.fb_val_loc_pl*s.factor fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,b.orderdate
- ,b.requestdate
- ,b.shipdate
- ,b.adj_orderdate
- ,b.adj_requestdate
- ,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,'scale vp' iterdet
- ,null::jsonb iterdef
- ,b.order_season
- ,b.order_month
- ,b.ship_season
- ,b.ship_month
- ,b.request_season
- ,b.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.fb_val_loc*s.factor)::numeric value_loc
- ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
- ,b.cost_loc
- ,b.cost_usd
- ,b.units
- FROM
- volume b
- CROSS JOIN pscale s
-)
---select sum(value_usd), sum(fb_qty) from pricing
-, ins AS (
- INSERT INTO rlarp.osm_fcpool SELECT * FROM pricing 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
\ No newline at end of file
diff --git a/route_meta/scenario_package.sql b/route_meta/scenario_package.sql
deleted file mode 100644
index d095ba4..0000000
--- a/route_meta/scenario_package.sql
+++ /dev/null
@@ -1,133 +0,0 @@
-\timing
---explain (analyze, buffers)
-WITH
-mseq AS (
- SELECT * FROM
- (
- VALUES
- ('Jun',1)
- ,('Jul',2)
- ,('Aug',3)
- ,('Sep',4)
- ,('Oct',5)
- ,('Nov',6)
- ,('Dec',7)
- ,('Jan',8)
- ,('Feb',9)
- ,('Mar',10)
- ,('Apr',11)
- ,('May',12)
-
- ) x(m,s)
-)
---select * from mseq
-,base AS (
-SELECT
- order_season
- ,order_month
- ,version
- ,iter
- ,part_descr
- ,bill_cust_descr
- ,ship_cust_descr
- ,SUM(units) units
- ,SUM(value_usd) value_usd
-FROM
- rlarp.osm_fcpool
-WHERE
- quota_rep_descr = '90005 - MARK WILKINSON'
- AND segm = 'Retail'
- AND mod_chan = 'MASS'
- AND order_month = 'May'
-GROUP BY
- order_season
- ,order_month
- ,version
- ,iter
- ,part_descr
- ,bill_cust_descr
- ,ship_cust_descr
-)
-,months AS (
- SELECT
- order_season
- ,version
- ,iter
- ,order_month
- ,mseq.s seq
- ,sum(units) units
- ,sum(value_usd) value_usd
- FROM
- base
- INNER JOIN mseq ON
- mseq.m = base.order_month
- GROUP BY
- order_season
- ,version
- ,iter
- ,order_month
- ,s
-)
-,mpvt AS (
- SELECT
- order_month
- ,seq
- ,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty"
- ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty"
- ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj qty"
- ,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot qty"
- ,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd"
- ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd"
- ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj value_usd"
- ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot value_usd"
- FROM
- months
- GROUP BY
- order_month
- ,seq
- ORDER BY
- seq ASC
-)
-,mlist AS (
- SELECT
- mseq.m order_month
- ,"2019 qty"
- ,"2020 base qty"
- ,"2020 adj qty"
- ,"2020 tot qty"
- ,"2019 value_usd"
- ,"2020 base value_usd"
- ,"2020 adj value_usd"
- ,"2020 tot value_usd"
- FROM
- mseq
- LEFT OUTER JOIN mpvt ON
- mpvt.order_month = mseq.m
- ORDER BY
- mseq.s ASC
-)
-,totals AS (
- SELECT
- order_season
- ,version
- ,iter
- ,sum(units) units
- ,sum(value_usd) value_usd
- FROM
- months
- GROUP BY
- order_season
- ,version
- ,iter
-)
-SELECT
- jsonb_build_object(
- 'months'
- ,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
- ,'mlist'
- ,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
- ,'base'
- ,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
- ,'totals'
- ,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
- ) package
\ No newline at end of file
diff --git a/route_sql/addmonth_vd.sql b/route_sql/addmonth_vd.sql
index 1277fbb..0c2370a 100644
--- a/route_sql/addmonth_vd.sql
+++ b/route_sql/addmonth_vd.sql
@@ -1,517 +1,517 @@
-/*----------------replace these tags--------------
-target_increment
-scenario = submitted
-target_month
-
----------------------------------------------------*/
-
-WITH
-target AS (select target_increment incr)
-,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)
-)
-,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----------------------------
- scenario = target_scenario
- -----------------additional params-------------------
- AND version = 'b20'
- AND iter = 'copy'
- 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 s, m from mseq where m = 'target_month' )
-,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
-)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,null::date orderdate --history date mix
- ,null::date requestdate --history date mix
- ,null::date shipdate --history date mix
- ,null::date adj_orderdate --history
- ,null::date adj_requestdate --history
- ,null::date adj_shipdate --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_fcpool
- WHERE
- -----------------scenario----------------------------
- scenario = target_scenario
- -----------------additional params-------------------
- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
- AND adj_orderdate <= adj_shipdate
- 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(fb_val_loc *r_rate) FROM basemix) base
- ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
- ,b.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.fb_qty*s.factor*m.momix fb_qty
- ,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.fb_val_loc*s.factor*m.momix fb_val_loc
- ,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
- ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
- ,'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.fb_val_loc*s.factor*m.momix)::numeric value_loc
- ,(b.fb_val_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.fb_qty*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_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
-)
+/*----------------replace these tags--------------
+target_increment
+scenario = submitted
+target_month
+
+---------------------------------------------------*/
+
+WITH
+target AS (select target_increment incr)
+,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)
+)
+,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----------------------------
+ scenario = target_scenario
+ -----------------additional params-------------------
+ AND version = 'b20'
+ AND iter = 'copy'
+ 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 s, m from mseq where m = 'target_month' )
+,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
+)
+,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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
+ ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
+ ,calc_status --0
+ ,flag --0
+ ,null::date orderdate --history date mix
+ ,null::date requestdate --history date mix
+ ,null::date shipdate --history date mix
+ ,null::date adj_orderdate --history
+ ,null::date adj_requestdate --history
+ ,null::date adj_shipdate --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_fcpool
+ WHERE
+ -----------------scenario----------------------------
+ scenario = target_scenario
+ -----------------additional params-------------------
+ AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
+ AND adj_orderdate <= adj_shipdate
+ 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(fb_val_loc *r_rate) FROM basemix) base
+ ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
+ ,b.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.fb_qty*s.factor*m.momix fb_qty
+ ,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.fb_val_loc*s.factor*m.momix fb_val_loc
+ ,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl
+ ,b.calc_status --0
+ ,b.flag --0
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate
+ ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate
+ ,'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.fb_val_loc*s.factor*m.momix)::numeric value_loc
+ ,(b.fb_val_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.fb_qty*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_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 json_agg(row_to_json(insagg)) x from insagg
\ No newline at end of file
diff --git a/route_sql/addmonth_vupd.sql b/route_sql/addmonth_vupd.sql
index ee56eab..a876885 100644
--- a/route_sql/addmonth_vupd.sql
+++ b/route_sql/addmonth_vupd.sql
@@ -145,8 +145,8 @@ SELECT
,terms
,''::text custpo --0
,'I' dhincr --0
- ,sum(diext) diext
- ,sum(ditdis) ditdis
+ ,null::numeric diext
+ ,null::numeric ditdis
,null::date dcodat --calculated date mix
,null::date ddqdat --calculated date mix
,null::date dcmdat --calculated date mix
@@ -445,7 +445,8 @@ WHERE
SELECT
(SELECT pincr::numeric FROM target) incr
,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
- ,(SELECT pincr::numeric FROM target)/(SELECT sum(fb_val_loc) FROM volume) factor
+ --(target_sales - current_state)/current state = adjustment factor
+ ,((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume) factor
)
,price AS (
SELECT
@@ -509,10 +510,10 @@ SELECT
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --0
- ,b.fb_qty
- ,b.fb_cst_loc
- ,b.fb_cst_loc_cur
- ,b.fb_cst_loc_fut
+ ,0 fb_qty
+ ,0 fb_cst_loc
+ ,0 fb_cst_loc_cur
+ ,0 fb_cst_loc_fut
,b.fb_val_loc*p.factor fb_val_loc
,b.fb_val_loc_pl*p.factor fb_val_loc_pl
,b.calc_status --0
@@ -523,11 +524,10 @@ SELECT
,b.adj_orderdate
,b.adj_requestdate
,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
- -----------------------ui columns--------------------------------
- ,b.iterdet
- ,b.iterdef
+ ,'replace_version' "version" --calculated
+ ,'replace_source'||' price' iter --calculateds
+ ,'replace_iterdet' iterdet
+ ,$$replace_iterdef$$::jsonb iterdef
,b.order_season
,b.order_month
,b.ship_season
@@ -554,15 +554,15 @@ SELECT
,b.director_descr
,b.fb_val_loc*p.factor value_loc
,b.fb_val_loc*p.factor*r_rate value_usd
- ,b.fb_cst_loc cost_loc
- ,b.fb_cst_loc*c_rate cost_usd
- ,b.units
+ ,0 cost_loc
+ ,0 cost_usd
+ ,0 units
FROM
volume b
CROSS JOIN pscale p
)
, ins AS (
- INSERT INTO rlarp.osm_fcpool SELECT * FROM price RETURNING *
+ INSERT INTO rlarp.osm_fcpool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING *
)
,insagg AS (
SELECT
diff --git a/route_sql/build/build_pool.sql b/route_sql/build/build_pool.sql
new file mode 100644
index 0000000..00eee29
--- /dev/null
+++ b/route_sql/build/build_pool.sql
@@ -0,0 +1,518 @@
+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;
\ No newline at end of file
diff --git a/route_sql/build/copy_orders.sql b/route_sql/build/copy_orders.sql
new file mode 100644
index 0000000..2242194
--- /dev/null
+++ b/route_sql/build/copy_orders.sql
@@ -0,0 +1,547 @@
+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;
\ No newline at end of file
diff --git a/route_sql/build/last_price.sql b/route_sql/build/last_price.sql
new file mode 100644
index 0000000..10c2d18
--- /dev/null
+++ b/route_sql/build/last_price.sql
@@ -0,0 +1,120 @@
+BEGIN;
+
+with p 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
+)
+--SELECT * FROM p WHERE rn = 1
+,incr AS (
+ SELECT
+ b.plnt
+ ,b."ddord#"
+ ,b."dditm#"
+ ,b."fgbol#"
+ ,b."fgent#"
+ ,b."diinv#"
+ ,b."dilin#"
+ ,b.promo
+ ,b.return_reas
+ ,b.terms
+ ,b.custpo
+ ,b.dhincr
+ ,b.diext
+ ,b.ditdis
+ ,b.dcodat
+ ,b.ddqdat
+ ,b.dcmdat
+ ,b.dhidat
+ ,b.fspr
+ ,b.remit_to
+ ,b.bill_class
+ ,b.bill_cust
+ ,b.bill_rep
+ ,b.bill_terr
+ ,b.ship_class
+ ,b.ship_cust
+ ,b.ship_rep
+ ,b.ship_terr
+ ,b.quota_rep
+ ,b.account
+ ,b.shipgrp
+ ,b.geo
+ ,b.chan
+ ,b.orig_ctry
+ ,b.orig_prov
+ ,b.orig_post
+ ,b.dest_ctry
+ ,b.dest_prov
+ ,b.dest_post
+ ,b.part
+ ,b.ord_gldc
+ ,b.majg
+ ,b.ming
+ ,b.majs
+ ,b.mins
+ ,b.gldc
+ ,b.glec
+ ,b.harm
+ ,b.clss
+ ,b.brand
+ ,b.assc
+ ,b.fs_line
+ ,b.r_currency
+ ,b.r_rate
+ ,b.c_currency
+ ,b.c_rate
+ ,b.ddqtoi
+ ,b.ddqtsi
+ ,b.fgqshp
+ ,b.diqtsh
+ ,0 fb_qty
+ ,0 fb_cst_loc
+ ,0 fb_cst_loc_cur
+ ,0 fb_cst_loc_fut
+ ,b.fb_qty * p.price - b.fb_val_loc fb_val_loc
+ ,0 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"
+ ,'last price paid' iter
+ FROM
+ rlarp.osmfs b
+ INNER JOIN p ON
+ p.part = b.part
+ AND p.bill_cust = b.bill_cust
+ AND p.ship_cust = b.ship_cust
+ AND p.rn = 1
+ WHERE
+ b.orderdate BETWEEN '2019-03-25' AND '2019-05-31'
+ OR b.orderdate BETWEEN '2020-03-25' AND '2020-05-31'
+)
+, ins AS (
+ INSERT INTO rlarp.osmfs SELECT * FROM incr RETURNING *
+)
+SELECT fspr, sum(fb_val_loc *r_rate) FROM ins GROUP BY fspr;
+
+COMMIT;
\ No newline at end of file
diff --git a/route_sql/build/list_changes_table.sql b/route_sql/build/list_changes_table.sql
new file mode 100644
index 0000000..3011b65
--- /dev/null
+++ b/route_sql/build/list_changes_table.sql
@@ -0,0 +1,17 @@
+SELECT
+ iterdef->>'user' "user",
+ (iterdef->>'stamp')::timestamptz "stamp",
+ iterdef->>'mesasage' "comment",
+ round(SUM(fb_val_loc * r_rate)::numeric,2) sales,
+ jsonb_pretty(iterdef) def
+FROM
+ rlarp.osm_fcpool
+WHERE
+ iterdef IS NOT NULL
+GROUP BY
+ iterdef->>'user',
+ iterdef->>'stamp',
+ iterdef->>'message',
+ iterdef
+ORDER BY
+ "stamp" DESC
diff --git a/route_sql/build/open_timing.sql b/route_sql/build/open_timing.sql
new file mode 100644
index 0000000..c8772cd
--- /dev/null
+++ b/route_sql/build/open_timing.sql
@@ -0,0 +1,96 @@
+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;
\ No newline at end of file
diff --git a/route_sql/build/set_fspr_gld.sql b/route_sql/build/set_fspr_gld.sql
new file mode 100644
index 0000000..c975728
--- /dev/null
+++ b/route_sql/build/set_fspr_gld.sql
@@ -0,0 +1,29 @@
+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
\ No newline at end of file
diff --git a/route_sql/list_changes.sql b/route_sql/list_changes.sql
new file mode 100644
index 0000000..596c056
--- /dev/null
+++ b/route_sql/list_changes.sql
@@ -0,0 +1,21 @@
+WITH
+list AS (
+SELECT
+ iterdef->>'user' "user",
+ iterdef->>'stamp' "stamp",
+ iterdef->>'mesasage' "comment",
+ round(SUM(fb_val_loc * r_rate)::numeric,2) sales,
+ jsonb_pretty(iterdef) def
+FROM
+ rlarp.osm_fcpool
+WHERE
+ iterdef IS NOT NULL
+ AND iterdef @> $$replace_user$$::jsonb
+GROUP BY
+ iterdef->>'user',
+ iterdef->>'stamp',
+ iterdef->>'message',
+ iterdef
+)
+SELECT
+ jsonb_agg(row_to_json(list)::jsonb) x from list
\ No newline at end of file
diff --git a/route_sql/new_basket.sql b/route_sql/new_basket.sql
new file mode 100644
index 0000000..c4ed2f7
--- /dev/null
+++ b/route_sql/new_basket.sql
@@ -0,0 +1,787 @@
+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
+ --probably dont want to include adjustments that have blown away volume and good sources of dating info
+ -----------------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 mxm
+--this basemix is probably join going to be part-bill-ship with all master data joined later since it is not known if any original is salvaged
+,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#"
+ ,mxm.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
+ ,sd.fspr
+ ,null::text remit_to
+ ,null::text bill_class
+ ,(regexp_match(jr.bill_cust_descr,'(.*?)(?= - |$)'))[1] bill_cust
+ ,null:: text bill_rep
+ ,null::text bill_terr
+ ,null::text ship_class
+ ,(regexp_match(jr.ship_cust_descr,'(.*?)(?= - |$)'))[1] ship_cust
+ ,null::text ship_rep
+ ,null::text ship_terr
+ ,null::text quota_rep
+ ,null::text account
+ ,null::text shipgrp
+ ,null::text geo
+ ,null::text chan
+ ,null::text orig_ctry
+ ,null::text orig_prov
+ ,null::text orig_post
+ ,null::text dest_ctry
+ ,null::text dest_prov
+ ,null::text dest_post
+ ,(regexp_match(jr.part_descr,'(.*?)(?= - |$)'))[1] part
+ ,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
+ ,'41010'::text fs_line
+ ,null::text r_currency
+ ,null::numeric 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
+ ,mxm.qty*mxm.momix*jr.mix fb_qty --history value
+ ,0::numeric fb_cst_loc --history part mix
+ ,0::numeric fb_cst_loc_cur
+ ,0::numeric fb_cst_loc_fut
+ ,mxm.amount*mxm.momix*jr.mix fb_val_loc --history value
+ ,0::numeric fb_val_loc_pl
+ ,''::text calc_status
+ ,''::text 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
+ ,jr.part_descr part_descr
+ ,null::text part_family
+ ,null::text part_group
+ ,null::text branding
+ ,null::text color
+ ,null::text segm
+ ,jr.bill_cust_descr
+ ,null::text billto_group
+ ,jr.ship_cust_descr
+ ,null::text shipto_group
+ ,null::text majg_descr
+ ,null::text ming_descr
+ ,null::text majs_descr
+ ,null::text mins_descr
+ ,null::text mod_chan
+ ,null::text mod_chansub
+ ,null::text quota_rep_descr
+ ,null::text director_descr
+ ,null::numeric value_loc
+ ,null::numeric value_usd
+ ,null::numeric cost_loc
+ ,null::numeric cost_usd
+ ,null::numeric units
+ ,jr.mix
+ FROM
+ target
+ JOIN LATERAL json_array_elements(def->'basket') as ae(e) ON true
+ JOIN LATERAL json_to_record(ae.e) as jr(part_descr text, bill_cust_descr text, ship_cust_descr text, mix numeric) ON true
+ 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
+)
+--SELECT * FROM basemix
+,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
+ ,bc.bvcomp remit_to --master data
+ ,bc.bvclas bill_class --master data
+ ,b.bill_cust --history cust mix
+ ,bc.bvsalm bill_rep --master data
+ ,bc.bvterr bill_terr --master data
+ ,sc.bvclas ship_class --master data
+ ,b.ship_cust --history cust mix
+ ,sc.bvsalm ship_rep --master data
+ ,sc.bvterr ship_terr --master data
+ --quota rep--------------------------------------------
+ ,RTRIM(
+ COALESCE(
+ --retail items go to currep, or if null go to 90005
+ CASE WHEN COALESCE(avgled,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(avming,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
+ ,coalesce(cg.cgrp,bc.bvname) account
+ ,coalesce(sg.cgrp,sc.bvname) shipgrp
+ ,coalesce(t.geo,'UNDEFINED') geo
+ ,coalesce(c.chan,'UNDEFINED') chan
+ ,ad.qzcryc orig_ctry
+ ,ad.qzprov orig_prov
+ ,ad.qzpost orig_post
+ ,sc.bvctry dest_ctry
+ ,sc.bvprcd dest_prov
+ ,sc.bvpost dest_post
+ ,b.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
+ ,bc.bvcurr r_currency --history cust mix
+ ,rx.rate 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_descr
+ ,SUBSTR(b.part,1,3)||coalesce(' - '||F.DESCR,'') part_family
+ ,SUBSTRING(b.part,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(b.part,9,3) ELSE '' END color
+ ,COALESCE(SEG.SEGM,'Other') segm
+ ,b.bill_cust_descr
+ ,coalesce(cg.cgrp,bc.bvname) billto_group
+ ,b.ship_cust_descr
+ ,coalesce(sg.cgrp,sc.bvname) 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 c.RETAIL
+ ELSE coalesce(c.chan,'UNDEFINED')
+ END mod_chan
+ ,CASE COALESCE(SEG.SEGM,'Other')
+ WHEN 'Retail' THEN c.RETAILSUB
+ ELSE ''
+ END mod_chansub
+ ,repc.repp quota_rep_descr
+ ,repc.director director_descr
+ ,b.fb_val_loc value_loc
+ ,b.fb_val_loc * rx.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
+ basemix 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 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.FFCHNL c ON
+ c.BILL = bc.bvclas
+ AND c.SHIP = sc.bvclas
+ LEFT OUTER JOIN rlarp.ffterr t ON
+ t.prov = sc.bvprcd AND
+ t.ctry = sc.bvctry
+ 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(avgled,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(avming,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
+ ,''
+ )
+ )
+ 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'
+ LEFT OUTER JOIN rlarp.ffcret rx ON
+ rx.perd = '1910'
+ AND rx.rtyp = 'MA'
+ AND rx.fcur = bc.bvcurr
+ AND rx.tcur = 'US'
+ LEFT OUTER JOIN LGDAT.ADRS AD ON
+ AD.QZADR = plnt."yaadr#"
+)
+/*
+,clone_part_meta AS (
+ SELECT
+ 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.segm
+ ,b.majg_descr
+ ,b.ming_descr
+ ,b.majs_descr
+ ,b.mins_descr
+ ,b.quota_rep
+ ,sum(fb_val_loc * r_rate) agg
+ FROM
+ rlarp.osm_fcpool b
+ WHERE
+ -----------------scenario----------------------------
+ where_clause
+ --probably dont want to include adjustments that have blown away volume and good sources of dating info
+ -----------------additional params-------------------
+ AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
+ AND adj_orderdate <= adj_shipdate
+ GROUP BY
+ 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.segm
+ ,b.majg_descr
+ ,b.ming_descr
+ ,b.majs_descr
+ ,b.mins_descr
+ ,b.quota_rep
+ ORDER BY
+ agg desc
+)
+*/
+, 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 json_agg(row_to_json(insagg)) x from insagg
diff --git a/route_sql/new_part.sql b/route_sql/new_part.sql
new file mode 100644
index 0000000..ebc3cad
--- /dev/null
+++ b/route_sql/new_part.sql
@@ -0,0 +1,803 @@
+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
\ No newline at end of file
diff --git a/route_sql/part_list.sql b/route_sql/part_list.sql
new file mode 100644
index 0000000..33e388c
--- /dev/null
+++ b/route_sql/part_list.sql
@@ -0,0 +1,23 @@
+WITH
+act As (
+ SELECT DISTINCT
+ v6part part
+ FROM
+ lgdat.stka
+ WHERE
+ v6stat = 'A'
+)
+,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
\ No newline at end of file
diff --git a/route_sql/scale_pd.sql b/route_sql/scale_pd.sql
index 81145bc..0c6110e 100644
--- a/route_sql/scale_pd.sql
+++ b/route_sql/scale_pd.sql
@@ -14,8 +14,8 @@ SELECT
,terms
,''::text custpo --0
,'I' dhincr --0
- ,sum(diext) diext
- ,sum(ditdis) ditdis
+ ,null::numeric diext
+ ,null::numeric ditdis
,null::date dcodat --calculated date mix
,null::date ddqdat --calculated date mix
,null::date dcmdat --calculated date mix
diff --git a/route_sql/scale_vd.sql b/route_sql/scale_vd.sql
index 8148cf3..5e9e5d6 100644
--- a/route_sql/scale_vd.sql
+++ b/route_sql/scale_vd.sql
@@ -1,384 +1,384 @@
-WITH
-target AS (select target_increment incr)
-,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
- ,sum(diext) diext
- ,sum(ditdis) 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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
- ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
- ,calc_status --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ,'b20' "version" --calculated
- ,'adjustment' iter --calculated
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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_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
- 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 --0
- ,flag --0
- ,orderdate --history date mix
- ,requestdate --history date mix
- ,shipdate --history date mix
- ,adj_orderdate --history
- ,adj_requestdate --history
- ,adj_shipdate --history
- ---------------ui columns-------------------------
- ,order_season
- ,order_month
- ,ship_season
- ,ship_month
- ,request_season
- ,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
-)
-,scale AS (
- SELECT
- (SELECT incr::numeric FROM target) incr
- ,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base
- ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
- ,b.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.fb_qty*s.factor fb_qty
- ,b.fb_cst_loc*s.factor fb_cst_loc
- ,b.fb_cst_loc_cur*s.factor fb_cst_loc_cur
- ,b.fb_cst_loc_fut*s.factor fb_cst_loc_fut
- ,b.fb_val_loc*s.factor fb_val_loc
- ,b.fb_val_loc_pl*s.factor fb_val_loc_pl
- ,b.calc_status --0
- ,b.flag --0
- ,b.orderdate
- ,b.requestdate
- ,b.shipdate
- ,b.adj_orderdate
- ,b.adj_requestdate
- ,b.adj_shipdate
- ,'replace_version' "version" --calculated
- ,'replace_source'||' volume' iter
- -----------------------ui columns--------------------------------
- ,'replace_iterdet' iterdet
- ,$$replace_iterdef$$::jsonb iterdef
- ,b.order_season
- ,b.order_month
- ,b.ship_season
- ,b.ship_month
- ,b.request_season
- ,b.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.fb_val_loc*s.factor)::numeric value_loc
- ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
- ,(b.fb_cst_loc*s.factor)::numeric cost_loc
- ,(b.fb_cst_loc*s.factor*c_rate)::numeric cost_usd
- ,(b.fb_qty*s.factor)::numeric units
-FROM
- basemix b
- CROSS JOIN scale s
-)
---select sum(value_usd), count(*) from final
-, 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
-)
+WITH
+target AS (select target_increment incr)
+,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(fb_qty,0)) fb_qty --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(fb_val_loc,0)) fb_val_loc --history value
+ ,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0
+ ,calc_status --0
+ ,flag --0
+ ,orderdate --history date mix
+ ,requestdate --history date mix
+ ,shipdate --history date mix
+ ,adj_orderdate --history
+ ,adj_requestdate --history
+ ,adj_shipdate --history
+ ,'b20' "version" --calculated
+ ,'adjustment' iter --calculated
+ ---------------ui columns-------------------------
+ ,order_season
+ ,order_month
+ ,ship_season
+ ,ship_month
+ ,request_season
+ ,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_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
+ 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 --0
+ ,flag --0
+ ,orderdate --history date mix
+ ,requestdate --history date mix
+ ,shipdate --history date mix
+ ,adj_orderdate --history
+ ,adj_requestdate --history
+ ,adj_shipdate --history
+ ---------------ui columns-------------------------
+ ,order_season
+ ,order_month
+ ,ship_season
+ ,ship_month
+ ,request_season
+ ,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
+)
+,scale AS (
+ SELECT
+ (SELECT incr::numeric FROM target) incr
+ ,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base
+ ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) 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
+ ,b.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.fb_qty*s.factor fb_qty
+ ,b.fb_cst_loc*s.factor fb_cst_loc
+ ,b.fb_cst_loc_cur*s.factor fb_cst_loc_cur
+ ,b.fb_cst_loc_fut*s.factor fb_cst_loc_fut
+ ,b.fb_val_loc*s.factor fb_val_loc
+ ,b.fb_val_loc_pl*s.factor fb_val_loc_pl
+ ,b.calc_status --0
+ ,b.flag --0
+ ,b.orderdate
+ ,b.requestdate
+ ,b.shipdate
+ ,b.adj_orderdate
+ ,b.adj_requestdate
+ ,b.adj_shipdate
+ ,'replace_version' "version" --calculated
+ ,'replace_source'||' volume' iter
+ -----------------------ui columns--------------------------------
+ ,'replace_iterdet' iterdet
+ ,$$replace_iterdef$$::jsonb iterdef
+ ,b.order_season
+ ,b.order_month
+ ,b.ship_season
+ ,b.ship_month
+ ,b.request_season
+ ,b.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.fb_val_loc*s.factor)::numeric value_loc
+ ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
+ ,(b.fb_cst_loc*s.factor)::numeric cost_loc
+ ,(b.fb_cst_loc*s.factor*c_rate)::numeric cost_usd
+ ,(b.fb_qty*s.factor)::numeric units
+FROM
+ basemix b
+ CROSS JOIN scale s
+)
+--select sum(value_usd), count(*) from final
+, 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 json_agg(row_to_json(insagg)) x from insagg
\ No newline at end of file
diff --git a/route_sql/scale_vupd.sql b/route_sql/scale_vupd.sql
index e072aba..16e73cc 100644
--- a/route_sql/scale_vupd.sql
+++ b/route_sql/scale_vupd.sql
@@ -14,8 +14,8 @@ target AS (select target_vol vincr, target_prc pincr)
,terms
,''::text custpo --0
,'I' dhincr --0
- ,sum(diext) diext
- ,sum(ditdis) ditdis
+ ,null::numeric diext
+ ,null::numeric ditdis
,null::date dcodat --calculated date mix
,null::date ddqdat --calculated date mix
,null::date dcmdat --calculated date mix
@@ -315,8 +315,7 @@ target AS (select target_vol vincr, target_prc pincr)
SELECT
(SELECT pincr::numeric FROM target) incr
,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
- ,(SELECT pincr::numeric FROM target)/(SELECT sum(fb_val_loc) FROM volume) factor
- ,(select SUM(fb_qty) from volume) tvol
+ ,((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume) factor
)
--select * from pscale
,pricing AS (
@@ -381,10 +380,10 @@ target AS (select target_vol vincr, target_prc pincr)
,b.ddqtsi --0
,b.fgqshp --0
,b.diqtsh --0
- ,b.fb_qty
- ,b.fb_cst_loc
- ,b.fb_cst_loc_cur
- ,b.fb_cst_loc_fut
+ ,0 fb_qty
+ ,0 fb_cst_loc
+ ,0 fb_cst_loc_cur
+ ,0 fb_cst_loc_fut
,b.fb_val_loc*s.factor fb_val_loc
,b.fb_val_loc_pl*s.factor fb_val_loc_pl
,b.calc_status --0
@@ -395,11 +394,11 @@ target AS (select target_vol vincr, target_prc pincr)
,b.adj_orderdate
,b.adj_requestdate
,b.adj_shipdate
- ,b.version --calculated
- ,b.iter --calculated
+ ,'replace_version' "version" --calculated
+ ,'replace_source'||' price' iter
-----------------------ui columns--------------------------------
- ,b.iterdet
- ,b.iterdef
+ ,'replace_iterdet' iterdet
+ ,$$replace_iterdef$$::jsonb iterdef
,b.order_season
,b.order_month
,b.ship_season
@@ -426,16 +425,16 @@ target AS (select target_vol vincr, target_prc pincr)
,b.director_descr
,(b.fb_val_loc*s.factor)::numeric value_loc
,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd
- ,b.cost_loc
- ,b.cost_usd
- ,b.units
+ ,0 cost_loc
+ ,0 cost_usd
+ ,0 units
FROM
volume b
CROSS JOIN pscale s
)
--select sum(value_usd), sum(fb_qty) from pricing
, ins AS (
- INSERT INTO rlarp.osm_fcpool SELECT * FROM pricing RETURNING *
+ INSERT INTO rlarp.osm_fcpool (SELECT * FROM pricing UNION ALL SELECT * FROM volume) RETURNING *
)
,insagg AS (
SELECT
diff --git a/route_sql/scenario_package.sql b/route_sql/scenario_package.sql
index 0d3ceac..5a7e9c0 100644
--- a/route_sql/scenario_package.sql
+++ b/route_sql/scenario_package.sql
@@ -36,6 +36,7 @@ FROM
rlarp.osm_fcpool
WHERE
where_clause
+ AND order_season IN (2019,2020)
GROUP BY
order_season
,order_month
@@ -45,6 +46,30 @@ GROUP BY
,bill_cust_descr
,ship_cust_descr
)
+, basket1 AS (
+ SELECT
+ part_descr
+ ,bill_cust_descr
+ ,ship_cust_descr
+ ,SUM(value_usd) value_usd
+ FROM
+ base
+ GROUP BY
+ part_descr
+ ,bill_cust_descr
+ ,ship_cust_descr
+)
+,basket AS (
+ SELECT
+ part_descr
+ ,bill_cust_descr
+ ,ship_cust_descr
+ ,CASE WHEN sum(value_usd) over () = 0 THEN 0 ELSE value_usd/sum(value_usd) over () END mix
+ FROM
+ basket1
+ ORDER BY
+ mix DESC
+)
,months AS (
SELECT
order_season
@@ -70,12 +95,12 @@ GROUP BY
order_month
,seq
,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty"
- ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty"
- ,SUM(units) FILTER (WHERE order_season = 2020 AND iter <> 'copy') "2020 adj qty"
+ ,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','short ship','bad_ship')) "2020 base qty"
+ ,SUM(units) FILTER (WHERE order_season = 2020 AND iter NOT IN ('copy','short ship','bad_ship')) "2020 adj qty"
,SUM(units) FILTER (WHERE order_season = 2020) "2020 tot qty"
,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd"
- ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd"
- ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter <> 'copy') "2020 adj value_usd"
+ ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','short ship','bad_ship')) "2020 base value_usd"
+ ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter NOT IN ('copy','short ship','bad_ship')) "2020 adj value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2020) "2020 tot value_usd"
FROM
months
@@ -123,8 +148,8 @@ SELECT
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
,'mpvt'
,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
- ,'base'
- ,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
+ ,'basket'
+ ,(SELECT jsonb_agg(row_to_json(basket)::jsonb) FROM basket)
,'totals'
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
) package
\ No newline at end of file