UPDATE
    RLARP.OSMFS_DEV S
SET
    DSM = CR.QUOTA_REP
FROM
(
    SELECT DISTINCT
        VERSION,
        COALESCE(GLEC,'') GLEC,
        COALESCE(MING,'') 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.OSMFS_DEV S
        LEFT OUTER JOIN LGDAT.CUST ON
            BVCUST = BILL_CUST
        LEFT OUTER JOIN lgpgm.usrcust cu ON 
            cu.cucust = s.bill_cust
    --WHERE
    --    COALESCE(GLEC,'') IS NOT NULL
    ) CR
WHERE
    CR.VERSION = S.VERSION
    AND CR.GLEC = COALESCE(S.GLEC,'')
    AND CR.MING = COALESCE(S.MING,'')
    AND CR.BILL_CUST = S.BILL_CUST 
    AND CR.SHIP_CUST = S.SHIP_CUST
    AND COALESCE(S.DSM,'') <> CR.QUOTA_REP;

