forecast_api/build/snap_cust_pool.sql

63 lines
1.8 KiB
MySQL
Raw Permalink Normal View History

2020-03-13 10:48:26 -04:00
BEGIN;
--forego setting other customer attributes as they are not stored in osm_pool anyways
2020-03-13 10:48:26 -04:00
UPDATE
RLARP.OSM_POOL S
2020-03-13 10:48:26 -04:00
SET
DSM = CR.QUOTA_REP
FROM
(
SELECT DISTINCT
VERSION,
COALESCE(SEGM,'') SEGM,
MING_DESCR,
BILL_CUST_DESCR,
SHIP_CUST_DESCR,
2020-06-16 15:53:02 -04:00
------------quota rep column--------------
CASE WHEN COALESCE(SUBSTRING(ming_descr,1,3),'') = 'B52' THEN 'PW' ELSE
2020-06-16 15:53:02 -04:00
--if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available
CASE WHEN COALESCE(segm,'') = 'Retail' AND COALESCE(cu.currep,'') <> '' THEN
cu.currep
--default logic
ELSE
CASE SUBSTR(bc.bvclas,2,3)
WHEN 'DIS' THEN
sc.bvsalm
ELSE
bc.bvsalm
END
2020-03-13 10:48:26 -04:00
END
END QUOTA_REP
FROM
RLARP.OSM_POOL S
LEFT OUTER JOIN LGDAT.CUST BC ON
BC.BVCUST = (regexp_match(S.bill_cust_descr,'(.*) - .*'))[1]
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = (regexp_match(S.ship_cust_descr,'(.*) - .*'))[1]
LEFT OUTER JOIN lgpgm.usrcust cu ON
cu.cucust = (regexp_match(S.bill_cust_descr,'(.*) - .*'))[1]
) CR
2020-03-13 10:48:26 -04:00
WHERE
CR.VERSION = S.VERSION
AND CR.SEGM = COALESCE(S.SEGM,'')
AND CR.MING_DESCR = S.MING_DESCR
AND CR.BILL_CUST_DESCR = S.BILL_CUST_DESCR
AND CR.SHIP_CUST_DESCR = S.SHIP_CUST_DESCR
AND COALESCE(S.DSM,'') <> CR.QUOTA_REP;
2020-03-13 10:48:26 -04:00
UPDATE
rlarp.osm_pool o
SET
quota_rep_descr = (regexp_match(r.repp,'.* - (.*)$'))[1]
,director = r.director
2020-03-13 10:48:26 -04:00
FROM
rlarp.repc r
2020-03-13 10:48:26 -04:00
WHERE
r.rcode = o.dsm
AND (
quota_rep_descr <> (regexp_match(r.repp,'.* - (.*)$'))[1]
OR o.director <> r.director
);
2020-06-07 22:43:50 -04:00
COMMIT;