functional but joining to itemm is very expensive

This commit is contained in:
Paul Trowbridge 2020-02-13 17:29:13 -05:00
parent acca526dca
commit 8d1cd1ade6

View File

@ -235,14 +235,14 @@ SELECT
,substring(jr.part_descr,1,8) part_group ,substring(jr.part_descr,1,8) part_group
,mxm.qty*mxm.momix*jr.mix units ,mxm.qty*mxm.momix*jr.mix units
,mxm.amount*mxm.momix*jr.mix value_loc ,mxm.amount*mxm.momix*jr.mix value_loc
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) order_date ,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) order_date
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month ,to_char(make_date(mxm.yr + 2021,mxm.cal,mxm.odom),'Mon') order_month
,od.ssyr order_season ,od.ssyr order_season
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag request_date ,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag request_date
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag,'Mon') request_month ,to_char(make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag,'Mon') request_month
,rd.ssyr request_season ,rd.ssyr request_season
,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag ship_date ,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag ship_date
,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month ,to_char(make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag,'Mon') ship_month
,sd.ssyr ship_season ,sd.ssyr ship_season
,jr.mix ,jr.mix
FROM FROM
@ -251,11 +251,11 @@ SELECT
JOIN LATERAL json_to_record(ae.e) as jr(part_descr text, bill_cust_descr text, ship_cust_descr text, mix numeric) 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 CROSS JOIN mxm
LEFT OUTER JOIN gld od ON LEFT OUTER JOIN gld od ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat make_date(mxm.yr + 2021,mxm.cal,mxm.odom) BETWEEN od.sdat AND od.edat
LEFT OUTER JOIN gld rd ON LEFT OUTER JOIN gld rd ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag BETWEEN rd.sdat AND rd.edat make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag BETWEEN rd.sdat AND rd.edat
LEFT OUTER JOIN gld sd ON LEFT OUTER JOIN gld sd ON
make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
) )
--SELECT * FROM basemix --SELECT * FROM basemix
,log AS ( ,log AS (
@ -272,8 +272,8 @@ SELECT
,(SELECT rcode FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') dsm ,(SELECT rcode FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') dsm
,log.doc->'scenario'->>'quota_rep_descr' quota_rep_descr ,log.doc->'scenario'->>'quota_rep_descr' quota_rep_descr
,(SELECT director FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') director ,(SELECT director FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') director
,coalesce(cg.cgrp,bc.bvname) billto_group ,CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END billto_group
,coalesce(sg.cgrp,sc.bvname) shipto_group ,CASE sc.BVADR6 WHEN '' THEN sc.BVNAME ELSE bc.BVADR6 END 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 --if the bill to class is ditsributor, then it's either warehouse or drop
WHEN 'DIS' THEN WHEN 'DIS' THEN
@ -301,24 +301,24 @@ SELECT
,b.part ,b.part
,b.part_descr ,b.part_descr
,b.part_group ,b.part_group
,i.brnd branding ,i.branding
,i.majg||' - '||i.majgd majg_descr ,i.majg||' - '||i.majgd majg_descr
,i.ming||' - '||i.mingd ming_descr ,i.ming||' - '||i.mingd ming_descr
,i.majs||' - '||i.majsd majs_descr ,i.majs||' - '||i.majsd majs_descr
,i.mins||' - '||i.minsd mins_descr ,i.mins||' - '||i.minsd mins_descr
,seg.segm ,seg.segm
,b.fs_line --master data ,'41010' fs_line --master data
,bc.bvcurr r_currency --history cust mix ,bc.bvcurr r_currency --history cust mix
,rx.rate r_rate --master data ,rx.rate r_rate --master data
,copr.curr c_currency --master data ,copr.curr c_currency --master data
,cx.rate c_rate --master data ,cx.rate c_rate --master data
,b.units ,b.units
,b.value_loc ,b.value_loc
,b.value_usd ,b.value_loc * rx.rate value_usd
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units cost_loc ,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units cost_loc
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate cost_loc ,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate cost_loc
,b.calc_status ,'CLOSED' calc_status
,b.flag ,'SHIPMENT' flag
,b.order_date ,b.order_date
,b.order_month ,b.order_month
,b.order_season ,b.order_season
@ -337,24 +337,24 @@ FROM
basemix b basemix b
CROSS JOIN log CROSS JOIN log
LEFT OUTER JOIN rlarp.itemm i ON LEFT OUTER JOIN rlarp.itemm i ON
i.itemm = b.part i.item = b.part
LEFT OUTER JOIN SEG ON LEFT OUTER JOIN SEG ON
SEG.GLEC = i.glec SEG.GLEC = i.glec
--AND SEG.SEGM <> 'Other' --AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN LGDAT.CUST BC ON LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = b.BILL_CUST BC.BVCUST = rtrim(substring(b.bill_cust_descr,1,8))
LEFT OUTER JOIN LGDAT.CUST SC ON LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = b.SHIP_CUST SC.BVCUST = rtrim(substring(b.ship_cust_descr,1,8))
LEFT OUTER JOIN REPC ON LEFT OUTER JOIN REPC ON
REPC.RCODE = RTRIM( REPC.RCODE = RTRIM(
--retail items go to currep, or if null go to 90005 --retail items go to currep, or if null go to 90005
CASE WHEN S.GLEC IN ('1RE','1CU') THEN CASE WHEN i.glec IN ('1RE','1CU') THEN
CASE WHEN BVCTRY = 'CAN' THEN CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman --Rachel Bowman
'50300' '50300'
ELSE ELSE
--select customers go to select reps --select customers go to select reps
CASE ACCOUNT CASE CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END
------Alecia Latini------------------------------- ------Alecia Latini-------------------------------
WHEN 'DO IT BEST' THEN '90006' WHEN 'DO IT BEST' THEN '90006'
WHEN 'ACE HARDWARE' THEN '90006' WHEN 'ACE HARDWARE' THEN '90006'
@ -379,14 +379,14 @@ FROM
END END
--minor group b52 goes to dedicated rep --minor group b52 goes to dedicated rep
ELSE ELSE
CASE WHEN MING = 'B52' THEN CASE WHEN i.MING = 'B52' THEN
'PW' 'PW'
--gdir, ndir go to bill-to rep --gdir, ndir go to bill-to rep
ELSE ELSE
CASE WHEN BILL_CLASS IN ('GDIR','NDIR') THEN CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
BILL_REP bc.bvsalm
ELSE ELSE
SHIP_REP sc.bvsalm
END END
END END
END END
@ -401,7 +401,7 @@ FROM
ir.y0part = b.part ir.y0part = b.part
AND ir.y0plnt = i.dplt AND ir.y0plnt = i.dplt
LEFT OUTER JOIN lgdat.plnt ON LEFT OUTER JOIN lgdat.plnt ON
yaplnt = a.v6plnt yaplnt = i.dplt
LEFT OUTER JOIN copr ON LEFT OUTER JOIN copr ON
copr.comp = yacomp::text copr.comp = yacomp::text
LEFT OUTER JOIN rlarp.ffcret cx ON LEFT OUTER JOIN rlarp.ffcret cx ON