Switch source table from rlarp.itemmv to "CMS.CUSLG".itemm

This commit is contained in:
PhilRunninger 2023-03-01 17:55:30 -05:00
parent 59cb926d9c
commit ed5975daf7
6 changed files with 99 additions and 98 deletions

View File

@ -1,10 +1,11 @@
-- Connection: usmidsap01.ubm
--\timing
TRUNCATE TABLE rlarp.osmf;
INSERT INTO rlarp.osmf
WITH
gld AS (
SELECT
SELECT
N1COMP COMP
,N1CCYY FSYR
,KPMAXP PERDS
@ -13,13 +14,13 @@ gld AS (
,N1SD01 SDAT
,N1ED01 EDAT
,to_char(N1ED01,'yymm') CAPR
,N1ED01 - N1SD01 +1 NDAYS
,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
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
KPCCYY = N1CCYY
WHERE
N1COMP = 93
@ -286,7 +287,7 @@ gld AS (
,o.calc_status
,o.flag
,o.odate + interval '1 year' odate
,o.oseas + 1 rseas
,o.oseas + 1 rseas
,o.rdate + interval '1 year' rdate
,o.rseas + 1 rseas
,o.pdate + interval '1 year' pdate
@ -434,7 +435,7 @@ gld AS (
,o.calc_status
,o.flag
,o.odate odate
,o.oseas rseas
,o.oseas rseas
,o.rdate rdate
,o.rseas rseas
,o.pdate pdate
@ -479,7 +480,7 @@ gld AS (
,ss.ssyr
)
,incr AS (
SELECT
SELECT
o."ddord#"
,o."dditm#"
,o."fgbol#"
@ -540,10 +541,10 @@ SELECT
,o.coltier
,o.colstat
,o.sizc
,o.uomp
,o.suffix
,o.uomp
,o.suffix
,o.accs_ps
,o.brnd
,o.brnd
,o.majg
,o.ming
,o.majs
@ -586,7 +587,7 @@ SELECT
,o.sseas + 1 --incremented
,'b22' "version"
,'copy' iter
FROM
FROM
baseline o
LEFT OUTER JOIN gld ON
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
@ -661,11 +662,11 @@ SELECT
,colgrp
,coltier
,colstat
,sizc
,uomp
,suffix
,accs_ps
,brnd
,sizc
,uomp
,suffix
,accs_ps
,brnd
,majg
,ming
,majs
@ -704,8 +705,8 @@ SELECT
,oseas
,rdate
,rseas
,pdate
,pseas
,pdate
,pseas
,sdate
,sseas
,"version"

View File

@ -39,7 +39,7 @@ SELECT DISTINCT
,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
FROM
rlarp.osm_pool o
LEFT OUTER JOIN rlarp.itemmv i ON
LEFT OUTER JOIN "CMS.CUSLG".itemmv i ON
i.item = o.part
LEFT OUTER JOIN seg ON
seg.glec = i.glec

View File

@ -1,6 +1,6 @@
--BEGIN;
WITH
WITH
------------------goal price increases---------------------
incr AS (
SELECT * FROM (VALUES
@ -99,7 +99,7 @@ incr AS (
WHERE
pricing <> ''
AND dba <> ''
GROUP BY
GROUP BY
dba
)
----customer dba price list pricing------
@ -154,11 +154,11 @@ incr AS (
,row_number() OVER (PARTITION BY o.styc||'.'||o.colgrp||substring(o.sizc,1,3),o.account, o.shipgrp ORDER BY o.odate DESC) rn
FROM
rlarp.osm_dev o
INNER JOIN rlarp.itemmv i ON
INNER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part
WHERE
---exclude R&A's
o.fs_line = '41010'
o.fs_line = '41010'
---exclude canceled orders
AND o.calc_status <> 'CANCELED'
---exclude quotes
@ -207,7 +207,7 @@ incr AS (
,assc
,chgrp
)
--SELECT * FROM baseline WHERE product = 'AMK06000.CBXX'
--SELECT * FROM baseline WHERE product = 'AMK06000.CBXX'
----------calculate pricing as it sits in the forecast--------------
,poolprice AS (
SELECT
@ -226,7 +226,7 @@ SELECT
,jsonb_agg(DISTINCT iter) iters
FROM
rlarp.osm_pool o
,rlarp.itemmv i
,"CMS.CUSLG".itemm i
WHERE
i.item = o.part
--AND o.units <> 0
@ -249,7 +249,7 @@ HAVING
)
----------pivot the pricing out into columns per customer/product--------
,pivot AS (
SELECT
SELECT
p.productt
,p.product
--,p.styc
@ -275,7 +275,7 @@ SELECT
WHEN 'D' THEN i.rate
ELSE CASE p.majg
WHEN '610' THEN .02
ElSE CASE p.colgrp
ElSE CASE p.colgrp
WHEN 'B' THEN .20
WHEN 'C' THEN .25
ELSE 1
@ -285,7 +285,7 @@ SELECT
,ms.avg_price target
----------------potential price list increase of 5% across board-----
,JSONB_AGG(DISTINCT (plist.jcpric*1.05)/1000) jcprice
--,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
--,CASE WHEN sum(val_usd) FILTER (WHERE oseas = 2020) IS NULL
-- THEN CASE WHEN sum(units) FILTER (WHERE oseas = 2021) IS NULL
-- THEN 'unknown'
-- ELSE 'new'
@ -295,7 +295,7 @@ SELECT
-- ELSE 'repeat'
-- END
--END flag
FROM
FROM
p
LEFT OUTER JOIN baseline bl ON
bl.product = p.product
@ -353,29 +353,29 @@ GROUP BY
--,log AS (
-- INSERT INTO
-- rlarp.osm_log(doc)
-- SELECT
-- SELECT
-- $${
-- "message":"application of last price and target increases to all forecast orders",
-- "tag":"last price r1",
-- "type":"build"
-- }$$::jsonb doc
-- "type":"build"
-- }$$::jsonb doc
-- RETURNING *
--)
---------collapse iterations-----------------------
,collapse AS (
SELECT
o.fspr
,o.plnt ---master data
,o.plnt ---master data
,o.promo --history date mix
,o.terms
,o.bill_cust_descr --history cust mix
,o.ship_cust_descr --history cust mix
,o.dsm
,o.quota_rep_descr --master data
,o.quota_rep_descr --master data
,o.director
,o.billto_group --master data
,o.billto_group --master data
,o.shipto_group
,o.chan --master data
,o.chan --master data
,o.chansub
,o.chan_retail
,o.part
@ -389,11 +389,11 @@ GROUP BY
,o.mins_descr
,o.segm
,o.substance
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,SUM(o.units) units
,SUM(o.value_loc) value_loc
,SUM(o.value_usd) value_usd
@ -401,8 +401,8 @@ GROUP BY
,SUM(o.value_loc) FILTER (WHERE iter <> 'upload price')/SUM(o.units) FILTER (WHERE iter <> 'upload price') price_loc
,SUM(o.cost_loc) cost_loc
,SUM(o.cost_usd) cost_usd
,o.calc_status --0
,o.flag --0
,o.calc_status --0
,o.flag --0
,o.order_date --history date mix
,o.order_month
,o.order_season
@ -415,22 +415,22 @@ GROUP BY
FROM
rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool
,rlarp.itemmv i
,"CMS.CUSLG".itemm i
WHERE
i.item = o.part
GROUP BY
GROUP BY
o.fspr
,o.plnt ---master data
,o.plnt ---master data
,o.promo --history date mix
,o.terms
,o.bill_cust_descr --history cust mix
,o.ship_cust_descr --history cust mix
,o.dsm
,o.quota_rep_descr --master data
,o.quota_rep_descr --master data
,o.director
,o.billto_group --master data
,o.billto_group --master data
,o.shipto_group
,o.chan --master data
,o.chan --master data
,o.chansub
,o.chan_retail
,o.part
@ -444,13 +444,13 @@ GROUP BY
,o.mins_descr
,o.segm
,o.substance
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status --0
,o.flag --0
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.calc_status --0
,o.flag --0
,o.order_date --history date mix
,o.order_month
,o.order_season
@ -467,17 +467,17 @@ GROUP BY
,ins AS (
SELECT
o.fspr
,o.plnt ---master data
,o.plnt ---master data
,o.promo --history date mix
,o.terms
,o.bill_cust_descr --history cust mix
,o.ship_cust_descr --history cust mix
,o.dsm
,o.quota_rep_descr --master data
,o.quota_rep_descr --master data
,o.director
,o.billto_group --master data
,o.billto_group --master data
,o.shipto_group
,o.chan --master data
,o.chan --master data
,o.chansub
,o.chan_retail
,o.part
@ -490,11 +490,11 @@ SELECT
,o.mins_descr
,o.segm
,o.substance
,o.fs_line --master data
,o.fs_line --master data
,o.r_currency --history cust mix
,o.r_rate --master data
,o.c_currency --master data
,o.c_rate --master data
,o.r_rate --master data
,o.c_currency --master data
,o.c_rate --master data
--,o.units
,0::numeric units
,greatest(
@ -538,8 +538,8 @@ SELECT
-----------
,0::numeric cost_loc
,0::numeric cost_usd
,o.calc_status --0
,o.flag --0
,o.calc_status --0
,o.flag --0
,o.order_date --history date mix
,o.order_month
,o.order_season
@ -560,7 +560,7 @@ SELECT
FROM
collapse o
--need to join to itemm to get the product from osm_pool
--,fcp a
--,fcp a
,pivot a
--,log
WHERE

View File

@ -87,7 +87,7 @@ target AS (select $$replace_request$$::json def)
SELECT
LTRIM(RTRIM(A9)) AS COMP,
A30 AS DESCR,
SUBSTR(C.A249,242,2) CURR,
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,
@ -206,7 +206,7 @@ target AS (select $$replace_request$$::json def)
FROM
mmix
)
--select * from mmixp
--select * from mmixp
--month cross join mix
,mxm AS (
SELECT
@ -232,7 +232,7 @@ FROM
FROM
mmixp
ORDER BY
abs(seq - t.s) ASC
abs(seq - t.s) ASC
LIMIT 1
)
)
@ -290,7 +290,7 @@ SELECT
,(SELECT max(director) FROM repc WHERE rname = log.doc->'scenario'->>'quota_rep_descr') director
,COALESCE(CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END,b.bill_cust_descr) billto_group
,COALESCE(CASE sc.BVADR6 WHEN '' THEN sc.BVNAME ELSE sc.BVADR6 END,b.ship_cust_descr) shipto_group
,CASE SUBSTRING(bc.bvclas,2,3)
,CASE SUBSTRING(bc.bvclas,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
@ -302,7 +302,7 @@ SELECT
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END chan
,CASE SUBSTRING(bc.bvclas,2,3)
,CASE SUBSTRING(bc.bvclas,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(sc.bvclas,2,3)
@ -329,7 +329,7 @@ SELECT
,rx.rate r_rate --master data
,copr.curr c_currency --master data
,cx.rate c_rate --master data
,round(b.units ,2) units
,round(b.units ,2) units
,round(b.value_usd / COALESCE(rx.rate,1) ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
,round(b.value_usd ,2) value_usd --b.value is already denominated in usd
,round(COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units ,2) cost_loc
@ -354,7 +354,7 @@ SELECT
FROM
basemix b
CROSS JOIN log
LEFT OUTER JOIN rlarp.itemmv i ON
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = b.part
LEFT OUTER JOIN SEG ON
SEG.GLEC = i.glec
@ -364,7 +364,7 @@ FROM
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = rtrim(substring(b.ship_cust_descr,1,8))
LEFT OUTER JOIN REPC ON
REPC.RCODE = RTRIM(
REPC.RCODE = RTRIM(
--retail items go to currep, or if null go to 90005
CASE WHEN i.glec IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
@ -397,11 +397,11 @@ FROM
END
--minor group b52 goes to dedicated rep
ELSE
CASE WHEN i.MING = 'B52' THEN
CASE WHEN i.MING = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm

View File

@ -11,7 +11,7 @@ target AS (select 'replace_new_mold' new_mold)
SUM(value_usd) value_usd
FROM
rlarp.osm_pool o
LEFT OUTER JOIN rlarp.itemmv i ON
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part
WHERE
-----------------scenario----------------------------
@ -46,7 +46,7 @@ target AS (select 'replace_new_mold' new_mold)
CASE WHEN substring(b.part,9,12) = substring(i.item,9,12) THEN '1' ELSE 0 END fit
FROM
basemix b
LEFT OUTER JOIN rlarp.itemmv i ON
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = (SELECT new_mold FROM target WHERE new_mold <> '')
AND CASE WHEN b.sizc = i.sizc THEN '1' ELSE '0' END||
CASE WHEN i.aplnt = 'I' THEN '0' ELSE '1' END||
@ -55,7 +55,7 @@ target AS (select 'replace_new_mold' new_mold)
CASE WHEN b.colc = i.colc THEN '1' ELSE '0' END||
CASE WHEN substring(b.part,9,12) = substring(i.item,9,12) THEN '1' ELSE 0 END >= '111110'
)
--SELECT * FROM tag
--SELECT * FROM tag
,rsort AS (
SELECT
part,
@ -63,9 +63,9 @@ SELECT
item swap,
fit,
ROW_NUMBER() OVER (PARTITION BY tag.part ORDER BY fit DESC) ranked
FROM
FROM
tag
ORDER BY
ORDER BY
part
,fit desc
)
@ -74,4 +74,4 @@ SELECT
FROM
rsort
WHERE
ranked = 1
ranked = 1

View File

@ -44,17 +44,17 @@ target AS (SELECT $$swap_doc$$::jsonb swap)
,remove AS (
SELECT
fspr
,plnt ---master data
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,quota_rep_descr --master data
,director
,billto_group --master data
,billto_group --master data
,shipto_group
,chan --master data
,chan --master data
,chansub
,chan_retail
,part
@ -67,18 +67,18 @@ target AS (SELECT $$swap_doc$$::jsonb swap)
,mins_descr
,segm
,substance
,fs_line --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,r_rate --master data
,c_currency --master data
,c_rate --master data
,-units units
,-value_loc value_loc
,-value_usd value_usd
,-cost_loc cost_loc
,-cost_usd cost_usd
,calc_status --0
,flag --0
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
@ -160,13 +160,13 @@ target AS (SELECT $$swap_doc$$::jsonb swap)
remove o
INNER JOIN pl ON
pl.original = o.part
INNER JOIN rlarp.itemmv m ON
INNER JOIN "CMS.CUSLG".itemm m ON
m.item = pl.replace
LEFT OUTER JOIN rlarp.icstx c ON
c.part = pl.replace
AND c.plnt = m.dplt
LEFT OUTER JOIN rlarp.plpr r ON
yaplnt = m.dplt
r.plnt = m.dplt
LEFT OUTER JOIN rlarp.ffcret x ON
x.fcur = r.curr
AND x.tcur = 'US'