SELECT DISTINCT c.setid , c.cust_id , L.LANGUAGE_CD , L.name1 FROM ps_customer C , ps_cust_address A , PS_CUST_OPTION X , PS_CUST_TEAM CT , PS_TEAM_CODE_TBL TCT , PS_TEAM_MEMBERS TM , PS_CUSTOMER_LANG L WHERE c.cust_status = 'A' AND c.bill_to_flg = 'Y' AND c.cust_level <> 'P' AND C.setid = A.setid AND C.cust_id = A.cust_id AND C.setid = L.setid AND C.cust_id = L.cust_id AND A.effdt = ( SELECT MAX(effdt) FROM ps_cust_address X WHERE A.setid = X.setid AND A.cust_id = X.cust_id AND A.address_seq_num = X.address_seq_num AND effdt <= %CurrentDateIn AND eff_status = 'A') AND C.SETID = X.SETID AND C.CUST_ID = X.CUST_ID AND X.EFFDT = ( SELECT MAX(EFFDT) FROM PS_CUST_OPTION WHERE SETID = X.SETID AND CUST_ID = X.CUST_ID AND EFF_STATUS = 'A' AND EFFDT <= %CurrentDateIn) AND CT.SETID = C.SETID AND CT.CUST_ID = C.CUST_ID AND CT.DEFAULT_FLAG = 'Y' AND TCT.SETID = ( SELECT SETID FROM PS_SET_CNTRL_REC WHERE RECNAME = 'TEAM_CODE_TBL' AND SETCNTRLVALUE = CT.SETID) AND TCT.SUPPORT_TEAM_CD = CT.SUPPORT_TEAM_CD AND TCT.EFFDT = ( SELECT MAX(X2.EFFDT) FROM PS_TEAM_CODE_TBL X2 WHERE X2.SETID = TCT.SETID AND X2.SUPPORT_TEAM_CD = CT.SUPPORT_TEAM_CD AND X2.EFFDT <= %CurrentDateIn AND X2.EFF_STATUS = 'A') AND TM.SETID = TCT.SETID AND TM.SUPPORT_TEAM_CD = TCT.SUPPORT_TEAM_CD AND TM.EFFDT = TCT.EFFDT AND TM.PRIORITY = ( SELECT MIN(Y.PRIORITY) FROM PS_TEAM_MEMBERS Y , PS_MEMBER_PERSON MP , PS_MEMBER_TYPE_TBL MT WHERE Y.SETID = TM.SETID AND Y.SUPPORT_TEAM_CD = TM.SUPPORT_TEAM_CD AND Y.EFFDT = TM.EFFDT AND Y.PRIORITY > 0 AND MP.SETID = Y.SETID AND MP.SUPPORT_TEAM_MBR = Y.SUPPORT_TEAM_MBR AND MT.SETID = MP.SETID AND MT.TEAM_MEMBER_TYPE = MP.TEAM_MEMBER_TYPE AND MT.EFFDT = ( SELECT MAX(MT2.EFFDT) FROM PS_MEMBER_TYPE_TBL MT2 WHERE MT2.SETID = MT.SETID AND MT2.TEAM_MEMBER_TYPE = MT.TEAM_MEMBER_TYPE AND MT2.EFFDT <= %CurrentDateIn AND MT2.EFF_STATUS = 'A') AND MT.SALES_PERSON = 'Y' )
|