declare @ownerId int = ; declare @ownerType varchar(3) = ; with data as ( select bundleId, bundleName, active , ( select count(*) from tbl_BCRM_Objectives o with (nolock) where o.ownerId = b.ownerId and o.ownerType = b.ownerType and o.bundleId = b.bundleId ) as objectiveCount from tbl_BCRM_ObjectiveBundles b with (nolock) where ownerId = @ownerId and ownerType = @ownerType ) #forJSON( 'data', attributes.datastore, 'bundleName' )#