forecast_api/route_sql/new_basket.sql

507 lines
16 KiB
MySQL
Raw Normal View History

2020-02-13 16:41:39 -05:00
WITH
2019-03-22 04:51:44 -04:00
/*
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 (
2020-02-13 16:41:39 -05:00
SELECT
2019-03-22 04:51:44 -04:00
N1COMP COMP
,N1CCYY FSYR
,KPMAXP PERDS
,N1FSPP PERD
,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT
,N1ED01 EDAT
,daterange(n1sd01, n1ed01) drange
2019-03-22 04:51:44 -04:00
,to_char(N1ED01,'yymm') CAPR
2020-02-13 16:41:39 -05:00
,N1ED01 - N1SD01 +1 NDAYS
2019-03-22 04:51:44 -04:00
,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') || ' - '|| TO_CHAR(n1ed01,'Mon') SORTMO
2020-02-13 16:41:39 -05:00
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
2019-03-22 04:51:44 -04:00
KPCCYY = N1CCYY
WHERE
N1COMP = 93
--AND DIGITS(N1FSYP) = '1901'
)
,mseq AS (
2020-02-13 16:41:39 -05:00
SELECT * FROM
2019-03-22 04:51:44 -04:00
(
VALUES
2020-02-13 16:41:39 -05:00
('01 - Jun',1,6,-1)
,('02 - Jul',2,7,-1)
,('03 - Aug',3,8,-1)
,('04 - Sep',4,9,-1)
,('05 - Oct',5,10,-1)
,('06 - Nov',6,11,-1)
,('07 - Dec',7,12,-1)
,('08 - Jan',8,1,0)
,('09 - Feb',9,2,0)
,('10 - Mar',10,3,0)
,('11 - Apr',11,4,0)
,('12 - May',12,5,0)
2019-03-22 04:51:44 -04:00
) 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)
)
,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'
)
,copr AS (
2020-02-13 16:41:39 -05:00
SELECT
LTRIM(RTRIM(A9)) AS COMP,
A30 AS DESCR,
2019-03-22 04:51:44 -04:00
SUBSTR(C.A249,242,2) CURR,
2020-02-13 16:41:39 -05:00
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
2019-03-22 04:51:44 -04:00
A2 = 'AA' OR A2 IS NULL
)
,alldates AS (
2020-02-13 16:41:39 -05:00
SELECT
2019-03-22 04:51:44 -04:00
promo
,terms
,order_month
,mseq.s seq
2020-02-13 16:41:39 -05:00
,order_date
,request_date
,ship_date
2019-03-22 04:51:44 -04:00
,sum(value_usd) value_usd
FROM
2020-02-13 16:41:39 -05:00
rlarp.osm_pool
2019-03-22 04:51:44 -04:00
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
2020-02-13 16:41:39 -05:00
AND order_date <= ship_date
2019-03-22 04:51:44 -04:00
GROUP BY
promo
,terms
,order_month
,mseq.s
2020-02-13 16:41:39 -05:00
,order_date
,request_date
,ship_date
2019-03-22 04:51:44 -04:00
HAVING
sum(value_usd) <> 0
)
--select * from alldates
,dom AS (
SELECT
2020-02-13 16:41:39 -05:00
extract(day FROM order_date) DOM
2019-03-22 04:51:44 -04:00
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
2020-02-13 16:41:39 -05:00
extract(day FROM order_date)
2019-03-22 04:51:44 -04:00
)
2020-02-13 16:41:39 -05:00
--select * from dom
2019-03-22 04:51:44 -04:00
---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)---------------
,mmix AS (
SELECT
2020-02-13 16:41:39 -05:00
to_char(order_date,'Mon') _month
2019-03-22 04:51:44 -04:00
,seq
,promo
2020-02-13 16:41:39 -05:00
,sum(extract(day from order_date)*value_usd) dom_wa
--,request_date-order_date rlag
,sum((request_date-order_date)*(value_usd)) rlag_wa
--,ship_date - request_date slag
,sum((ship_date - request_date)*(value_usd)) slag_wa
2019-03-22 04:51:44 -04:00
,sum(value_usd) value_usd
FROM
alldates
GROUP BY
2020-02-13 16:41:39 -05:00
to_char(order_date,'Mon')
2019-03-22 04:51:44 -04:00
,seq
,promo
2020-02-13 16:41:39 -05:00
--,extract(day from order_date)
--,request_date-order_date
--,ship_date - request_date
2019-03-22 04:51:44 -04:00
)
2020-02-13 16:41:39 -05:00
--select * from mmix
2019-03-22 04:51:44 -04:00
,targm AS (
SELECT
je.key as month
,r.amount
,r.qty
,s
,cal
,yr
2020-02-13 16:41:39 -05:00
FROM
2019-03-22 04:51:44 -04:00
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
)
2020-02-13 16:41:39 -05:00
--select * from targm
2019-03-22 04:51:44 -04:00
,mmixp AS (
SELECT
_month
,seq
,promo
2019-04-16 09:26:39 -04:00
,greatest(least(round((dom_wa/value_usd)::numeric,0)::int,28),1) odom
2019-03-22 04:51:44 -04:00
,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
)
2020-02-13 16:41:39 -05:00
--select * from mmixp
2019-03-22 04:51:44 -04:00
--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 = (
2020-02-13 16:41:39 -05:00
SELECT
2019-03-22 04:51:44 -04:00
_month
2020-02-13 16:41:39 -05:00
FROM
mmixp
ORDER BY
2019-03-22 04:51:44 -04:00
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
2020-02-13 16:41:39 -05:00
sd.fspr
,mxm.promo
,jr.bill_cust_descr
,jr.ship_cust_descr
2019-03-22 04:51:44 -04:00
,(regexp_match(jr.part_descr,'(.*?)(?= - |$)'))[1] part
2020-02-13 16:41:39 -05:00
,jr.part_descr
,substring(jr.part_descr,1,8) part_group
,mxm.qty*mxm.momix*jr.mix units
,mxm.amount*mxm.momix*jr.mix value_usd --assume that target dollars are USD
,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) order_date
,od.sortmo order_month
2020-02-13 16:41:39 -05:00
,od.ssyr order_season
,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag request_date
,rd.sortmo request_month
2020-02-13 16:41:39 -05:00
,rd.ssyr request_season
,make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag ship_date
,sd.sortmo ship_month
2020-02-13 16:41:39 -05:00
,sd.ssyr ship_season
2019-03-22 04:51:44 -04:00
,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
od.drange @> make_date(mxm.yr + 2021,mxm.cal,mxm.odom)
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN gld rd ON
2020-02-14 00:15:11 -05:00
rd.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag)
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN gld sd ON
2020-02-14 00:15:11 -05:00
sd.drange @> (make_date(mxm.yr + 2021,mxm.cal,mxm.odom) + rlag + slag)
2019-03-22 04:51:44 -04:00
)
--SELECT * FROM basemix
2020-02-13 16:41:39 -05:00
,log AS (
INSERT INTO rlarp.osm_log(doc) SELECT $$replace_iterdef$$::jsonb doc RETURNING *
)
2019-03-22 04:51:44 -04:00
,final AS (
SELECT
2020-02-13 16:41:39 -05:00
b.fspr
,i.dplt plnt
,b.promo
,bc.bvterm terms
,b.bill_cust_descr
,b.ship_cust_descr
,(SELECT rcode FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') dsm
,log.doc->'scenario'->>'quota_rep_descr' quota_rep_descr
,(SELECT director FROM repc WHERE repp = log.doc->'scenario'->>'quota_rep_descr') director
,CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END billto_group
,CASE sc.BVADR6 WHEN '' THEN sc.BVNAME ELSE bc.BVADR6 END shipto_group
2020-02-13 16:41:39 -05:00
,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
CASE SUBSTRING(sc.bvclas,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
2019-03-22 04:51:44 -04:00
END
2020-02-13 16:41:39 -05:00
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END
--everything else does not involve a distributor and is considered direct
ELSE 'DIR'
END chan
,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)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(sc.bvclas,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
2019-03-22 04:51:44 -04:00
END
2020-02-13 16:41:39 -05:00
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(sc.bvclas,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END chansub
,null::text chan_retail
,b.part
,b.part_descr
,b.part_group
,i.branding
2020-02-13 16:41:39 -05:00
,i.majg||' - '||i.majgd majg_descr
,i.ming||' - '||i.mingd ming_descr
,i.majs||' - '||i.majsd majs_descr
,i.mins||' - '||i.minsd mins_descr
,seg.segm
,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
,'41010' fs_line --master data
2019-03-22 04:51:44 -04:00
,bc.bvcurr r_currency --history cust mix
2020-02-13 16:41:39 -05:00
,rx.rate r_rate --master data
,copr.curr c_currency --master data
,cx.rate c_rate --master data
,b.units
,b.value_usd / rx.rate --b.value is denominated in USD, need to apply currency to get to local
,b.value_usd value_usd --b.value is already denominated in usd
2020-02-13 16:41:39 -05:00
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units cost_loc
,COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate cost_usd
,'CLOSED' calc_status
,'SHIPMENT' flag
2020-02-13 16:41:39 -05:00
,b.order_date
2019-03-22 04:51:44 -04:00
,b.order_month
2020-02-13 16:41:39 -05:00
,b.order_season
,b.request_date
2019-03-22 04:51:44 -04:00
,b.request_month
2020-02-13 16:41:39 -05:00
,b.request_season
,b.ship_date
,b.ship_month
,b.ship_season
,'replace_version' "version"
,'replace_source'||' volume' iter
,log.id
2020-02-18 14:47:46 -05:00
,COALESCE(log.doc->>'tag','') "tag"
2020-02-18 15:35:56 -05:00
,log.doc->>'message' "comment"
2020-02-18 17:10:49 -05:00
,log.doc->>'module' module
2019-03-22 04:51:44 -04:00
FROM
basemix b
2020-02-13 16:41:39 -05:00
CROSS JOIN log
LEFT OUTER JOIN rlarp.itemmv i ON
i.item = b.part
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN SEG ON
2020-02-13 16:41:39 -05:00
SEG.GLEC = i.glec
2019-03-22 04:51:44 -04:00
--AND SEG.SEGM <> 'Other'
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = rtrim(substring(b.bill_cust_descr,1,8))
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = rtrim(substring(b.ship_cust_descr,1,8))
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN REPC ON
2020-02-13 16:41:39 -05:00
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
2020-02-13 16:41:39 -05:00
--Rachel Bowman
'50300'
ELSE
--select customers go to select reps
CASE CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END
2020-02-13 16:41:39 -05:00
------Alecia Latini-------------------------------
WHEN 'DO IT BEST' THEN '90006'
WHEN 'ACE HARDWARE' THEN '90006'
WHEN 'ALDI' THEN '90006'
WHEN 'AMAZON.COM' THEN '90006'
WHEN 'GARDEN RIDGE CORP' THEN '90006' --AKA "At Home"
WHEN 'TRUE VALUE' THEN '90006'
WHEN 'WAYFAIR' THEN '90006'
WHEN 'GRIFFIN' THEN '90006'
WHEN 'WAL-MART' THEN '90006'
------Tony Landino--------------------------------
WHEN 'THE HOME DEPOT' THEN '50802'
WHEN 'FRED MEYER' THEN '50802'
WHEN 'MENARDS' THEN '50802'
WHEN 'KROGER' THEN '50802'
WHEN 'OCEAN STATE JOBBERS' THEN '50802'
WHEN 'AURORA WHOLESALE' THEN '50802'
WHEN 'LEON KORRAL' THEN '50802'
--all other retail goes to Doran Marable-----------
ELSE '50200'
END
END
--minor group b52 goes to dedicated rep
ELSE
CASE WHEN i.MING = 'B52' THEN
2020-02-13 16:41:39 -05:00
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
2020-02-13 16:41:39 -05:00
ELSE
sc.bvsalm
2020-02-13 16:41:39 -05:00
END
END
END
)
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = b.part
2020-02-13 16:41:39 -05:00
AND im.cgplnt = i.dplt
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN lgdat.icstp ip ON
ip.chpart = b.part
2020-02-13 16:41:39 -05:00
AND ip.chplnt = i.dplt
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN lgdat.icstr ir ON
ir.y0part = b.part
2020-02-13 16:41:39 -05:00
AND ir.y0plnt = i.dplt
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN lgdat.plnt ON
yaplnt = i.dplt
2019-03-22 04:51:44 -04:00
LEFT OUTER JOIN copr ON
copr.comp = yacomp::text
LEFT OUTER JOIN rlarp.ffcret cx ON
2020-02-13 16:41:39 -05:00
cx.perd = '2008'
2019-03-22 04:51:44 -04:00
AND cx.rtyp = 'MA'
AND cx.fcur = copr.curr
AND cx.tcur = 'US'
LEFT OUTER JOIN rlarp.ffcret rx ON
2020-02-13 16:41:39 -05:00
rx.perd = '2008'
2019-03-22 04:51:44 -04:00
AND rx.rtyp = 'MA'
AND rx.fcur = bc.bvcurr
AND rx.tcur = 'US'
)
, ins AS (
2020-02-13 16:41:39 -05:00
INSERT INTO rlarp.osm_pool SELECT * FROM final RETURNING *
2019-03-22 04:51:44 -04:00
)
,insagg AS (
2020-02-13 16:41:39 -05:00
SELECT
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
2020-02-13 16:41:39 -05:00
,chan
,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
,logid
2020-02-18 14:47:46 -05:00
,tag
2020-02-18 15:35:56 -05:00
,comment
2020-02-13 16:41:39 -05:00
--------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
ins
GROUP BY
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
2020-02-13 16:41:39 -05:00
,chan
,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
,logid
2020-02-18 14:47:46 -05:00
,tag
2020-02-18 15:35:56 -05:00
,comment
2019-03-22 04:51:44 -04:00
)
2020-02-13 16:41:39 -05:00
SELECT json_agg(row_to_json(insagg)) x from insagg