CREATE OR ALTER VIEW pricing.cust AS
SELECT
    bvcust code,
    bvbill default_billto,
    bvname descr,
    CASE WHEN bvadr6 = '' THEN bvname ELSE bvadr6 END dba,
    bvctry country,
    bvprcd province,
    bvcity city,
    bvcomp remit_to,
    bvclas cclass,
    bvstat status,
    bvtype ctype,
    RTRIM(bvschl) plevel,
    s.bk7des3 folder,
--    pl.lists lists,
    dr.repp default_rep,
    rr.repp retail_rep,
    gr.repp inside_rep,
    nr.repp keyaccount_rep,
    u.mfresp tier
FROM
    cmsinterfacein.lgdat.cust c
    LEFT OUTER JOIN cmsinterfacein.lgpgm.usrcust ON
        cucust = bvcust
    LEFT OUTER JOIN pricing.repc dr ON
        dr.rcode = bvsalm
    LEFT OUTER JOIN pricing.repc rr ON
        rr.rcode = currep
    LEFT OUTER JOIN pricing.repc gr ON
        gr.rcode = cugrep
    LEFT OUTER JOIN pricing.repc nr ON
        nr.rcode = cunrep
    LEFT OUTER JOIN pricing.sach s ON
        s.bk7code = c.bvschl
    LEFT OUTER JOIN pricing.ffterr t ON
        t.prov = bvprcd
        AND t.ctry = bvctry
    LEFT OUTER JOIN pricing.ffcret x ON
        x.fcur = c.bvcurr
        AND x.tcur = 'US'
        AND x.perd = (SELECT fspr FROM pricing.gld WHERE GETDATE()  BETWEEN sdat AND edat)
        AND x.rtyp = 'MA'
    LEFT OUTER JOIN cmsinterfacein.lgdat.usrc u ON 
        u.mfsrce = 'MN' 
        AND u.mfent# = 12
        AND u.mfkey2 = c.bvcust
--	LEFT OUTER JOIN (
--	    SELECT jbplvl, STRING_AGG(JBPLCD, ', ') AS lists
--	    FROM CMSinterfacein.[CMS.CUSLG].iprcbhc
--	    GROUP BY jbplvl
--	) AS pl ON
--	    pl.jbplvl = c.bvschl
--	WHERE CASE WHEN bvadr6 = '' THEN bvname ELSE bvadr6 END = 'GRIFFIN'
