---> with interactionsBase as ( select * from tbl_DW_Interactions#_tableSuffix# with (nolock) where 1 = 1 and territoryId in ( ) and salesRepId in ( ) ), listScope as ( select distinct l.partnerType, pli.partnerId from tbl_LST_PartnerLists l with (nolock) inner join tbl_LST_PartnerListItems pli with (nolock) on pli.listId = l.listId where l.ownerId = and l.ownerType = and l.listId in ( ) ), interactions as ( select i.* from interactionsBase i inner join listScope l on l.partnerId = i.partnerId and l.partnerType = i.partnerType ), interactions as ( select * from interactionsBase ), interactionsFormatted as ( select *, sku + ' - ' + skuDesc as skuFull, product + ' - ' + shortDesc as productFull, catNum + ' - ' + category as categoryFull, salesRepFirstName + ' ' + salesRepLastName as salesRepFullName, case when budgetCategoryMgr_fspro_userId > 0 then budgetCategoryMgr_firstName + ' ' + budgetCategoryMgr_lastName else null end as budgetCategoryMgr_fullName, partnerType + ':' + cast( partnerId as varchar ) as partnerKey, case when datediff( day, priorInteractionDate, interactionDate ) > 365 or priorInteractionDate is null then 'Y' else 'N' end newAccount, case when datediff( day, salesRepPriorInteractionDate, interactionDate ) > 365 or salesRepPriorInteractionDate is null then 'Y' else 'N' end salesRepNewAccount, case when coalesce( salesRepOwnerId, 0 ) < 0 then 'Y' else 'N' end as salesRepDeleted from interactions ) #forJSON( 'interactionsFormatted', attributes.dataStore, _selectColumns[1], _selectColumns, listToArray( _summaryColumns ) )# attributes.datastore.writeToLog( _r );