declare @ownerId int = ; declare @ownerType varchar(3) = ; with brokers as ( select distinct b.broker_id as brokerId, b.broker_name as brokerName FROM tblBrokers b with (nolock) inner join tbl_SMA_BrokerOfficeLink sma with (nolock) on sma.brokerId = b.broker_id inner join tblBrokerOffices bo with (nolock) on bo.broker_id = b.broker_id and bo.broker_office_id = sma.brokerOfficeId WHERE sma.mfr_id = @ownerId ) #forJSON( 'brokers', attributes.datastore, 'brokerName' )#