declare @ownerId int = 78; declare @ownerType varchar(3) = 'MSC'; with repliconTasks as ( select case when f2.fieldName = 'REQ_CAPEXTASK' then 'CAPEX' else 'OPEX' end expenditureType, selectionValue as expenditureTask, selectionLabel expenditureName from tbl_SCRUM_FieldLookups f2 with (nolock) where f2.ownerid = @ownerId and f2.ownerType = @ownerType and f2.fieldName IN ('REQ_CAPEXTASK','REQ_OPEXTASK') ) , allProjects as ( select p.projectId, case when t.expenditureType is not null then p.name + ' (' + t.expenditureType + ')' else p.name end as projectName, p.expenditureTask from tbl_SCRUM_Projects p with (nolock) left outer join repliconTasks t on t.expenditureTask = p.expenditureTask where ownerId = @ownerId and ownerType = @ownerType ) #forJSON( 'allProjects', attributes.dataStore, 'projectName' )#