change join basis and set some columns
This commit is contained in:
parent
0018c14d40
commit
e41f12ef7e
@ -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)
|
||||
--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'
|
||||
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
|
||||
@ -45,63 +45,63 @@ target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_gro
|
||||
) 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)
|
||||
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)
|
||||
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'
|
||||
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 (
|
||||
@ -115,33 +115,33 @@ target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_gro
|
||||
,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)
|
||||
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)
|
||||
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,'') = ''
|
||||
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
|
||||
)
|
||||
----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
|
||||
,SI AS (
|
||||
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
|
||||
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
|
||||
)
|
||||
,alldates AS (
|
||||
SELECT
|
||||
@ -532,8 +532,8 @@ SELECT
|
||||
,b.cost_loc
|
||||
,b.cost_usd
|
||||
,b.units
|
||||
,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix
|
||||
,mxm.*
|
||||
--,(b.agg/sum(b.agg) OVER (partition by mxm.*)) bmix
|
||||
--,mxm.*
|
||||
FROM
|
||||
basemix b
|
||||
CROSS JOIN mxm
|
||||
@ -585,7 +585,7 @@ SELECT
|
||||
,b.dest_prov --master data
|
||||
,b.dest_post --master data
|
||||
,(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.avming,p.awming) ming --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.avharm,p.awharm) harm --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
|
||||
,b.fs_line --master data
|
||||
,b.r_currency --history cust mix
|
||||
@ -605,31 +605,31 @@ SELECT
|
||||
,b.ddqtsi
|
||||
,b.fgqshp
|
||||
,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_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
|
||||
,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
|
||||
,b.orderdate
|
||||
,b.requestdate
|
||||
,b.shipdate
|
||||
,b.adj_orderdate
|
||||
,b.adj_requestdate
|
||||
,b.adj_shipdate
|
||||
,b."version"
|
||||
,b.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.iterdet
|
||||
,b.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
|
||||
@ -654,40 +654,40 @@ SELECT
|
||||
,b.cost_usd
|
||||
,b.units
|
||||
FROM
|
||||
applyx x
|
||||
applyx b
|
||||
LEFT OUTER JOIN LGDAT.STKMM M ON
|
||||
M.AVPART = x.PART
|
||||
M.AVPART = b.PART
|
||||
LEFT OUTER JOIN LGDAT.STKMP P ON
|
||||
P.AWPART = x.PART
|
||||
P.AWPART = b.PART
|
||||
LEFT OUTER JOIN lgdat.stka a ON
|
||||
v6part = x.part
|
||||
v6part = b.part
|
||||
AND v6plnt = COALESCE(m.avdplt,p.awdplt)
|
||||
LEFT OUTER JOIN SEG ON
|
||||
SEG.GLEC = x.GLEC
|
||||
SEG.GLEC = COALESCE(m.avgled,p.awgled)
|
||||
--AND SEG.SEGM <> 'Other'
|
||||
LEFT OUTER JOIN MG ON
|
||||
MG.F1 = SUBSTR(x.PART,16,1)
|
||||
AND MG.MING = x.MING
|
||||
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(x.PART,1,3)
|
||||
F.F3 = SUBSTR(b.PART,1,3)
|
||||
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
|
||||
L.BILL = x.BILL_CLASS
|
||||
AND L.SHIP = x.SHIP_CLASS
|
||||
L.BILL = b.BILL_CLASS
|
||||
AND L.SHIP = b.SHIP_CLASS
|
||||
LEFT OUTER JOIN LGDAT.CUST BC ON
|
||||
BC.BVCUST = x.BILL_CUST
|
||||
BC.BVCUST = b.BILL_CUST
|
||||
LEFT OUTER JOIN LGDAT.CUST SC ON
|
||||
SC.BVCUST = x.BILL_CUST
|
||||
SC.BVCUST = b.BILL_CUST
|
||||
LEFT OUTER JOIN REPC ON
|
||||
REPC.RCODE = x.QUOTA_REP
|
||||
REPC.RCODE = b.QUOTA_REP
|
||||
LEFT OUTER JOIN SI ON
|
||||
SI.BSMJCD = x.MAJS
|
||||
AND SI.BSMNCD = x.MINS
|
||||
SI.BSMJCD = COALESCE(m.avmajs,p.awmajs)
|
||||
AND SI.BSMNCD = COALESCE(m.avmins,p.awmins)
|
||||
LEFT OUTER JOIN SJ ON
|
||||
SJ.BSMJCD = x.MAJS
|
||||
SJ.BSMJCD = COALESCE(m.avmajs,p.awmajs)
|
||||
LEFT OUTER JOIN LGDAT.MAJG ON
|
||||
BQGRP = x.MAJG
|
||||
BQGRP = COALESCE(m.avmajg,p.awmajg)
|
||||
LEFT OUTER JOIN LGDAT.MMGP ON
|
||||
BRMGRP = x.MING
|
||||
AND BRGRP = x.MAJG
|
||||
BRMGRP = COALESCE(m.avming,p.awming)
|
||||
AND BRGRP = COALESCE(m.avmajg,p.awmajg)
|
Loading…
Reference in New Issue
Block a user