select ter.territoryId, ter.ownerId, ter.fsl_tablecode, ter.name, ter.internalSalesId from tbl_TER_Territories ter inner join tbl_SMA_BrokerOfficeLink sma on sma.territoryId = ter.territoryId and sma.mfr_id = ter.ownerId where ter.ownerId = and ter.fsl_tablecode = 'MFR' and sma.brokerid = and ter.internalSalesId = and exists ( select * from tbl_MFR_TPF_Detail#_tpfSuffix# tpf inner join tbl_CDR_Distributors cdr on cdr.cdr_recordId = tpf.tpartnerId where tpf.tpartnerType = 'CDR' and tpf.mfr_id = ter.ownerId and cdr.cdr_territoryId = ter.territoryId and tpf.forecast_status = 'A' )