----------------------------SET BILL-TO REP------------------------------------ UPDATE rlarp.osmf s SET bill_rep = c.bvsalm ,bill_class = c.bvclas ,bill_terr = c.bvterr ,bill_ctry = c.bvctry ,bill_prov = c.bvprcd ,bill_post = c.bvpost ,remit_to = c.bvcomp ,account = CASE bvadr6 WHEN '' THEN bvname ELSE bvadr6 END FROM lgdat.cust c WHERE c.bvcust = s.bill_cust AND ( COALESCE(s.bill_rep,'') <> c.bvsalm OR COALESCE(s.bill_class,'') <> c.bvclas OR COALESCE(s.bill_terr,'') <> c.bvterr OR COALESCE(bill_ctry,'') <> c.bvctry OR COALESCE(bill_prov,'') <> c.bvprcd OR COALESCE(bill_post,'') <> c.bvpost OR COALESCE(remit_to,'') <> c.bvcomp::text ); ----------------------------SET SHIP-TO REP------------------------------------ UPDATE rlarp.osmf s SET ship_rep = c.bvsalm ,ship_class = c.bvclas ,ship_terr = c.bvterr ,dest_ctry = c.bvctry ,dest_prov = c.bvprcd ,dest_post = c.bvpost FROM lgdat.cust c WHERE c.bvcust = s.ship_cust AND ( COALESCE(s.ship_rep,'') <> c.bvsalm OR COALESCE(s.ship_class,'') <> c.bvclas OR COALESCE(s.ship_terr,'') <> c.bvterr OR COALESCE(dest_ctry,'') <> c.bvctry OR COALESCE(dest_prov,'') <> c.bvprcd OR COALESCE(dest_post,'') <> c.bvpost ); ----------------------------SET BILLTO GROUP------------------------------------ UPDATE rlarp.osmf o SET ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END FROM lgdat.cust c WHERE c.bvcust = o.bill_cust AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END; ----------------------------SET SHIPTO GROUP------------------------------------ UPDATE rlarp.osmf o SET SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END FROM lgdat.cust c WHERE c.bvcust = o.ship_cust AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,''); ---------------------------SET CHANNEL----------------------------------------- UPDATE rlarp.osmf SET CHAN = CASE SUBSTRING(BILL_CLASS,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(SHIP_CLASS,2,3) WHEN 'DIS' THEN 'WHS' ELSE 'DRP' END --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, CHANSUB = CASE SUBSTRING(BILL_CLASS,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(SHIP_CLASS,2,3) WHEN 'DIS' THEN 'WHS' ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END END WHEN 'MAS' THEN 'RMN' WHEN 'NAT' THEN 'RMN' ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END END WHERE COALESCE(CHAN,'') <> CASE SUBSTRING(BILL_CLASS,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(SHIP_CLASS,2,3) WHEN 'DIS' THEN 'WHS' ELSE 'DRP' END --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 OR COALESCE(CHANSUB,'') <> CASE SUBSTRING(BILL_CLASS,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(SHIP_CLASS,2,3) WHEN 'DIS' THEN 'WHS' ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END END WHEN 'MAS' THEN 'RMN' WHEN 'NAT' THEN 'RMN' ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END END; ---------------------------SET QUOTA REP--------------------------------------- UPDATE rlarp.osmf s SET dsm = cr.quota_rep FROM ( SELECT DISTINCT version, COALESCE(glec,'') glec, ming, bill_cust, ship_cust, ------------quota rep column-------------- CASE WHEN COALESCE(ming,'') = 'B52' THEN 'PW' ELSE --if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available CASE WHEN COALESCE(glec,'') = '1RE' AND COALESCE(cu.currep,'') <> '' THEN cu.currep --default logic ELSE CASE SUBSTR(bill_class,2,3) WHEN 'DIS' THEN ship_rep ELSE bill_rep END END END QUOTA_REP FROM rlarp.osmf s LEFT OUTER JOIN lgdat.cust ON bvcust = bill_cust LEFT OUTER JOIN lgpgm.usrcust cu ON cu.cucust = s.bill_cust WHERE version = 'ACTUALS' ) CR WHERE cr.version = s.version AND cr.glec = COALESCE(s.glec,'') AND cr.ming = s.ming AND cr.bill_cust = s.bill_cust AND cr.ship_cust = s.ship_cust AND COALESCE(s.dsm,'') <> cr.quota_rep; -------------------------------------------------------------------------- ---------------------------Add Ownership columns-------------------------- -------------------------------------------------------------------------- UPDATE rlarp.osmf s SET dsm = cr.default_rep -- keyaccount_rep = cr.keyaccount_rep, -- retail_rep = cr.retail_rep, -- inside_rep = cr.inside_rep FROM ( SELECT DISTINCT s.bill_cust, bc.cclass, s.ship_cust, CASE substring(s.ming, 1, 3) WHEN 'A27' THEN '70100' ELSE COALESCE(sc.default_rep, bc.default_rep) END AS default_rep, CASE WHEN COALESCE(sc.keyaccount_rep, '') <> '' AND bc.cclass = 'GDIS' THEN sc.keyaccount_rep ELSE bc.keyaccount_rep END AS keyaccount_rep, s.glec, CASE WHEN COALESCE(s.glec, '') = '1RE' AND COALESCE(bc.retail_rep, '') <> '' THEN bc.retail_rep ELSE NULL END AS retail_rep, CASE SUBSTRING(bc.cclass, 2, 3) WHEN 'DIS' THEN sc.inside_rep ELSE bc.inside_rep END AS inside_rep FROM rlarp.osmf s LEFT OUTER JOIN rlarp.cust bc ON bc.code = s.bill_cust LEFT OUTER JOIN rlarp.cust sc ON sc.code = s.ship_cust WHERE glec IS NOT NULL ) cr WHERE cr.glec = s.glec AND cr.bill_cust = s.bill_cust AND COALESCE(cr.ship_cust, '') = COALESCE(s.ship_cust, ''); -- UPDATE -- rlarp.osmf o -- SET -- director = r.director -- FROM -- rlarp.repc r -- WHERE -- r.rcode = o.dsm;