with bgs as ( select bg.name as bgName, bg.uniqueId, cdr.cdr_recordId as dstId from tbl_CDR_Distributors cdr with (nolock) inner join tbl_BYG_BuyingGroups bg with (nolock) on cdr.fslBuyingGroupId = bg.uniqueId where cdr.ownerId = and cdr.fsl_tablecode = and cdr.cdr_dstcompanyType = 'G' ), bgsFormatted as ( select * ,[#attributes.datastore.map[_mc]#] as [#_mc#] from bgs ) #forJSON( 'bgsFormatted', attributes.dataStore, 'bgName' )#