change join basis and set some columns

This commit is contained in:
Trowbridge 2019-03-21 14:12:26 -04:00
parent 0018c14d40
commit e41f12ef7e

View File

@ -5,26 +5,26 @@ the volume must be expressed in terms of units, since that is what it will be sc
target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_group":"XRD16002 - 16 CM SP COEX POT 4 SLOT WHBK","version":"b20","iter":["copy","adj volume","adj price"]},"stamp":"2019-03-21 11:31:57","user":"Trowbridge, Paul","source":"adj","type":"new_part","months":{"Oct":{"amount":25080,"qty":264000},"Nov":{"amount":69540,"qty":732000},"Dec":{"amount":950,"qty":10000},"Jan":{"amount":21850,"qty":230000},"Feb":{"amount":19380,"qty":204000},"Mar":{"amount":20790.621864,"qty":218848.6512},"Apr":{"amount":53297.1118899,"qty":561022.23042}},"newpart":"XNT0T1G0X19B118PYAUE - 01.00 PW SS T1G TALL CX WH/BK"}'::json def) target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_group":"XRD16002 - 16 CM SP COEX POT 4 SLOT WHBK","version":"b20","iter":["copy","adj volume","adj price"]},"stamp":"2019-03-21 11:31:57","user":"Trowbridge, Paul","source":"adj","type":"new_part","months":{"Oct":{"amount":25080,"qty":264000},"Nov":{"amount":69540,"qty":732000},"Dec":{"amount":950,"qty":10000},"Jan":{"amount":21850,"qty":230000},"Feb":{"amount":19380,"qty":204000},"Mar":{"amount":20790.621864,"qty":218848.6512},"Apr":{"amount":53297.1118899,"qty":561022.23042}},"newpart":"XNT0T1G0X19B118PYAUE - 01.00 PW SS T1G TALL CX WH/BK"}'::json def)
--select * from newm --select * from newm
,GLD AS ( ,GLD AS (
SELECT SELECT
N1COMP COMP N1COMP COMP
,N1CCYY FSYR ,N1CCYY FSYR
,KPMAXP PERDS ,KPMAXP PERDS
,N1FSPP PERD ,N1FSPP PERD
,to_char(N1FSYP,'FM0000') FSPR ,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT ,N1SD01 SDAT
,N1ED01 EDAT ,N1ED01 EDAT
,to_char(N1ED01,'yymm') CAPR ,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 ,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 ,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 FROM
LGDAT.GLDATREF LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND KPCOMP = N1COMP AND
KPCCYY = N1CCYY KPCCYY = N1CCYY
WHERE WHERE
N1COMP = 93 N1COMP = 93
--AND DIGITS(N1FSYP) = '1901' --AND DIGITS(N1FSYP) = '1901'
) )
,mseq AS ( ,mseq AS (
SELECT * FROM SELECT * FROM
@ -45,63 +45,63 @@ target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_gro
) x(m,s,cal,yr) ) x(m,s,cal,yr)
) )
,SEG AS ( ,SEG AS (
SELECT SELECT
x.GLEC x.GLEC
,x.SEGM ,x.SEGM
FROM FROM
( (
VALUES VALUES
('1CU','Retail'), ('1CU','Retail'),
('1GR','Greenhouse'), ('1GR','Greenhouse'),
('1NU','Nursery'), ('1NU','Nursery'),
('1RE','Retail'), ('1RE','Retail'),
('2WI','Greenhouse'), ('2WI','Greenhouse'),
('3BM','Other'), ('3BM','Other'),
('3CO','Other'), ('3CO','Other'),
('3PE','Other'), ('3PE','Other'),
('3PP','Other'), ('3PP','Other'),
('4CO','Other'), ('4CO','Other'),
('4RA','Other'), ('4RA','Other'),
('9MI','Other'), ('9MI','Other'),
('9SA','Other'), ('9SA','Other'),
('9TO','Other') ('9TO','Other')
) X(GLEC, SEGM) ) X(GLEC, SEGM)
) )
,MG AS ( ,MG AS (
SELECT SELECT
X.F1, X.MING, X.GRP X.F1, X.MING, X.GRP
FROM FROM
( (
VALUES VALUES
('L','000','Branded'), ('L','000','Branded'),
('L','B10','Branded'), ('L','B10','Branded'),
('L','B11','Branded'), ('L','B11','Branded'),
('L','B52','Branded'), ('L','B52','Branded'),
('L','C10','Branded'), ('L','C10','Branded'),
('L','D12','Branded'), ('L','D12','Branded'),
('L','M11','Branded'), ('L','M11','Branded'),
('L','P12','Branded'), ('L','P12','Branded'),
('L','P13','Branded'), ('L','P13','Branded'),
('L','S10','Branded'), ('L','S10','Branded'),
('L','Y10','Branded'), ('L','Y10','Branded'),
('L','Y11','Branded'), ('L','Y11','Branded'),
('L','Y12','Branded'), ('L','Y12','Branded'),
('P','B10','Branded'), ('P','B10','Branded'),
('P','B11','Branded'), ('P','B11','Branded'),
('P','B52','Branded') ('P','B52','Branded')
) AS X (F1, MING, GRP) ) AS X (F1, MING, GRP)
) )
,REPC AS ( ,REPC AS (
SELECT SELECT
LTRIM(RTRIM(C.A9)) RCODE LTRIM(RTRIM(C.A9)) RCODE
,LTRIM(RTRIM(C.A9)) || ' - ' || C.A30 REPP ,LTRIM(RTRIM(C.A9)) || ' - ' || C.A30 REPP
,COALESCE(Q.DIR,'Other') DIRECTOR ,COALESCE(Q.DIR,'Other') DIRECTOR
FROM FROM
LGDAT.CODE C LGDAT.CODE C
LEFT OUTER JOIN RLARP.QRH Q ON LEFT OUTER JOIN RLARP.QRH Q ON
Q.QR = LTRIM(RTRIM(C.A9)) Q.QR = LTRIM(RTRIM(C.A9))
WHERE WHERE
C.A2 = 'MM' C.A2 = 'MM'
) )
--MOLD DESCRIPTIONS --MOLD DESCRIPTIONS
,MD AS ( ,MD AS (
@ -115,33 +115,33 @@ target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_gro
,MAX(M.AVDES1) DESCR ,MAX(M.AVDES1) DESCR
FROM FROM
LGDAT.STKMM M LGDAT.STKMM M
WHERE WHERE
LENGTH(M.AVPART) > 8 LENGTH(M.AVPART) > 8
AND SUBSTR(M.AVGLED,1,1) <= '2' AND SUBSTR(M.AVGLED,1,1) <= '2'
GROUP BY GROUP BY
SUBSTR(M.AVPART,1,8) SUBSTR(M.AVPART,1,8)
UNION ALL UNION ALL
SELECT SELECT
SUBSTR(P.AWPART,1,8) MOLD SUBSTR(P.AWPART,1,8) MOLD
,MAX(P.AWDES1) DESCR ,MAX(P.AWDES1) DESCR
FROM FROM
LGDAT.STKMP P LGDAT.STKMP P
WHERE WHERE
LENGTH(P.AWPART) > 8 LENGTH(P.AWPART) > 8
AND SUBSTR(P.AWGLED,1,1) <= '2' AND SUBSTR(P.AWGLED,1,1) <= '2'
GROUP BY GROUP BY
SUBSTR(P.AWPART,1,8) SUBSTR(P.AWPART,1,8)
) U ) U
GROUP BY GROUP BY
U.MOLD U.MOLD
) )
----------------------------------------------------sales major codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS ( ,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
) )
----------------------------------------------------sales minor codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS ( ,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
) )
,alldates AS ( ,alldates AS (
SELECT SELECT
@ -532,8 +532,8 @@ SELECT
,b.cost_loc ,b.cost_loc
,b.cost_usd ,b.cost_usd
,b.units ,b.units
,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix --,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix
,mxm.* --,mxm.*
FROM FROM
basemix b basemix b
CROSS JOIN mxm CROSS JOIN mxm
@ -585,7 +585,7 @@ SELECT
,b.dest_prov --master data ,b.dest_prov --master data
,b.dest_post --master data ,b.dest_post --master data
,(SELECT (regexp_match(def->>'newpart','(.*) - '::text))[1] FROM target) part --history part mix ,(SELECT (regexp_match(def->>'newpart','(.*) - '::text))[1] FROM target) part --history part mix
,COAELSCE(m.avglcd, p.awgldc) ord_gldc --master data ,COALESCE(m.avglcd, p.awgldc) ord_gldc --master data
,COALESCE(m.avmajg,p.awmajg) majg --master data ,COALESCE(m.avmajg,p.awmajg) majg --master data
,COALESCE(m.avming,p.awming) ming --master data ,COALESCE(m.avming,p.awming) ming --master data
,COALESCE(m.avmajs,p.awmajs) majs --master data ,COALESCE(m.avmajs,p.awmajs) majs --master data
@ -594,7 +594,7 @@ SELECT
,COALESCE(m.avgled,p.awgled) glec --master data ,COALESCE(m.avgled,p.awgled) glec --master data
,COALESCE(m.avharm,p.awharm) harm --master data ,COALESCE(m.avharm,p.awharm) harm --master data
,COALESCE(m.avclss,p.awclss) clss --master data ,COALESCE(m.avclss,p.awclss) clss --master data
,SUBSTR(COALESCE(m.avcpt#,m.awvpt#),1,1) brand --master data ,SUBSTR(COALESCE(m."avcpt#",p."awvpt#"),1,1) brand --master data
,COALESCE(m.avassc,p.awassc) assc --master data ,COALESCE(m.avassc,p.awassc) assc --master data
,b.fs_line --master data ,b.fs_line --master data
,b.r_currency --history cust mix ,b.r_currency --history cust mix
@ -605,31 +605,31 @@ SELECT
,b.ddqtsi ,b.ddqtsi
,b.fgqshp ,b.fgqshp
,b.diqtsh ,b.diqtsh
,(b.agg/sum(b.agg) OVER (partition by mxm.*))*mxm.momix*mxm.qty fb_qty ,b.fb_qty
,b.fb_cst_loc ,b.fb_cst_loc
,b.fb_cst_loc_cur ,b.fb_cst_loc_cur
,b.fb_cst_loc_fut ,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
,b.fb_val_loc_pl ,b.fb_val_loc_pl
,b.calc_status ,b.calc_status
,b.flag ,b.flag
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) orderdate ,b.orderdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag requestdate ,b.requestdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag shipdate ,b.shipdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) adj_orderdate ,b.adj_orderdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag adj_requestdate ,b.adj_requestdate
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate ,b.adj_shipdate
,'replace_version' "version" ,b."version"
,'replace_source'||' volume' iter ,b.iter
-----------------------ui columns-------------------------------- -----------------------ui columns--------------------------------
,'replace_iterdet' iterdet ,b.iterdet
,$${"replace_iterdef":""}$$::json iterdef ,b.iterdef
,od.ssyr order_season ,b.order_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month ,b.order_month
,sd.ssyr ship_season ,b.ship_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month ,b.ship_month
,rd.ssyr request_season ,b.request_season
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month ,b.request_month
,b.part_descr ,b.part_descr
,b.part_family ,b.part_family
,b.part_group ,b.part_group
@ -654,40 +654,40 @@ SELECT
,b.cost_usd ,b.cost_usd
,b.units ,b.units
FROM FROM
applyx x applyx b
LEFT OUTER JOIN LGDAT.STKMM M ON LEFT OUTER JOIN LGDAT.STKMM M ON
M.AVPART = x.PART M.AVPART = b.PART
LEFT OUTER JOIN LGDAT.STKMP P ON LEFT OUTER JOIN LGDAT.STKMP P ON
P.AWPART = x.PART P.AWPART = b.PART
LEFT OUTER JOIN lgdat.stka a ON LEFT OUTER JOIN lgdat.stka a ON
v6part = x.part v6part = b.part
AND v6plnt = COALESCE(m.avdplt,p.awdplt) AND v6plnt = COALESCE(m.avdplt,p.awdplt)
LEFT OUTER JOIN SEG ON LEFT OUTER JOIN SEG ON
SEG.GLEC = x.GLEC SEG.GLEC = COALESCE(m.avgled,p.awgled)
--AND SEG.SEGM <> 'Other' --AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN MG ON LEFT OUTER JOIN MG ON
MG.F1 = SUBSTR(x.PART,16,1) MG.F1 = SUBSTR(b.PART,16,1)
AND MG.MING = x.MING AND MG.MING = COALESCE(m.avming,p.awming)
LEFT OUTER JOIN RLARP.FAMILY F ON LEFT OUTER JOIN RLARP.FAMILY F ON
F.F3 = SUBSTR(x.PART,1,3) F.F3 = SUBSTR(b.PART,1,3)
LEFT OUTER JOIN MD ON LEFT OUTER JOIN MD ON
MD.MOLD = SUBSTR(x.PART,1,8) MD.MOLD = SUBSTR(b.PART,1,8)
LEFT OUTER JOIN RLARP.FFCHNL L ON LEFT OUTER JOIN RLARP.FFCHNL L ON
L.BILL = x.BILL_CLASS L.BILL = b.BILL_CLASS
AND L.SHIP = x.SHIP_CLASS AND L.SHIP = b.SHIP_CLASS
LEFT OUTER JOIN LGDAT.CUST BC ON LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = x.BILL_CUST BC.BVCUST = b.BILL_CUST
LEFT OUTER JOIN LGDAT.CUST SC ON LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = x.BILL_CUST SC.BVCUST = b.BILL_CUST
LEFT OUTER JOIN REPC ON LEFT OUTER JOIN REPC ON
REPC.RCODE = x.QUOTA_REP REPC.RCODE = b.QUOTA_REP
LEFT OUTER JOIN SI ON LEFT OUTER JOIN SI ON
SI.BSMJCD = x.MAJS SI.BSMJCD = COALESCE(m.avmajs,p.awmajs)
AND SI.BSMNCD = x.MINS AND SI.BSMNCD = COALESCE(m.avmins,p.awmins)
LEFT OUTER JOIN SJ ON LEFT OUTER JOIN SJ ON
SJ.BSMJCD = x.MAJS SJ.BSMJCD = COALESCE(m.avmajs,p.awmajs)
LEFT OUTER JOIN LGDAT.MAJG ON LEFT OUTER JOIN LGDAT.MAJG ON
BQGRP = x.MAJG BQGRP = COALESCE(m.avmajg,p.awmajg)
LEFT OUTER JOIN LGDAT.MMGP ON LEFT OUTER JOIN LGDAT.MMGP ON
BRMGRP = x.MING BRMGRP = COALESCE(m.avming,p.awming)
AND BRGRP = x.MAJG AND BRGRP = COALESCE(m.avmajg,p.awmajg)