operatorDetails log {ts '2025-06-09 07:07:04'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13070325 drop table if exists [tmp_memberGroups_MFR6868_1749467224123]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749467224123] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749467224123] add constraint [pk_tmp_memberGroups_MFR6868_1749467224123] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749467224123] on [tmp_memberGroups_MFR6868_1749467224123] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749467224123]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749467224123] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749467224123] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749467224123] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749467224123] add constraint [pk_tmp_affiliations_MFR6868_1749467224123] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749467224123]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749467224123] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749467224123] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749467224123] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749467224123] add constraint [pk_tmp_memberships_MFR6868_1749467224123] primary key ( operatorId ) {ts '2025-06-09 07:07:04'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13070325 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749467224123] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:37:01'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749472621554]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472621554] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472621554] add constraint [pk_tmp_memberGroups_MFR6868_1749472621554] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472621554] on [tmp_memberGroups_MFR6868_1749472621554] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472621554]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472621554] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472621554] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472621554] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472621554] add constraint [pk_tmp_affiliations_MFR6868_1749472621554] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472621554]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472621554] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472621554] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472621554] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472621554] add constraint [pk_tmp_memberships_MFR6868_1749472621554] primary key ( operatorId ) {ts '2025-06-09 08:37:01'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472621554] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where [operatorId] = 14406913 ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:37:03'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749472623244]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472623244] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472623244] add constraint [pk_tmp_memberGroups_MFR6868_1749472623244] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472623244] on [tmp_memberGroups_MFR6868_1749472623244] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472623244]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472623244] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472623244] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472623244] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472623244] add constraint [pk_tmp_affiliations_MFR6868_1749472623244] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472623244]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472623244] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472623244] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472623244] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472623244] add constraint [pk_tmp_memberships_MFR6868_1749472623244] primary key ( operatorId ) {ts '2025-06-09 08:37:03'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472623244] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where [operatorId] = 14406913 ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:37:57'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9790025 drop table if exists [tmp_memberGroups_MFR6868_1749472676871]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472676871] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472676871] add constraint [pk_tmp_memberGroups_MFR6868_1749472676871] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472676871] on [tmp_memberGroups_MFR6868_1749472676871] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472676871]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472676871] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472676871] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472676871] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472676871] add constraint [pk_tmp_affiliations_MFR6868_1749472676871] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472676871]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472676871] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472676871] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472676871] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472676871] add constraint [pk_tmp_memberships_MFR6868_1749472676871] primary key ( operatorId ) {ts '2025-06-09 08:37:57'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9790025 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472676871] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:38:03'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14212108 drop table if exists [tmp_memberGroups_MFR6868_1749472683656]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472683656] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472683656] add constraint [pk_tmp_memberGroups_MFR6868_1749472683656] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472683656] on [tmp_memberGroups_MFR6868_1749472683656] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472683656]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472683656] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472683656] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472683656] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472683656] add constraint [pk_tmp_affiliations_MFR6868_1749472683656] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472683656]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472683656] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472683656] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472683656] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472683656] add constraint [pk_tmp_memberships_MFR6868_1749472683656] primary key ( operatorId ) {ts '2025-06-09 08:38:03'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14212108 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472683656] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:38:08'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13958607 drop table if exists [tmp_memberGroups_MFR6868_1749472688713]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472688713] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472688713] add constraint [pk_tmp_memberGroups_MFR6868_1749472688713] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472688713] on [tmp_memberGroups_MFR6868_1749472688713] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472688713]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472688713] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472688713] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472688713] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472688713] add constraint [pk_tmp_affiliations_MFR6868_1749472688713] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472688713]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472688713] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472688713] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472688713] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472688713] add constraint [pk_tmp_memberships_MFR6868_1749472688713] primary key ( operatorId ) {ts '2025-06-09 08:38:08'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13958607 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472688713] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:38:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9790025 drop table if exists [tmp_memberGroups_MFR6868_1749472696426]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472696426] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472696426] add constraint [pk_tmp_memberGroups_MFR6868_1749472696426] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472696426] on [tmp_memberGroups_MFR6868_1749472696426] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472696426]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472696426] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472696426] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472696426] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472696426] add constraint [pk_tmp_affiliations_MFR6868_1749472696426] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472696426]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472696426] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472696426] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472696426] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472696426] add constraint [pk_tmp_memberships_MFR6868_1749472696426] primary key ( operatorId ) {ts '2025-06-09 08:38:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9790025 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472696426] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:38:54'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14212108 drop table if exists [tmp_memberGroups_MFR6868_1749472734273]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472734273] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472734273] add constraint [pk_tmp_memberGroups_MFR6868_1749472734273] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472734273] on [tmp_memberGroups_MFR6868_1749472734273] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472734273]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472734273] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472734273] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472734273] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472734273] add constraint [pk_tmp_affiliations_MFR6868_1749472734273] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472734273]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472734273] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472734273] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472734273] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472734273] add constraint [pk_tmp_memberships_MFR6868_1749472734273] primary key ( operatorId ) {ts '2025-06-09 08:38:54'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14212108 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472734273] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:38:56'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13958607 drop table if exists [tmp_memberGroups_MFR6868_1749472736037]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472736037] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472736037] add constraint [pk_tmp_memberGroups_MFR6868_1749472736037] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472736037] on [tmp_memberGroups_MFR6868_1749472736037] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472736037]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472736037] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472736037] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472736037] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472736037] add constraint [pk_tmp_affiliations_MFR6868_1749472736037] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472736037]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472736037] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472736037] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472736037] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472736037] add constraint [pk_tmp_memberships_MFR6868_1749472736037] primary key ( operatorId ) {ts '2025-06-09 08:38:56'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13958607 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472736037] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:39:01'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341438 drop table if exists [tmp_memberGroups_MFR6868_1749472741539]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472741539] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472741539] add constraint [pk_tmp_memberGroups_MFR6868_1749472741539] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472741539] on [tmp_memberGroups_MFR6868_1749472741539] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472741539]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472741539] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472741539] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472741539] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472741539] add constraint [pk_tmp_affiliations_MFR6868_1749472741539] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472741539]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472741539] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472741539] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472741539] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472741539] add constraint [pk_tmp_memberships_MFR6868_1749472741539] primary key ( operatorId ) {ts '2025-06-09 08:39:01'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341438 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472741539] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:39:06'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341440 drop table if exists [tmp_memberGroups_MFR6868_1749472746531]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472746531] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472746531] add constraint [pk_tmp_memberGroups_MFR6868_1749472746531] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472746531] on [tmp_memberGroups_MFR6868_1749472746531] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472746531]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472746531] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472746531] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472746531] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472746531] add constraint [pk_tmp_affiliations_MFR6868_1749472746531] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472746531]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472746531] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472746531] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472746531] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472746531] add constraint [pk_tmp_memberships_MFR6868_1749472746531] primary key ( operatorId ) {ts '2025-06-09 08:39:06'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341440 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472746531] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:39:12'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749472752195]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472752195] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472752195] add constraint [pk_tmp_memberGroups_MFR6868_1749472752195] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472752195] on [tmp_memberGroups_MFR6868_1749472752195] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472752195]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472752195] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472752195] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472752195] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472752195] add constraint [pk_tmp_affiliations_MFR6868_1749472752195] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472752195]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472752195] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472752195] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472752195] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472752195] add constraint [pk_tmp_memberships_MFR6868_1749472752195] primary key ( operatorId ) {ts '2025-06-09 08:39:12'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472752195] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 08:39:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749472756065]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749472756065] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749472756065] add constraint [pk_tmp_memberGroups_MFR6868_1749472756065] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749472756065] on [tmp_memberGroups_MFR6868_1749472756065] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749472756065]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472756065] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749472756065] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749472756065] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749472756065] add constraint [pk_tmp_affiliations_MFR6868_1749472756065] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749472756065]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749472756065] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749472756065] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749472756065] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749472756065] add constraint [pk_tmp_memberships_MFR6868_1749472756065] primary key ( operatorId ) {ts '2025-06-09 08:39:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749472756065] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:08:29'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9943479 drop table if exists [tmp_memberGroups_MFR6868_1749488909139]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749488909139] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749488909139] add constraint [pk_tmp_memberGroups_MFR6868_1749488909139] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749488909139] on [tmp_memberGroups_MFR6868_1749488909139] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749488909139]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749488909139] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749488909139] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749488909139] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749488909139] add constraint [pk_tmp_affiliations_MFR6868_1749488909139] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749488909139]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749488909139] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749488909139] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749488909139] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749488909139] add constraint [pk_tmp_memberships_MFR6868_1749488909139] primary key ( operatorId ) {ts '2025-06-09 13:08:29'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9943479 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749488909139] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where [operatorId] = 9943479 ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:12:03'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9943479 drop table if exists [tmp_memberGroups_MFR6868_1749489123802]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489123802] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489123802] add constraint [pk_tmp_memberGroups_MFR6868_1749489123802] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489123802] on [tmp_memberGroups_MFR6868_1749489123802] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489123802]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489123802] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489123802] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489123802] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489123802] add constraint [pk_tmp_affiliations_MFR6868_1749489123802] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489123802]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489123802] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489123802] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489123802] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489123802] add constraint [pk_tmp_memberships_MFR6868_1749489123802] primary key ( operatorId ) {ts '2025-06-09 13:12:04'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9943479 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489123802] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where [operatorId] = 9943479 ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:14:04'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9790025 drop table if exists [tmp_memberGroups_MFR6868_1749489243984]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489243984] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489243984] add constraint [pk_tmp_memberGroups_MFR6868_1749489243984] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489243984] on [tmp_memberGroups_MFR6868_1749489243984] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489243984]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489243984] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489243984] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489243984] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489243984] add constraint [pk_tmp_affiliations_MFR6868_1749489243984] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489243984]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489243984] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489243984] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489243984] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489243984] add constraint [pk_tmp_memberships_MFR6868_1749489243984] primary key ( operatorId ) {ts '2025-06-09 13:14:04'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9790025 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489243984] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:14:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341438 drop table if exists [tmp_memberGroups_MFR6868_1749489256166]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489256166] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489256166] add constraint [pk_tmp_memberGroups_MFR6868_1749489256166] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489256166] on [tmp_memberGroups_MFR6868_1749489256166] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489256166]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489256166] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489256166] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489256166] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489256166] add constraint [pk_tmp_affiliations_MFR6868_1749489256166] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489256166]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489256166] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489256166] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489256166] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489256166] add constraint [pk_tmp_memberships_MFR6868_1749489256166] primary key ( operatorId ) {ts '2025-06-09 13:14:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341438 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489256166] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:14:30'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749489270017]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489270017] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489270017] add constraint [pk_tmp_memberGroups_MFR6868_1749489270017] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489270017] on [tmp_memberGroups_MFR6868_1749489270017] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489270017]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489270017] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489270017] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489270017] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489270017] add constraint [pk_tmp_affiliations_MFR6868_1749489270017] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489270017]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489270017] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489270017] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489270017] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489270017] add constraint [pk_tmp_memberships_MFR6868_1749489270017] primary key ( operatorId ) {ts '2025-06-09 13:14:30'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489270017] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:14:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749489279113]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489279113] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489279113] add constraint [pk_tmp_memberGroups_MFR6868_1749489279113] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489279113] on [tmp_memberGroups_MFR6868_1749489279113] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489279113]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489279113] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489279113] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489279113] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489279113] add constraint [pk_tmp_affiliations_MFR6868_1749489279113] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489279113]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489279113] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489279113] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489279113] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489279113] add constraint [pk_tmp_memberships_MFR6868_1749489279113] primary key ( operatorId ) {ts '2025-06-09 13:14:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489279113] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:24:09'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749489849096]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489849096] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489849096] add constraint [pk_tmp_memberGroups_MFR6868_1749489849096] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489849096] on [tmp_memberGroups_MFR6868_1749489849096] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489849096]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489849096] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489849096] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489849096] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489849096] add constraint [pk_tmp_affiliations_MFR6868_1749489849096] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489849096]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489849096] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489849096] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489849096] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489849096] add constraint [pk_tmp_memberships_MFR6868_1749489849096] primary key ( operatorId ) {ts '2025-06-09 13:24:09'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489849096] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:24:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749489879520]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489879520] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489879520] add constraint [pk_tmp_memberGroups_MFR6868_1749489879520] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489879520] on [tmp_memberGroups_MFR6868_1749489879520] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489879520]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489879520] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489879520] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489879520] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489879520] add constraint [pk_tmp_affiliations_MFR6868_1749489879520] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489879520]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489879520] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489879520] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489879520] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489879520] add constraint [pk_tmp_memberships_MFR6868_1749489879520] primary key ( operatorId ) {ts '2025-06-09 13:24:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489879520] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:25:42'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749489942769]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749489942769] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749489942769] add constraint [pk_tmp_memberGroups_MFR6868_1749489942769] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749489942769] on [tmp_memberGroups_MFR6868_1749489942769] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749489942769]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489942769] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749489942769] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749489942769] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749489942769] add constraint [pk_tmp_affiliations_MFR6868_1749489942769] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749489942769]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749489942769] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749489942769] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749489942769] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749489942769] add constraint [pk_tmp_memberships_MFR6868_1749489942769] primary key ( operatorId ) {ts '2025-06-09 13:25:42'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749489942769] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:30:25'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490225154]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490225154] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490225154] add constraint [pk_tmp_memberGroups_MFR6868_1749490225154] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490225154] on [tmp_memberGroups_MFR6868_1749490225154] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490225154]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490225154] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490225154] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490225154] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490225154] add constraint [pk_tmp_affiliations_MFR6868_1749490225154] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490225154]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490225154] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490225154] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490225154] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490225154] add constraint [pk_tmp_memberships_MFR6868_1749490225154] primary key ( operatorId ) {ts '2025-06-09 13:30:25'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490225154] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:31:28'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490288799]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490288799] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490288799] add constraint [pk_tmp_memberGroups_MFR6868_1749490288799] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490288799] on [tmp_memberGroups_MFR6868_1749490288799] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490288799]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490288799] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490288799] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490288799] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490288799] add constraint [pk_tmp_affiliations_MFR6868_1749490288799] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490288799]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490288799] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490288799] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490288799] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490288799] add constraint [pk_tmp_memberships_MFR6868_1749490288799] primary key ( operatorId ) {ts '2025-06-09 13:31:29'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490288799] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:33:06'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490386450]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490386450] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490386450] add constraint [pk_tmp_memberGroups_MFR6868_1749490386450] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490386450] on [tmp_memberGroups_MFR6868_1749490386450] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490386450]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490386450] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490386450] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490386450] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490386450] add constraint [pk_tmp_affiliations_MFR6868_1749490386450] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490386450]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490386450] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490386450] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490386450] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490386450] add constraint [pk_tmp_memberships_MFR6868_1749490386450] primary key ( operatorId ) {ts '2025-06-09 13:33:06'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490386450] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:33:31'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490411549]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490411549] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490411549] add constraint [pk_tmp_memberGroups_MFR6868_1749490411549] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490411549] on [tmp_memberGroups_MFR6868_1749490411549] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490411549]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490411549] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490411549] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490411549] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490411549] add constraint [pk_tmp_affiliations_MFR6868_1749490411549] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490411549]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490411549] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490411549] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490411549] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490411549] add constraint [pk_tmp_memberships_MFR6868_1749490411549] primary key ( operatorId ) {ts '2025-06-09 13:33:31'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490411549] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:34:13'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490453874]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490453874] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490453874] add constraint [pk_tmp_memberGroups_MFR6868_1749490453874] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490453874] on [tmp_memberGroups_MFR6868_1749490453874] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490453874]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490453874] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490453874] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490453874] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490453874] add constraint [pk_tmp_affiliations_MFR6868_1749490453874] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490453874]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490453874] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490453874] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490453874] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490453874] add constraint [pk_tmp_memberships_MFR6868_1749490453874] primary key ( operatorId ) {ts '2025-06-09 13:34:14'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490453874] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:34:43'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490483005]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490483005] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490483005] add constraint [pk_tmp_memberGroups_MFR6868_1749490483005] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490483005] on [tmp_memberGroups_MFR6868_1749490483005] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490483005]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490483005] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490483005] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490483005] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490483005] add constraint [pk_tmp_affiliations_MFR6868_1749490483005] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490483005]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490483005] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490483005] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490483005] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490483005] add constraint [pk_tmp_memberships_MFR6868_1749490483005] primary key ( operatorId ) {ts '2025-06-09 13:34:43'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490483005] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:35:07'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490507053]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490507053] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490507053] add constraint [pk_tmp_memberGroups_MFR6868_1749490507053] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490507053] on [tmp_memberGroups_MFR6868_1749490507053] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490507053]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490507053] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490507053] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490507053] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490507053] add constraint [pk_tmp_affiliations_MFR6868_1749490507053] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490507053]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490507053] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490507053] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490507053] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490507053] add constraint [pk_tmp_memberships_MFR6868_1749490507053] primary key ( operatorId ) {ts '2025-06-09 13:35:07'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490507053] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:36:09'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490569668]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490569668] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490569668] add constraint [pk_tmp_memberGroups_MFR6868_1749490569668] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490569668] on [tmp_memberGroups_MFR6868_1749490569668] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490569668]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490569668] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490569668] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490569668] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490569668] add constraint [pk_tmp_affiliations_MFR6868_1749490569668] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490569668]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490569668] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490569668] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490569668] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490569668] add constraint [pk_tmp_memberships_MFR6868_1749490569668] primary key ( operatorId ) {ts '2025-06-09 13:36:09'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490569668] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:36:36'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490596605]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490596605] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490596605] add constraint [pk_tmp_memberGroups_MFR6868_1749490596605] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490596605] on [tmp_memberGroups_MFR6868_1749490596605] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490596605]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490596605] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490596605] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490596605] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490596605] add constraint [pk_tmp_affiliations_MFR6868_1749490596605] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490596605]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490596605] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490596605] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490596605] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490596605] add constraint [pk_tmp_memberships_MFR6868_1749490596605] primary key ( operatorId ) {ts '2025-06-09 13:36:36'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490596605] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:37:55'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749490675315]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749490675315] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749490675315] add constraint [pk_tmp_memberGroups_MFR6868_1749490675315] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749490675315] on [tmp_memberGroups_MFR6868_1749490675315] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749490675315]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490675315] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749490675315] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749490675315] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749490675315] add constraint [pk_tmp_affiliations_MFR6868_1749490675315] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749490675315]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749490675315] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749490675315] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749490675315] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749490675315] add constraint [pk_tmp_memberships_MFR6868_1749490675315] primary key ( operatorId ) {ts '2025-06-09 13:37:55'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749490675315] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:49:21'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491361126]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491361126] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491361126] add constraint [pk_tmp_memberGroups_MFR6868_1749491361126] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491361126] on [tmp_memberGroups_MFR6868_1749491361126] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491361126]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491361126] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491361126] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491361126] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491361126] add constraint [pk_tmp_affiliations_MFR6868_1749491361126] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491361126]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491361126] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491361126] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491361126] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491361126] add constraint [pk_tmp_memberships_MFR6868_1749491361126] primary key ( operatorId ) {ts '2025-06-09 13:49:21'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491361126] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:49:54'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491394749]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491394749] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491394749] add constraint [pk_tmp_memberGroups_MFR6868_1749491394749] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491394749] on [tmp_memberGroups_MFR6868_1749491394749] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491394749]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491394749] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491394749] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491394749] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491394749] add constraint [pk_tmp_affiliations_MFR6868_1749491394749] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491394749]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491394749] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491394749] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491394749] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491394749] add constraint [pk_tmp_memberships_MFR6868_1749491394749] primary key ( operatorId ) {ts '2025-06-09 13:49:54'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491394749] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:50:58'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491457996]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491457996] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491457996] add constraint [pk_tmp_memberGroups_MFR6868_1749491457996] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491457996] on [tmp_memberGroups_MFR6868_1749491457996] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491457996]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491457996] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491457996] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491457996] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491457996] add constraint [pk_tmp_affiliations_MFR6868_1749491457996] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491457996]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491457996] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491457996] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491457996] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491457996] add constraint [pk_tmp_memberships_MFR6868_1749491457996] primary key ( operatorId ) {ts '2025-06-09 13:50:58'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491457996] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:51:41'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491501548]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491501548] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491501548] add constraint [pk_tmp_memberGroups_MFR6868_1749491501548] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491501548] on [tmp_memberGroups_MFR6868_1749491501548] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491501548]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491501548] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491501548] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491501548] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491501548] add constraint [pk_tmp_affiliations_MFR6868_1749491501548] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491501548]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491501548] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491501548] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491501548] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491501548] add constraint [pk_tmp_memberships_MFR6868_1749491501548] primary key ( operatorId ) {ts '2025-06-09 13:51:41'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491501548] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:52:00'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491519898]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491519898] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491519898] add constraint [pk_tmp_memberGroups_MFR6868_1749491519898] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491519898] on [tmp_memberGroups_MFR6868_1749491519898] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491519898]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491519898] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491519898] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491519898] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491519898] add constraint [pk_tmp_affiliations_MFR6868_1749491519898] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491519898]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491519898] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491519898] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491519898] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491519898] add constraint [pk_tmp_memberships_MFR6868_1749491519898] primary key ( operatorId ) {ts '2025-06-09 13:52:00'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491519898] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:52:41'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491560975]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491560975] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491560975] add constraint [pk_tmp_memberGroups_MFR6868_1749491560975] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491560975] on [tmp_memberGroups_MFR6868_1749491560975] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491560975]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491560975] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491560975] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491560975] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491560975] add constraint [pk_tmp_affiliations_MFR6868_1749491560975] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491560975]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491560975] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491560975] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491560975] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491560975] add constraint [pk_tmp_memberships_MFR6868_1749491560975] primary key ( operatorId ) {ts '2025-06-09 13:52:41'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491560975] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:53:19'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491599125]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491599125] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491599125] add constraint [pk_tmp_memberGroups_MFR6868_1749491599125] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491599125] on [tmp_memberGroups_MFR6868_1749491599125] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491599125]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491599125] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491599125] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491599125] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491599125] add constraint [pk_tmp_affiliations_MFR6868_1749491599125] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491599125]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491599125] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491599125] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491599125] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491599125] add constraint [pk_tmp_memberships_MFR6868_1749491599125] primary key ( operatorId ) {ts '2025-06-09 13:53:19'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491599125] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:56:32'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491792242]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491792242] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491792242] add constraint [pk_tmp_memberGroups_MFR6868_1749491792242] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491792242] on [tmp_memberGroups_MFR6868_1749491792242] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491792242]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491792242] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491792242] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491792242] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491792242] add constraint [pk_tmp_affiliations_MFR6868_1749491792242] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491792242]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491792242] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491792242] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491792242] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491792242] add constraint [pk_tmp_memberships_MFR6868_1749491792242] primary key ( operatorId ) {ts '2025-06-09 13:56:32'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491792242] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:57:18'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491838554]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491838554] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491838554] add constraint [pk_tmp_memberGroups_MFR6868_1749491838554] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491838554] on [tmp_memberGroups_MFR6868_1749491838554] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491838554]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491838554] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491838554] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491838554] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491838554] add constraint [pk_tmp_affiliations_MFR6868_1749491838554] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491838554]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491838554] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491838554] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491838554] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491838554] add constraint [pk_tmp_memberships_MFR6868_1749491838554] primary key ( operatorId ) {ts '2025-06-09 13:57:18'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491838554] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:57:40'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491859930]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491859930] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491859930] add constraint [pk_tmp_memberGroups_MFR6868_1749491859930] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491859930] on [tmp_memberGroups_MFR6868_1749491859930] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491859930]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491859930] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491859930] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491859930] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491859930] add constraint [pk_tmp_affiliations_MFR6868_1749491859930] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491859930]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491859930] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491859930] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491859930] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491859930] add constraint [pk_tmp_memberships_MFR6868_1749491859930] primary key ( operatorId ) {ts '2025-06-09 13:57:40'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491859930] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:57:57'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491877565]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491877565] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491877565] add constraint [pk_tmp_memberGroups_MFR6868_1749491877565] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491877565] on [tmp_memberGroups_MFR6868_1749491877565] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491877565]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491877565] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491877565] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491877565] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491877565] add constraint [pk_tmp_affiliations_MFR6868_1749491877565] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491877565]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491877565] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491877565] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491877565] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491877565] add constraint [pk_tmp_memberships_MFR6868_1749491877565] primary key ( operatorId ) {ts '2025-06-09 13:57:57'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491877565] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:58:34'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491914628]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491914628] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491914628] add constraint [pk_tmp_memberGroups_MFR6868_1749491914628] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491914628] on [tmp_memberGroups_MFR6868_1749491914628] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491914628]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491914628] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491914628] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491914628] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491914628] add constraint [pk_tmp_affiliations_MFR6868_1749491914628] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491914628]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491914628] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491914628] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491914628] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491914628] add constraint [pk_tmp_memberships_MFR6868_1749491914628] primary key ( operatorId ) {ts '2025-06-09 13:58:34'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491914628] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:59:04'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491943961]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491943961] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491943961] add constraint [pk_tmp_memberGroups_MFR6868_1749491943961] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491943961] on [tmp_memberGroups_MFR6868_1749491943961] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491943961]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491943961] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491943961] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491943961] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491943961] add constraint [pk_tmp_affiliations_MFR6868_1749491943961] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491943961]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491943961] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491943961] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491943961] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491943961] add constraint [pk_tmp_memberships_MFR6868_1749491943961] primary key ( operatorId ) {ts '2025-06-09 13:59:04'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491943961] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:59:32'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491972509]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491972509] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491972509] add constraint [pk_tmp_memberGroups_MFR6868_1749491972509] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491972509] on [tmp_memberGroups_MFR6868_1749491972509] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491972509]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491972509] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491972509] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491972509] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491972509] add constraint [pk_tmp_affiliations_MFR6868_1749491972509] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491972509]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491972509] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491972509] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491972509] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491972509] add constraint [pk_tmp_memberships_MFR6868_1749491972509] primary key ( operatorId ) {ts '2025-06-09 13:59:32'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491972509] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 13:59:59'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749491999670]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749491999670] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749491999670] add constraint [pk_tmp_memberGroups_MFR6868_1749491999670] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749491999670] on [tmp_memberGroups_MFR6868_1749491999670] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749491999670]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491999670] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749491999670] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749491999670] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749491999670] add constraint [pk_tmp_affiliations_MFR6868_1749491999670] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749491999670]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749491999670] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749491999670] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749491999670] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749491999670] add constraint [pk_tmp_memberships_MFR6868_1749491999670] primary key ( operatorId ) {ts '2025-06-09 13:59:59'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749491999670] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:01:14'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492074076]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492074076] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492074076] add constraint [pk_tmp_memberGroups_MFR6868_1749492074076] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492074076] on [tmp_memberGroups_MFR6868_1749492074076] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492074076]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492074076] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492074076] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492074076] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492074076] add constraint [pk_tmp_affiliations_MFR6868_1749492074076] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492074076]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492074076] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492074076] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492074076] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492074076] add constraint [pk_tmp_memberships_MFR6868_1749492074076] primary key ( operatorId ) {ts '2025-06-09 14:01:14'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492074076] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:01:59'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492118893]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492118893] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492118893] add constraint [pk_tmp_memberGroups_MFR6868_1749492118893] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492118893] on [tmp_memberGroups_MFR6868_1749492118893] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492118893]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492118893] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492118893] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492118893] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492118893] add constraint [pk_tmp_affiliations_MFR6868_1749492118893] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492118893]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492118893] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492118893] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492118893] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492118893] add constraint [pk_tmp_memberships_MFR6868_1749492118893] primary key ( operatorId ) {ts '2025-06-09 14:01:59'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492118893] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:06:43'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492403108]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492403108] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492403108] add constraint [pk_tmp_memberGroups_MFR6868_1749492403108] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492403108] on [tmp_memberGroups_MFR6868_1749492403108] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492403108]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492403108] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492403108] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492403108] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492403108] add constraint [pk_tmp_affiliations_MFR6868_1749492403108] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492403108]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492403108] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492403108] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492403108] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492403108] add constraint [pk_tmp_memberships_MFR6868_1749492403108] primary key ( operatorId ) {ts '2025-06-09 14:06:43'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492403108] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:07:20'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492440567]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492440567] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492440567] add constraint [pk_tmp_memberGroups_MFR6868_1749492440567] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492440567] on [tmp_memberGroups_MFR6868_1749492440567] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492440567]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492440567] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492440567] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492440567] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492440567] add constraint [pk_tmp_affiliations_MFR6868_1749492440567] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492440567]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492440567] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492440567] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492440567] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492440567] add constraint [pk_tmp_memberships_MFR6868_1749492440567] primary key ( operatorId ) {ts '2025-06-09 14:07:20'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492440567] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:08:31'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492511279]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492511279] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492511279] add constraint [pk_tmp_memberGroups_MFR6868_1749492511279] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492511279] on [tmp_memberGroups_MFR6868_1749492511279] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492511279]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492511279] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492511279] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492511279] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492511279] add constraint [pk_tmp_affiliations_MFR6868_1749492511279] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492511279]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492511279] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492511279] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492511279] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492511279] add constraint [pk_tmp_memberships_MFR6868_1749492511279] primary key ( operatorId ) {ts '2025-06-09 14:08:31'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492511279] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:11:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492699199]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492699199] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492699199] add constraint [pk_tmp_memberGroups_MFR6868_1749492699199] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492699199] on [tmp_memberGroups_MFR6868_1749492699199] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492699199]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492699199] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492699199] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492699199] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492699199] add constraint [pk_tmp_affiliations_MFR6868_1749492699199] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492699199]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492699199] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492699199] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492699199] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492699199] add constraint [pk_tmp_memberships_MFR6868_1749492699199] primary key ( operatorId ) {ts '2025-06-09 14:11:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492699199] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:16:33'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749492992974]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749492992974] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749492992974] add constraint [pk_tmp_memberGroups_MFR6868_1749492992974] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749492992974] on [tmp_memberGroups_MFR6868_1749492992974] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749492992974]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492992974] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749492992974] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749492992974] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749492992974] add constraint [pk_tmp_affiliations_MFR6868_1749492992974] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749492992974]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749492992974] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749492992974] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749492992974] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749492992974] add constraint [pk_tmp_memberships_MFR6868_1749492992974] primary key ( operatorId ) {ts '2025-06-09 14:16:33'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749492992974] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:20:34'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493234178]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493234178] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493234178] add constraint [pk_tmp_memberGroups_MFR6868_1749493234178] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493234178] on [tmp_memberGroups_MFR6868_1749493234178] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493234178]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493234178] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493234178] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493234178] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493234178] add constraint [pk_tmp_affiliations_MFR6868_1749493234178] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493234178]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493234178] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493234178] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493234178] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493234178] add constraint [pk_tmp_memberships_MFR6868_1749493234178] primary key ( operatorId ) {ts '2025-06-09 14:20:34'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493234178] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:25:23'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493523855]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493523855] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493523855] add constraint [pk_tmp_memberGroups_MFR6868_1749493523855] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493523855] on [tmp_memberGroups_MFR6868_1749493523855] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493523855]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493523855] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493523855] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493523855] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493523855] add constraint [pk_tmp_affiliations_MFR6868_1749493523855] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493523855]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493523855] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493523855] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493523855] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493523855] add constraint [pk_tmp_memberships_MFR6868_1749493523855] primary key ( operatorId ) {ts '2025-06-09 14:25:24'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493523855] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:26:14'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493574065]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493574065] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493574065] add constraint [pk_tmp_memberGroups_MFR6868_1749493574065] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493574065] on [tmp_memberGroups_MFR6868_1749493574065] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493574065]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493574065] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493574065] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493574065] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493574065] add constraint [pk_tmp_affiliations_MFR6868_1749493574065] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493574065]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493574065] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493574065] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493574065] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493574065] add constraint [pk_tmp_memberships_MFR6868_1749493574065] primary key ( operatorId ) {ts '2025-06-09 14:26:14'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493574065] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:27:17'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493637157]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493637157] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493637157] add constraint [pk_tmp_memberGroups_MFR6868_1749493637157] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493637157] on [tmp_memberGroups_MFR6868_1749493637157] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493637157]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493637157] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493637157] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493637157] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493637157] add constraint [pk_tmp_affiliations_MFR6868_1749493637157] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493637157]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493637157] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493637157] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493637157] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493637157] add constraint [pk_tmp_memberships_MFR6868_1749493637157] primary key ( operatorId ) {ts '2025-06-09 14:27:17'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493637157] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:27:59'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493679401]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493679401] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493679401] add constraint [pk_tmp_memberGroups_MFR6868_1749493679401] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493679401] on [tmp_memberGroups_MFR6868_1749493679401] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493679401]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493679401] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493679401] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493679401] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493679401] add constraint [pk_tmp_affiliations_MFR6868_1749493679401] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493679401]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493679401] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493679401] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493679401] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493679401] add constraint [pk_tmp_memberships_MFR6868_1749493679401] primary key ( operatorId ) {ts '2025-06-09 14:27:59'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493679401] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:30:14'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493814369]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493814369] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493814369] add constraint [pk_tmp_memberGroups_MFR6868_1749493814369] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493814369] on [tmp_memberGroups_MFR6868_1749493814369] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493814369]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493814369] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493814369] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493814369] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493814369] add constraint [pk_tmp_affiliations_MFR6868_1749493814369] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493814369]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493814369] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493814369] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493814369] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493814369] add constraint [pk_tmp_memberships_MFR6868_1749493814369] primary key ( operatorId ) {ts '2025-06-09 14:30:14'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493814369] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:32:34'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749493954215]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749493954215] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749493954215] add constraint [pk_tmp_memberGroups_MFR6868_1749493954215] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749493954215] on [tmp_memberGroups_MFR6868_1749493954215] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749493954215]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493954215] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749493954215] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749493954215] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749493954215] add constraint [pk_tmp_affiliations_MFR6868_1749493954215] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749493954215]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749493954215] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749493954215] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749493954215] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749493954215] add constraint [pk_tmp_memberships_MFR6868_1749493954215] primary key ( operatorId ) {ts '2025-06-09 14:32:34'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749493954215] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:34:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494056342]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494056342] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494056342] add constraint [pk_tmp_memberGroups_MFR6868_1749494056342] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494056342] on [tmp_memberGroups_MFR6868_1749494056342] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494056342]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494056342] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494056342] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494056342] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494056342] add constraint [pk_tmp_affiliations_MFR6868_1749494056342] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494056342]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494056342] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494056342] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494056342] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494056342] add constraint [pk_tmp_memberships_MFR6868_1749494056342] primary key ( operatorId ) {ts '2025-06-09 14:34:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494056342] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:35:15'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494115195]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494115195] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494115195] add constraint [pk_tmp_memberGroups_MFR6868_1749494115195] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494115195] on [tmp_memberGroups_MFR6868_1749494115195] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494115195]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494115195] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494115195] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494115195] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494115195] add constraint [pk_tmp_affiliations_MFR6868_1749494115195] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494115195]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494115195] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494115195] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494115195] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494115195] add constraint [pk_tmp_memberships_MFR6868_1749494115195] primary key ( operatorId ) {ts '2025-06-09 14:35:15'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494115195] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:36:20'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494179954]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494179954] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494179954] add constraint [pk_tmp_memberGroups_MFR6868_1749494179954] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494179954] on [tmp_memberGroups_MFR6868_1749494179954] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494179954]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494179954] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494179954] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494179954] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494179954] add constraint [pk_tmp_affiliations_MFR6868_1749494179954] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494179954]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494179954] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494179954] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494179954] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494179954] add constraint [pk_tmp_memberships_MFR6868_1749494179954] primary key ( operatorId ) {ts '2025-06-09 14:36:20'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494179954] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:38:54'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494334539]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494334539] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494334539] add constraint [pk_tmp_memberGroups_MFR6868_1749494334539] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494334539] on [tmp_memberGroups_MFR6868_1749494334539] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494334539]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494334539] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494334539] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494334539] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494334539] add constraint [pk_tmp_affiliations_MFR6868_1749494334539] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494334539]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494334539] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494334539] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494334539] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494334539] add constraint [pk_tmp_memberships_MFR6868_1749494334539] primary key ( operatorId ) {ts '2025-06-09 14:38:54'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494334539] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:39:45'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494385193]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494385193] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494385193] add constraint [pk_tmp_memberGroups_MFR6868_1749494385193] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494385193] on [tmp_memberGroups_MFR6868_1749494385193] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494385193]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494385193] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494385193] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494385193] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494385193] add constraint [pk_tmp_affiliations_MFR6868_1749494385193] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494385193]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494385193] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494385193] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494385193] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494385193] add constraint [pk_tmp_memberships_MFR6868_1749494385193] primary key ( operatorId ) {ts '2025-06-09 14:39:45'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494385193] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:42:17'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494537435]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494537435] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494537435] add constraint [pk_tmp_memberGroups_MFR6868_1749494537435] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494537435] on [tmp_memberGroups_MFR6868_1749494537435] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494537435]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494537435] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494537435] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494537435] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494537435] add constraint [pk_tmp_affiliations_MFR6868_1749494537435] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494537435]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494537435] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494537435] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494537435] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494537435] add constraint [pk_tmp_memberships_MFR6868_1749494537435] primary key ( operatorId ) {ts '2025-06-09 14:42:17'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494537435] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:43:21'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494601551]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494601551] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494601551] add constraint [pk_tmp_memberGroups_MFR6868_1749494601551] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494601551] on [tmp_memberGroups_MFR6868_1749494601551] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494601551]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494601551] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494601551] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494601551] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494601551] add constraint [pk_tmp_affiliations_MFR6868_1749494601551] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494601551]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494601551] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494601551] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494601551] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494601551] add constraint [pk_tmp_memberships_MFR6868_1749494601551] primary key ( operatorId ) {ts '2025-06-09 14:43:21'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494601551] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:44:49'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494689043]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494689043] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494689043] add constraint [pk_tmp_memberGroups_MFR6868_1749494689043] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494689043] on [tmp_memberGroups_MFR6868_1749494689043] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494689043]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494689043] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494689043] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494689043] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494689043] add constraint [pk_tmp_affiliations_MFR6868_1749494689043] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494689043]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494689043] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494689043] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494689043] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494689043] add constraint [pk_tmp_memberships_MFR6868_1749494689043] primary key ( operatorId ) {ts '2025-06-09 14:44:49'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494689043] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:46:00'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494759928]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494759928] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494759928] add constraint [pk_tmp_memberGroups_MFR6868_1749494759928] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494759928] on [tmp_memberGroups_MFR6868_1749494759928] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494759928]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494759928] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494759928] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494759928] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494759928] add constraint [pk_tmp_affiliations_MFR6868_1749494759928] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494759928]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494759928] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494759928] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494759928] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494759928] add constraint [pk_tmp_memberships_MFR6868_1749494759928] primary key ( operatorId ) {ts '2025-06-09 14:46:00'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494759928] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:47:56'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494876242]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494876242] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494876242] add constraint [pk_tmp_memberGroups_MFR6868_1749494876242] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494876242] on [tmp_memberGroups_MFR6868_1749494876242] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494876242]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494876242] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494876242] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494876242] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494876242] add constraint [pk_tmp_affiliations_MFR6868_1749494876242] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494876242]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494876242] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494876242] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494876242] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494876242] add constraint [pk_tmp_memberships_MFR6868_1749494876242] primary key ( operatorId ) {ts '2025-06-09 14:47:56'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494876242] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 14:48:27'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749494907118]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749494907118] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749494907118] add constraint [pk_tmp_memberGroups_MFR6868_1749494907118] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749494907118] on [tmp_memberGroups_MFR6868_1749494907118] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749494907118]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494907118] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749494907118] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749494907118] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749494907118] add constraint [pk_tmp_affiliations_MFR6868_1749494907118] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749494907118]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749494907118] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749494907118] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749494907118] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749494907118] add constraint [pk_tmp_memberships_MFR6868_1749494907118] primary key ( operatorId ) {ts '2025-06-09 14:48:27'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749494907118] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:48:17'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502097034]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502097034] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502097034] add constraint [pk_tmp_memberGroups_MFR6868_1749502097034] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502097034] on [tmp_memberGroups_MFR6868_1749502097034] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502097034]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502097034] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502097034] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502097034] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502097034] add constraint [pk_tmp_affiliations_MFR6868_1749502097034] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502097034]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502097034] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502097034] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502097034] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502097034] add constraint [pk_tmp_memberships_MFR6868_1749502097034] primary key ( operatorId ) {ts '2025-06-09 16:48:17'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502097034] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:49:10'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502150317]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502150317] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502150317] add constraint [pk_tmp_memberGroups_MFR6868_1749502150317] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502150317] on [tmp_memberGroups_MFR6868_1749502150317] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502150317]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502150317] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502150317] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502150317] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502150317] add constraint [pk_tmp_affiliations_MFR6868_1749502150317] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502150317]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502150317] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502150317] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502150317] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502150317] add constraint [pk_tmp_memberships_MFR6868_1749502150317] primary key ( operatorId ) {ts '2025-06-09 16:49:10'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502150317] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:49:37'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502177871]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502177871] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502177871] add constraint [pk_tmp_memberGroups_MFR6868_1749502177871] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502177871] on [tmp_memberGroups_MFR6868_1749502177871] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502177871]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502177871] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502177871] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502177871] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502177871] add constraint [pk_tmp_affiliations_MFR6868_1749502177871] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502177871]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502177871] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502177871] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502177871] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502177871] add constraint [pk_tmp_memberships_MFR6868_1749502177871] primary key ( operatorId ) {ts '2025-06-09 16:49:38'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502177871] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:52:06'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502326378]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502326378] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502326378] add constraint [pk_tmp_memberGroups_MFR6868_1749502326378] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502326378] on [tmp_memberGroups_MFR6868_1749502326378] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502326378]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502326378] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502326378] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502326378] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502326378] add constraint [pk_tmp_affiliations_MFR6868_1749502326378] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502326378]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502326378] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502326378] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502326378] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502326378] add constraint [pk_tmp_memberships_MFR6868_1749502326378] primary key ( operatorId ) {ts '2025-06-09 16:52:07'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502326378] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:52:48'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502368731]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502368731] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502368731] add constraint [pk_tmp_memberGroups_MFR6868_1749502368731] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502368731] on [tmp_memberGroups_MFR6868_1749502368731] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502368731]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502368731] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502368731] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502368731] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502368731] add constraint [pk_tmp_affiliations_MFR6868_1749502368731] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502368731]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502368731] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502368731] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502368731] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502368731] add constraint [pk_tmp_memberships_MFR6868_1749502368731] primary key ( operatorId ) {ts '2025-06-09 16:52:48'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502368731] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:55:08'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502508442]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502508442] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502508442] add constraint [pk_tmp_memberGroups_MFR6868_1749502508442] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502508442] on [tmp_memberGroups_MFR6868_1749502508442] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502508442]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502508442] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502508442] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502508442] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502508442] add constraint [pk_tmp_affiliations_MFR6868_1749502508442] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502508442]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502508442] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502508442] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502508442] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502508442] add constraint [pk_tmp_memberships_MFR6868_1749502508442] primary key ( operatorId ) {ts '2025-06-09 16:55:08'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502508442] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:56:48'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502608722]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502608722] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502608722] add constraint [pk_tmp_memberGroups_MFR6868_1749502608722] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502608722] on [tmp_memberGroups_MFR6868_1749502608722] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502608722]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502608722] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502608722] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502608722] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502608722] add constraint [pk_tmp_affiliations_MFR6868_1749502608722] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502608722]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502608722] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502608722] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502608722] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502608722] add constraint [pk_tmp_memberships_MFR6868_1749502608722] primary key ( operatorId ) {ts '2025-06-09 16:56:48'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502608722] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:57:54'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502674780]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502674780] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502674780] add constraint [pk_tmp_memberGroups_MFR6868_1749502674780] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502674780] on [tmp_memberGroups_MFR6868_1749502674780] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502674780]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502674780] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502674780] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502674780] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502674780] add constraint [pk_tmp_affiliations_MFR6868_1749502674780] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502674780]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502674780] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502674780] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502674780] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502674780] add constraint [pk_tmp_memberships_MFR6868_1749502674780] primary key ( operatorId ) {ts '2025-06-09 16:57:55'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502674780] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 16:58:49'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749502729198]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502729198] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502729198] add constraint [pk_tmp_memberGroups_MFR6868_1749502729198] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502729198] on [tmp_memberGroups_MFR6868_1749502729198] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502729198]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502729198] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502729198] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502729198] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502729198] add constraint [pk_tmp_affiliations_MFR6868_1749502729198] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502729198]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502729198] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502729198] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502729198] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502729198] add constraint [pk_tmp_memberships_MFR6868_1749502729198] primary key ( operatorId ) {ts '2025-06-09 16:58:49'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502729198] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:01:10'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749502870876]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749502870876] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749502870876] add constraint [pk_tmp_memberGroups_MFR6868_1749502870876] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749502870876] on [tmp_memberGroups_MFR6868_1749502870876] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749502870876]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502870876] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749502870876] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749502870876] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749502870876] add constraint [pk_tmp_affiliations_MFR6868_1749502870876] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749502870876]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749502870876] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749502870876] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749502870876] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749502870876] add constraint [pk_tmp_memberships_MFR6868_1749502870876] primary key ( operatorId ) {ts '2025-06-09 17:01:11'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749502870876] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:04:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503056102]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503056102] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503056102] add constraint [pk_tmp_memberGroups_MFR6868_1749503056102] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503056102] on [tmp_memberGroups_MFR6868_1749503056102] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503056102]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503056102] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503056102] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503056102] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503056102] add constraint [pk_tmp_affiliations_MFR6868_1749503056102] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503056102]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503056102] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503056102] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503056102] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503056102] add constraint [pk_tmp_memberships_MFR6868_1749503056102] primary key ( operatorId ) {ts '2025-06-09 17:04:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503056102] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:05:47'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503147146]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503147146] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503147146] add constraint [pk_tmp_memberGroups_MFR6868_1749503147146] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503147146] on [tmp_memberGroups_MFR6868_1749503147146] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503147146]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503147146] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503147146] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503147146] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503147146] add constraint [pk_tmp_affiliations_MFR6868_1749503147146] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503147146]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503147146] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503147146] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503147146] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503147146] add constraint [pk_tmp_memberships_MFR6868_1749503147146] primary key ( operatorId ) {ts '2025-06-09 17:05:47'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503147146] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:06:29'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503189199]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503189199] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503189199] add constraint [pk_tmp_memberGroups_MFR6868_1749503189199] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503189199] on [tmp_memberGroups_MFR6868_1749503189199] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503189199]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503189199] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503189199] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503189199] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503189199] add constraint [pk_tmp_affiliations_MFR6868_1749503189199] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503189199]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503189199] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503189199] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503189199] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503189199] add constraint [pk_tmp_memberships_MFR6868_1749503189199] primary key ( operatorId ) {ts '2025-06-09 17:06:29'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503189199] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:07:26'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503246735]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503246735] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503246735] add constraint [pk_tmp_memberGroups_MFR6868_1749503246735] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503246735] on [tmp_memberGroups_MFR6868_1749503246735] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503246735]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503246735] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503246735] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503246735] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503246735] add constraint [pk_tmp_affiliations_MFR6868_1749503246735] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503246735]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503246735] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503246735] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503246735] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503246735] add constraint [pk_tmp_memberships_MFR6868_1749503246735] primary key ( operatorId ) {ts '2025-06-09 17:07:26'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503246735] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:07:53'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503273228]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503273228] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503273228] add constraint [pk_tmp_memberGroups_MFR6868_1749503273228] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503273228] on [tmp_memberGroups_MFR6868_1749503273228] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503273228]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503273228] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503273228] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503273228] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503273228] add constraint [pk_tmp_affiliations_MFR6868_1749503273228] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503273228]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503273228] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503273228] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503273228] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503273228] add constraint [pk_tmp_memberships_MFR6868_1749503273228] primary key ( operatorId ) {ts '2025-06-09 17:07:53'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503273228] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:08:46'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503326026]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503326026] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503326026] add constraint [pk_tmp_memberGroups_MFR6868_1749503326026] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503326026] on [tmp_memberGroups_MFR6868_1749503326026] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503326026]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503326026] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503326026] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503326026] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503326026] add constraint [pk_tmp_affiliations_MFR6868_1749503326026] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503326026]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503326026] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503326026] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503326026] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503326026] add constraint [pk_tmp_memberships_MFR6868_1749503326026] primary key ( operatorId ) {ts '2025-06-09 17:08:46'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503326026] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:09:29'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503368906]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503368906] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503368906] add constraint [pk_tmp_memberGroups_MFR6868_1749503368906] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503368906] on [tmp_memberGroups_MFR6868_1749503368906] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503368906]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503368906] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503368906] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503368906] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503368906] add constraint [pk_tmp_affiliations_MFR6868_1749503368906] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503368906]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503368906] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503368906] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503368906] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503368906] add constraint [pk_tmp_memberships_MFR6868_1749503368906] primary key ( operatorId ) {ts '2025-06-09 17:09:29'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503368906] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:14:06'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503646095]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503646095] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503646095] add constraint [pk_tmp_memberGroups_MFR6868_1749503646095] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503646095] on [tmp_memberGroups_MFR6868_1749503646095] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503646095]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503646095] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503646095] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503646095] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503646095] add constraint [pk_tmp_affiliations_MFR6868_1749503646095] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503646095]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503646095] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503646095] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503646095] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503646095] add constraint [pk_tmp_memberships_MFR6868_1749503646095] primary key ( operatorId ) {ts '2025-06-09 17:14:06'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503646095] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:15:59'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503759050]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503759050] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503759050] add constraint [pk_tmp_memberGroups_MFR6868_1749503759050] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503759050] on [tmp_memberGroups_MFR6868_1749503759050] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503759050]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503759050] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503759050] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503759050] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503759050] add constraint [pk_tmp_affiliations_MFR6868_1749503759050] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503759050]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503759050] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503759050] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503759050] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503759050] add constraint [pk_tmp_memberships_MFR6868_1749503759050] primary key ( operatorId ) {ts '2025-06-09 17:15:59'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503759050] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:17:21'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503840925]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503840925] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503840925] add constraint [pk_tmp_memberGroups_MFR6868_1749503840925] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503840925] on [tmp_memberGroups_MFR6868_1749503840925] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503840925]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503840925] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503840925] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503840925] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503840925] add constraint [pk_tmp_affiliations_MFR6868_1749503840925] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503840925]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503840925] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503840925] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503840925] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503840925] add constraint [pk_tmp_memberships_MFR6868_1749503840925] primary key ( operatorId ) {ts '2025-06-09 17:17:21'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503840925] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:18:30'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749503910167]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503910167] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503910167] add constraint [pk_tmp_memberGroups_MFR6868_1749503910167] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503910167] on [tmp_memberGroups_MFR6868_1749503910167] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503910167]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503910167] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503910167] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503910167] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503910167] add constraint [pk_tmp_affiliations_MFR6868_1749503910167] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503910167]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503910167] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503910167] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503910167] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503910167] add constraint [pk_tmp_memberships_MFR6868_1749503910167] primary key ( operatorId ) {ts '2025-06-09 17:18:30'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503910167] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:19:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341440 drop table if exists [tmp_memberGroups_MFR6868_1749503979503]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749503979503] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749503979503] add constraint [pk_tmp_memberGroups_MFR6868_1749503979503] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749503979503] on [tmp_memberGroups_MFR6868_1749503979503] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749503979503]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503979503] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749503979503] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749503979503] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749503979503] add constraint [pk_tmp_affiliations_MFR6868_1749503979503] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749503979503]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749503979503] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749503979503] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749503979503] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749503979503] add constraint [pk_tmp_memberships_MFR6868_1749503979503] primary key ( operatorId ) {ts '2025-06-09 17:19:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341440 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749503979503] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:21:33'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504093268]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504093268] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504093268] add constraint [pk_tmp_memberGroups_MFR6868_1749504093268] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504093268] on [tmp_memberGroups_MFR6868_1749504093268] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504093268]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504093268] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504093268] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504093268] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504093268] add constraint [pk_tmp_affiliations_MFR6868_1749504093268] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504093268]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504093268] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504093268] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504093268] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504093268] add constraint [pk_tmp_memberships_MFR6868_1749504093268] primary key ( operatorId ) {ts '2025-06-09 17:21:33'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504093268] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:21:57'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504117763]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504117763] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504117763] add constraint [pk_tmp_memberGroups_MFR6868_1749504117763] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504117763] on [tmp_memberGroups_MFR6868_1749504117763] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504117763]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504117763] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504117763] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504117763] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504117763] add constraint [pk_tmp_affiliations_MFR6868_1749504117763] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504117763]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504117763] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504117763] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504117763] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504117763] add constraint [pk_tmp_memberships_MFR6868_1749504117763] primary key ( operatorId ) {ts '2025-06-09 17:21:58'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504117763] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:23:44'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749504224478]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504224478] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504224478] add constraint [pk_tmp_memberGroups_MFR6868_1749504224478] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504224478] on [tmp_memberGroups_MFR6868_1749504224478] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504224478]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504224478] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504224478] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504224478] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504224478] add constraint [pk_tmp_affiliations_MFR6868_1749504224478] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504224478]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504224478] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504224478] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504224478] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504224478] add constraint [pk_tmp_memberships_MFR6868_1749504224478] primary key ( operatorId ) {ts '2025-06-09 17:23:44'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504224478] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:24:49'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504289065]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504289065] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504289065] add constraint [pk_tmp_memberGroups_MFR6868_1749504289065] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504289065] on [tmp_memberGroups_MFR6868_1749504289065] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504289065]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504289065] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504289065] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504289065] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504289065] add constraint [pk_tmp_affiliations_MFR6868_1749504289065] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504289065]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504289065] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504289065] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504289065] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504289065] add constraint [pk_tmp_memberships_MFR6868_1749504289065] primary key ( operatorId ) {ts '2025-06-09 17:24:49'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504289065] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:26:15'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504375464]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504375464] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504375464] add constraint [pk_tmp_memberGroups_MFR6868_1749504375464] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504375464] on [tmp_memberGroups_MFR6868_1749504375464] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504375464]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504375464] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504375464] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504375464] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504375464] add constraint [pk_tmp_affiliations_MFR6868_1749504375464] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504375464]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504375464] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504375464] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504375464] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504375464] add constraint [pk_tmp_memberships_MFR6868_1749504375464] primary key ( operatorId ) {ts '2025-06-09 17:26:15'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504375464] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:27:15'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504435106]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504435106] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504435106] add constraint [pk_tmp_memberGroups_MFR6868_1749504435106] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504435106] on [tmp_memberGroups_MFR6868_1749504435106] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504435106]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504435106] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504435106] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504435106] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504435106] add constraint [pk_tmp_affiliations_MFR6868_1749504435106] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504435106]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504435106] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504435106] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504435106] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504435106] add constraint [pk_tmp_memberships_MFR6868_1749504435106] primary key ( operatorId ) {ts '2025-06-09 17:27:15'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504435106] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:28:33'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504513387]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504513387] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504513387] add constraint [pk_tmp_memberGroups_MFR6868_1749504513387] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504513387] on [tmp_memberGroups_MFR6868_1749504513387] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504513387]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504513387] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504513387] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504513387] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504513387] add constraint [pk_tmp_affiliations_MFR6868_1749504513387] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504513387]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504513387] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504513387] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504513387] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504513387] add constraint [pk_tmp_memberships_MFR6868_1749504513387] primary key ( operatorId ) {ts '2025-06-09 17:28:33'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504513387] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:29:26'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504566072]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504566072] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504566072] add constraint [pk_tmp_memberGroups_MFR6868_1749504566072] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504566072] on [tmp_memberGroups_MFR6868_1749504566072] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504566072]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504566072] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504566072] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504566072] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504566072] add constraint [pk_tmp_affiliations_MFR6868_1749504566072] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504566072]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504566072] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504566072] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504566072] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504566072] add constraint [pk_tmp_memberships_MFR6868_1749504566072] primary key ( operatorId ) {ts '2025-06-09 17:29:26'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504566072] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:30:06'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504605691]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504605691] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504605691] add constraint [pk_tmp_memberGroups_MFR6868_1749504605691] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504605691] on [tmp_memberGroups_MFR6868_1749504605691] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504605691]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504605691] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504605691] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504605691] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504605691] add constraint [pk_tmp_affiliations_MFR6868_1749504605691] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504605691]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504605691] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504605691] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504605691] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504605691] add constraint [pk_tmp_memberships_MFR6868_1749504605691] primary key ( operatorId ) {ts '2025-06-09 17:30:06'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504605691] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:30:50'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341440 drop table if exists [tmp_memberGroups_MFR6868_1749504650648]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504650648] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504650648] add constraint [pk_tmp_memberGroups_MFR6868_1749504650648] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504650648] on [tmp_memberGroups_MFR6868_1749504650648] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504650648]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504650648] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504650648] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504650648] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504650648] add constraint [pk_tmp_affiliations_MFR6868_1749504650648] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504650648]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504650648] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504650648] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504650648] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504650648] add constraint [pk_tmp_memberships_MFR6868_1749504650648] primary key ( operatorId ) {ts '2025-06-09 17:30:50'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341440 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504650648] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:32:08'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749504728036]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504728036] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504728036] add constraint [pk_tmp_memberGroups_MFR6868_1749504728036] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504728036] on [tmp_memberGroups_MFR6868_1749504728036] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504728036]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504728036] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504728036] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504728036] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504728036] add constraint [pk_tmp_affiliations_MFR6868_1749504728036] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504728036]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504728036] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504728036] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504728036] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504728036] add constraint [pk_tmp_memberships_MFR6868_1749504728036] primary key ( operatorId ) {ts '2025-06-09 17:32:08'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504728036] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:32:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13958607 drop table if exists [tmp_memberGroups_MFR6868_1749504759768]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504759768] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504759768] add constraint [pk_tmp_memberGroups_MFR6868_1749504759768] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504759768] on [tmp_memberGroups_MFR6868_1749504759768] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504759768]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504759768] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504759768] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504759768] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504759768] add constraint [pk_tmp_affiliations_MFR6868_1749504759768] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504759768]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504759768] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504759768] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504759768] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504759768] add constraint [pk_tmp_memberships_MFR6868_1749504759768] primary key ( operatorId ) {ts '2025-06-09 17:32:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13958607 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504759768] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:32:47'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 9790025 drop table if exists [tmp_memberGroups_MFR6868_1749504766878]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504766878] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504766878] add constraint [pk_tmp_memberGroups_MFR6868_1749504766878] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504766878] on [tmp_memberGroups_MFR6868_1749504766878] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504766878]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504766878] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504766878] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504766878] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504766878] add constraint [pk_tmp_affiliations_MFR6868_1749504766878] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504766878]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504766878] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504766878] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504766878] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504766878] add constraint [pk_tmp_memberships_MFR6868_1749504766878] primary key ( operatorId ) {ts '2025-06-09 17:32:47'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 9790025 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504766878] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:34:35'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341440 drop table if exists [tmp_memberGroups_MFR6868_1749504875031]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749504875031] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749504875031] add constraint [pk_tmp_memberGroups_MFR6868_1749504875031] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749504875031] on [tmp_memberGroups_MFR6868_1749504875031] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749504875031]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504875031] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749504875031] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749504875031] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749504875031] add constraint [pk_tmp_affiliations_MFR6868_1749504875031] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749504875031]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749504875031] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749504875031] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749504875031] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749504875031] add constraint [pk_tmp_memberships_MFR6868_1749504875031] primary key ( operatorId ) {ts '2025-06-09 17:34:35'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341440 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749504875031] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 17:46:28'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13958607 drop table if exists [tmp_memberGroups_MFR6868_1749505588002]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749505588002] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749505588002] add constraint [pk_tmp_memberGroups_MFR6868_1749505588002] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749505588002] on [tmp_memberGroups_MFR6868_1749505588002] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749505588002]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749505588002] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749505588002] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749505588002] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749505588002] add constraint [pk_tmp_affiliations_MFR6868_1749505588002] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749505588002]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749505588002] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749505588002] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749505588002] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749505588002] add constraint [pk_tmp_memberships_MFR6868_1749505588002] primary key ( operatorId ) {ts '2025-06-09 17:46:28'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13958607 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749505588002] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where [operatorId] = 13958607 ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:10:16'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749507016655]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507016655] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507016655] add constraint [pk_tmp_memberGroups_MFR6868_1749507016655] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507016655] on [tmp_memberGroups_MFR6868_1749507016655] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507016655]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507016655] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507016655] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507016655] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507016655] add constraint [pk_tmp_affiliations_MFR6868_1749507016655] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507016655]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507016655] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507016655] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507016655] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507016655] add constraint [pk_tmp_memberships_MFR6868_1749507016655] primary key ( operatorId ) {ts '2025-06-09 18:10:16'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507016655] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:11:02'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749507062164]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507062164] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507062164] add constraint [pk_tmp_memberGroups_MFR6868_1749507062164] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507062164] on [tmp_memberGroups_MFR6868_1749507062164] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507062164]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507062164] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507062164] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507062164] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507062164] add constraint [pk_tmp_affiliations_MFR6868_1749507062164] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507062164]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507062164] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507062164] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507062164] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507062164] add constraint [pk_tmp_memberships_MFR6868_1749507062164] primary key ( operatorId ) {ts '2025-06-09 18:11:02'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507062164] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:12:53'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14212108 drop table if exists [tmp_memberGroups_MFR6868_1749507173159]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507173159] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507173159] add constraint [pk_tmp_memberGroups_MFR6868_1749507173159] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507173159] on [tmp_memberGroups_MFR6868_1749507173159] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507173159]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507173159] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507173159] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507173159] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507173159] add constraint [pk_tmp_affiliations_MFR6868_1749507173159] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507173159]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507173159] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507173159] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507173159] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507173159] add constraint [pk_tmp_memberships_MFR6868_1749507173159] primary key ( operatorId ) {ts '2025-06-09 18:12:53'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14212108 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507173159] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:13:37'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341440 drop table if exists [tmp_memberGroups_MFR6868_1749507217020]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507217020] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507217020] add constraint [pk_tmp_memberGroups_MFR6868_1749507217020] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507217020] on [tmp_memberGroups_MFR6868_1749507217020] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507217020]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507217020] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507217020] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507217020] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507217020] add constraint [pk_tmp_affiliations_MFR6868_1749507217020] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507217020]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507217020] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507217020] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507217020] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507217020] add constraint [pk_tmp_memberships_MFR6868_1749507217020] primary key ( operatorId ) {ts '2025-06-09 18:13:37'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341440 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507217020] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:13:39'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14406913 drop table if exists [tmp_memberGroups_MFR6868_1749507219789]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507219789] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507219789] add constraint [pk_tmp_memberGroups_MFR6868_1749507219789] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507219789] on [tmp_memberGroups_MFR6868_1749507219789] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507219789]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507219789] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507219789] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507219789] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507219789] add constraint [pk_tmp_affiliations_MFR6868_1749507219789] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507219789]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507219789] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507219789] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507219789] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507219789] add constraint [pk_tmp_memberships_MFR6868_1749507219789] primary key ( operatorId ) {ts '2025-06-09 18:13:40'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14406913 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507219789] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:13:41'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 14341433 drop table if exists [tmp_memberGroups_MFR6868_1749507221842]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507221842] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507221842] add constraint [pk_tmp_memberGroups_MFR6868_1749507221842] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507221842] on [tmp_memberGroups_MFR6868_1749507221842] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507221842]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507221842] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507221842] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507221842] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507221842] add constraint [pk_tmp_affiliations_MFR6868_1749507221842] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507221842]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507221842] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507221842] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507221842] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507221842] add constraint [pk_tmp_memberships_MFR6868_1749507221842] primary key ( operatorId ) {ts '2025-06-09 18:13:42'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 14341433 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507221842] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:13:53'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 10134226 drop table if exists [tmp_memberGroups_MFR6868_1749507233363]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507233363] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507233363] add constraint [pk_tmp_memberGroups_MFR6868_1749507233363] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507233363] on [tmp_memberGroups_MFR6868_1749507233363] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507233363]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507233363] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507233363] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507233363] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507233363] add constraint [pk_tmp_affiliations_MFR6868_1749507233363] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507233363]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507233363] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507233363] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507233363] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507233363] add constraint [pk_tmp_memberships_MFR6868_1749507233363] primary key ( operatorId ) {ts '2025-06-09 18:13:53'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 10134226 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507233363] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:13:56'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 10132040 drop table if exists [tmp_memberGroups_MFR6868_1749507235881]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507235881] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507235881] add constraint [pk_tmp_memberGroups_MFR6868_1749507235881] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507235881] on [tmp_memberGroups_MFR6868_1749507235881] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507235881]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507235881] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507235881] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507235881] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507235881] add constraint [pk_tmp_affiliations_MFR6868_1749507235881] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507235881]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507235881] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507235881] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507235881] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507235881] add constraint [pk_tmp_memberships_MFR6868_1749507235881] primary key ( operatorId ) {ts '2025-06-09 18:13:56'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 10132040 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507235881] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:14:38'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13958607 drop table if exists [tmp_memberGroups_MFR6868_1749507278858]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507278858] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507278858] add constraint [pk_tmp_memberGroups_MFR6868_1749507278858] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507278858] on [tmp_memberGroups_MFR6868_1749507278858] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507278858]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507278858] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507278858] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507278858] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507278858] add constraint [pk_tmp_affiliations_MFR6868_1749507278858] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507278858]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507278858] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507278858] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507278858] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507278858] add constraint [pk_tmp_memberships_MFR6868_1749507278858] primary key ( operatorId ) {ts '2025-06-09 18:14:39'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13958607 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507278858] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER {ts '2025-06-09 18:14:40'} SQL declare @ownerId int = 6868 ; declare @ownerType varchar(3) = 'MFR' ; declare @operatorId int = 13170728 drop table if exists [tmp_memberGroups_MFR6868_1749507280605]; select opr.ownerId, opr.fsltablecode, cho.cho_type as oprCompanyType, opr.companyName, cho.cho_id as fsl_choId, opr.operatorId, row_number() over ( partition by opr.fsl_choId order by operatorId ) as mappingPriority into [tmp_memberGroups_MFR6868_1749507280605] from tbl_OPR_ClientOperators opr with (nolock) inner join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and cho.cho_type in ( 'C', 'G' ); alter table [tmp_memberGroups_MFR6868_1749507280605] add constraint [pk_tmp_memberGroups_MFR6868_1749507280605] primary key ( fsltablecode, ownerId, operatorId ); create index [idx_tmp_memberGroups_MFR6868_1749507280605] on [tmp_memberGroups_MFR6868_1749507280605] (fsl_choId); drop table if exists [tmp_affiliations_MFR6868_1749507280605]; with rawAffiliations as ( select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, affiliationStatus, partnerAffiliateNbr from tbl_CRM_Affiliations with (nolock) where affiliationStatus = 'A' and ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' union all select ownerId, ownerType, partnerType, partnerId, orgId, orgType, affiliationDate, 'A' affiliationStatus, null as partnerAffiliateNbr from tbl_CRM_InferredAffiliations with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerId = @operatorId and partnerType = 'OPR' ) , affiliations as ( select *, row_number() over ( partition by partnerType, partnerId, orgType, orgId order by affiliationDate ) as instanceCount from rawAffiliations ) , memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507280605] ) , affiliations2 as ( select a.*, row_number() over ( partition by a.partnerId, org.oprCompanyType order by a.affiliationDate ) as ranking, org.companyName, org.fsl_choId, org.oprCompanyType from affiliations a inner join memberGroups org on org.operatorId = a.orgId and org.ownerId = a.ownerId and org.fsltablecode = a.ownerType where instanceCount = 1 ) -- select a.partnerId operatorId, org.companyName, org.fsl_choId, a.affiliationDate, a.partnerAffiliateNbr, row_number() over ( partition by a.partnerId order by a.affiliationDate ) as ranking select a.ownerId, a.ownerType, a.partnerId as operatorId, companyName, fsl_choId, affiliationDate, partnerAffiliateNbr, ranking, case when oprCompanyType = 'G' then 'gpo' else 'cmc' end + cast( ranking as varchar ) as memberGroupN into [tmp_affiliations_MFR6868_1749507280605] from affiliations2 a; alter table [tmp_affiliations_MFR6868_1749507280605] alter column fsl_choId int not null; alter table [tmp_affiliations_MFR6868_1749507280605] add constraint [pk_tmp_affiliations_MFR6868_1749507280605] primary key ( ownerType, ownerId, operatorId, fsl_choId ); drop table if exists [tmp_memberships_MFR6868_1749507280605]; with memberGroups as ( select * from [tmp_memberGroups_MFR6868_1749507280605] with (nolock) ) , affiliations as ( select * from [tmp_affiliations_MFR6868_1749507280605] with (nolock) ) , allMemberships as ( select p.operatorId, gpo1OPR.operatorId as gpoId_1, gpo2OPR.operatorId as gpoId_2, gpo3OPR.operatorId as gpoId_3, gpo4OPR.operatorId as gpoId_4, gpo1OPR.companyName as gpoName_1, gpo2OPR.companyName as gpoName_2, gpo3OPR.companyName as gpoName_3, gpo4OPR.companyName as gpoName_4, cmc1OPR.operatorId as cmcId_1, cmc2OPR.operatorId as cmcId_2, cmc3OPR.operatorId as cmcId_3, cmc4OPR.operatorId as cmcId_4, cmc1OPR.companyName as cmcName_1, cmc2OPR.companyname as cmcName_2, cmc3OPR.companyName as cmcName_3, cmc4OPR.companyName as cmcName_4, gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 from ( select operatorId, fsl_choId, memberGroupN from affiliations where ranking <= 4 ) as gpoData pivot ( min( fsl_choId ) for memberGroupN in ( gpo1, gpo2, gpo3, gpo4, cmc1, cmc2, cmc3, cmc4 ) ) as p left outer join memberGroups gpo1OPR with (nolock) on gpo1OPR.fsl_choId = p.gpo1 and gpo1OPR.mappingPriority = 1 left outer join memberGroups gpo2OPR with (nolock) on gpo2OPR.fsl_choId = p.gpo2 and gpo2OPR.mappingPriority = 1 left outer join memberGroups gpo3OPR with (nolock) on gpo3OPR.fsl_choId = p.gpo3 and gpo3OPR.mappingPriority = 1 left outer join memberGroups gpo4OPR with (nolock) on gpo4OPR.fsl_choId = p.gpo4 and gpo4OPR.mappingPriority = 1 left outer join memberGroups cmc1OPR with (nolock) on cmc1OPR.fsl_choid = p.cmc1 and cmc1OPR.mappingPriority = 1 left outer join memberGroups cmc2OPR with (nolock) on cmc2OPR.fsl_choid = p.cmc2 and cmc2OPR.mappingPriority = 1 left outer join memberGroups cmc3OPR with (nolock) on cmc3OPR.fsl_choid = p.cmc3 and cmc3OPR.mappingPriority = 1 left outer join memberGroups cmc4OPR with (nolock) on cmc4OPR.fsl_choid = p.cmc4 and cmc4OPR.mappingPriority = 1 ) -- select * from allMemberships select a.*, ga1OPR.AffiliationDate gpoAffiliationDate_1, ga1OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_1, ga2OPR.AffiliationDate gpoAffiliationDate_2, ga2OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_2, ga3OPR.AffiliationDate gpoAffiliationDate_3, ga3OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_3, ga4OPR.AffiliationDate gpoAffiliationDate_4, ga4OPR.partnerAffiliateNbr gpoPartnerAffiliateNbr_4, ca1OPR.AffiliationDate cmcAffiliationDate_1, ca1OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_1, ca2OPR.AffiliationDate cmcAffiliationDate_2, ca2OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_2, ca3OPR.AffiliationDate cmcAffiliationDate_3, ca3OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_3, ca4OPR.AffiliationDate cmcAffiliationDate_4, ca4OPR.partnerAffiliateNbr cmcPartnerAffiliateNbr_4 into [tmp_memberships_MFR6868_1749507280605] from allMemberships a left outer join affiliations ga1OPR with (nolock) on ga1OPR.fsl_choId = a.gpo1 and ga1OPR.operatorId = a.operatorId left outer join affiliations ga2OPR with (nolock) on ga2OPR.fsl_choId = a.gpo2 and ga2OPR.operatorId = a.operatorId left outer join affiliations ga3OPR with (nolock) on ga3OPR.fsl_choId = a.gpo3 and ga3OPR.operatorId = a.operatorId left outer join affiliations ga4OPR with (nolock) on ga4OPR.fsl_choId = a.gpo4 and ga4OPR.operatorId = a.operatorId left outer join affiliations ca1OPR with (nolock) on ca1OPR.fsl_choid = a.cmc1 and ca1OPR.operatorId = a.operatorId left outer join affiliations ca2OPR with (nolock) on ca2OPR.fsl_choid = a.cmc2 and ca2OPR.operatorId = a.operatorId left outer join affiliations ca3OPR with (nolock) on ca3OPR.fsl_choid = a.cmc3 and ca3OPR.operatorId = a.operatorId left outer join affiliations ca4OPR with (nolock) on ca4OPR.fsl_choid = a.cmc4 and ca4OPR.operatorId = a.operatorId; alter table [tmp_memberships_MFR6868_1749507280605] add constraint [pk_tmp_memberships_MFR6868_1749507280605] primary key ( operatorId ) {ts '2025-06-09 18:14:40'} SQL declare @ownerId int = 6868 ; declare @ownertype varchar(3) = 'MFR' ; declare @operatorId int = 13170728 ; declare @thisYear int = Year( getDate() ); declare @lastYear int = @thisYear - 1; declare @nextYear int = @thisYear + 1; with distributionDetailsRaw as ( select dd.ownerId, dd.ownerType, dd.operatorId, dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, nullif( dd.salesRepId, 0 ) as salesRepId, row_number() over ( partition by dd.cdr_recordId order by dd.distributionDetailsId ) as cdr_recordId_rank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), /* distributionDetails as ( select dd.distributionDetailsId, dd.cdr_recordId, cdr.cdr_dstName, dd.accountNumber, dd.avgWeeklyPurchases, dd.oprPriority, sr.fspro_userId as salesRepId, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary, case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 2 else 3 end as sortRank from tbl_OPR_DistributionDetails dd with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = dd.ownerId and cdr.fsl_tablecode = dd.ownerType and cdr.cdr_recordId = dd.cdr_recordId inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where dd.ownerId = @ownerId and dd.ownerType = @ownerType and dd.operatorId = @operatorId ), */ distributionDetails as ( select dd.*, sr.firstName salesRepFirstName, sr.lastName salesRepLastName, sr.email salesRepEmail /*, cast( case when dd.cdr_recordId = nullif( opr.distrib1, 0 ) then 1 else 0 end as bit ) as isPrimary, cast( case when dd.cdr_recordId = nullif( opr.distrib2, 0 ) then 1 else 0 end as bit ) as isSecondary */ from distributionDetailsRaw dd with (nolock) /* inner join tbl_OPR_ClientOperators opr ( nolock) on opr.ownerId = dd.ownerId and opr.fsltablecode = dd.ownerType and opr.operatorId = dd.operatorId */ left outer join tbl_FSPro_members sr with (nolock) on sr.ownerId = dd.ownerId and sr.fsl_tablecode = dd.ownerType and sr.fspro_userId = dd.salesRepId where cdr_recordId_rank = 1 ), partnerURLs as ( select p.partnerId, p.partnerType, p.instagramURL, p.linkedinURL, p.tiktokURL, p.facebookURL, p.youtubeURL, p.twitterURL from ( select urlType + 'URL' as urlType, urlValue, partnerId, partnerType from tbl_CRM_PartnerURLs with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId and nullif( urlValue, '' ) is not null ) urls pivot ( min( urlValue ) for urlType in ( instagramURL, linkedinURL, tiktokURL, facebookURL, youtubeURL, twitterURL ) ) as p ), rawUnitForecast as ( select ownerId, ownerType, partnerId as operatorId, coalesce( unit_qty, 1 ) as num_units, case when cast( forecast_year as int ) = @thisYear then 'num_units_thisYr' when cast( forecast_year as int ) = @lastYear then 'num_units_lastYr' else 'num_units_nextYr' end as fieldName from tbl_MFR_TPF_PartnerUnitForecast with (nolock) where forecast_year in ( @lastYear, @thisYear, @nextYear ) and ownerId = @ownerId and ownerType = @ownerType and partnerType = 'OPR' and partnerId = @operatorId ), unitForecastPivot as ( select ownerId, ownerType, operatorId, num_units_lastYr, num_units_thisYr, num_units_nextYr from ( select * from rawUnitForecast with (nolock) ) as pivotSrc pivot ( max( num_units ) for fieldName in ( num_units_lastYr, num_units_thisYr, num_units_nextYr ) ) as pivotResult ), unitForecast as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, coalesce( uf.num_units_lastYr, coalesce( opr.num_units_lastYr, 1 ) ) num_units_lastYr, coalesce( uf.num_units_thisYr, coalesce( opr.num_units_thisYr, 1 ) ) num_units_thisYr, coalesce( uf.num_units_nextYr, coalesce( opr.num_units_nextYr, 1 ) ) num_units_nextYr from tbl_OPR_ClientOperators opr with (nolock) left outer join unitForecastPivot uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownerType = opr.fsltablecode and uf.operatorId = opr.operatorId where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ), affiliations as ( select * from [tmp_memberships_MFR6868_1749507280605] with (nolock) ), data as ( select opr.ownerId, opr.fsltablecode as ownerType, opr.operatorId, opr.oprType as priority, opr.classificationId as classificationId, opr.mfr_bsr_id, opr.profileComplete, case when opr.profileComplete = 'Y' then null else nullif( opr.profileErrors, '' ) end profileErrors, cast( floor( 10191817 * sqrt( opr.operatorId * log10( opr.operatorId ))) as bigint ) as operatorTk, opr.companyName, opr.address, opr.address2, opr.city, opr.state, opr.zipCode, opr.countryId, opr.county, opr.phone, opr.phoneExt, opr.faxNumber, nullif( opr.mfrCustNum, '' ) mfrCustNum, nullif( opr.url, '' ) url, cho.cho_id, cho.cho_name, cho.cho_type, case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 ) then 'INDEPENDENT' when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 'UNIT' when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 'CHAIN HQ' when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 'GPO' when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 'CMC' when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 'SERVICE' when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 'HOLDING' else 'UNKNOWN' end as operatorType, cast( case when cho.cho_type = 'I' or ( opr.oprCompanyType = '0' and opr.independentYN = 1 )then 1 else 0 end as bit ) isIndependent, cast( case when cho.cho_type = 'U' or ( opr.oprCompanyType = '0' and opr.independentYN = 0 ) then 1 else 0 end as bit ) isUnit, cast( case when cho.cho_type = 'O' or opr.oprCompanyType = '1' then 1 else 0 end as bit ) isChainHQ, cast( case when cho.cho_type = 'G' or opr.oprCompanyType = 'G' then 1 else 0 end as bit ) isGPO, cast( case when cho.cho_type = 'C' or opr.oprCompanyType = 'C' then 1 else 0 end as bit ) isCMC, cast( case when cho.cho_type = 'S' or opr.oprCompanyType = 'S' then 1 else 0 end as bit ) isService, cast( case when cho.cho_type = 'H' or opr.oprCompanyType = 'H' then 1 else 0 end as bit ) isHolding, cast( case when cho.cho_id is not null then 1 else 0 end as bit ) is1FSMapped, nullif( opr.primaryContactId, 0 ) as primaryContactId, nullif( opr.cuisineId, 0 ) as cuisineId, cuisine.attrDescription as cuisine, cast( case when opr.tradeEnabled = 'Y' then 1 else 0 end as bit ) as tradeEnabled, nullif( opr.tradeNbr, '' ) tradeNbr, coalesce( opr.alcoholStatus, '?' ) as alcoholStatus, opr.seasonOpenDate, opr.seasonCloseDate, opr.seasonType, opr.directCustomer, opr.directCustomerNbr, nullif( opr.comments, '' ) comments, nullif( opr.comments2, '' ) comments2, nullif( opr.distrib1, 0 ) as distrib1, dd1.salesRepId as distrib1_salesRepId, dd1.accountNumber as distrib1_accountNumber, dd1.avgWeeklyPurchases as distrib1_avgWeeklyPurchases, nullif( opr.distrib2, 0 ) as distrib2, dd2.salesRepId as distrib2_salesRepId, dd2.accountNumber as distrib2_accountNumber, dd2.avgWeeklyPurchases as distrib2_avgWeeklyPurchases, -- json_query(( select * from distributionDetails as dd with (nolock) order by dd.cdr_dstName for json auto, include_null_values )) as distributionDetails, nullif( opr.chain_distr_notes, '' ) as chain_distr_notes, opr.recordSource, opr.dateCreated, opr.lastUpdated, opr.lastInteractionDate, nullif( opr.email, '' ) email, pu.instagramURL, pu.linkedinURL, pu.tiktokURL, pu.facebookURL, pu.youtubeURL, pu.twitterURL, ter.territoryId, ter.name as territoryName, ter.territoryPath, nullif( tm.email, '' ) as territoryManagerEmail, opr.oprSegment, seg.clientSegId, seg.segmentName, seg.parentSegmentName, seg.segmentPath, opr.oprCompanyType, opr.unitsFollowSegment, opr.unitsFollowCuisine, opr.unitsFollowSeasonality, opr.unitsFollowGPO, opr.unitsFollowCMC, coalesce( popr.unitsFollowSegment, 'N' ) as segmentFollowsHQ, coalesce( popr.unitsFollowCuisine, 'N' ) as cuisineFollowsHQ, coalesce( popr.unitsFollowSeasonality, 'N' ) as seasonalityFollowsHQ, opr.oprParentCo, popr.operatorId parentOperatorId, popr.companyname parentCompanyName, opr.chainHQUnitNotes, opr.unitNbr, popr.chainHQUnitNotes as chainHQUnitNotesFromParent, nullif( opr.mdmType, '' ) mdmType, nullif( opr.mdmId, '' ) mdmId, opr.mdmDate, nullif( opr.relationshipRank, 0 ) relationshipRank, uf.num_units_lastYr as num_units_lastYr, uf.num_units_thisYr as num_units_thisYr, coalesce( uf.num_units_nextYr, uf.num_units_thisYr ) as num_units_nextYr, cast( case when exists( select * from tbl_OPR_ClientOperators u with (nolock) where u.ownerId = opr.ownerId and u.fsltablecode = opr.fsltablecode and u.oprParentCo = opr.operatorId ) then 1 else 0 end as bit ) hasUnits , a.cmcID_1, a.cmcPartnerAffiliateNbr_1, a.cmcAffiliationDate_1 , a.cmcID_2, a.cmcPartnerAffiliateNbr_2, a.cmcAffiliationDate_2 , a.cmcID_3, a.cmcPartnerAffiliateNbr_3, a.cmcAffiliationDate_3 , a.cmcID_4, a.cmcPartnerAffiliateNbr_4, a.cmcAffiliationDate_4 , a.gpoID_1, a.gpoPartnerAffiliateNbr_1, a.gpoAffiliationDate_1 , a.gpoID_2, a.gpoPartnerAffiliateNbr_2, a.gpoAffiliationDate_2 , a.gpoID_3, a.gpoPartnerAffiliateNbr_3, a.gpoAffiliationDate_3 , a.gpoID_4, a.gpoPartnerAffiliateNbr_4, a.gpoAffiliationDate_4 , bsr.email accountOwnerEmail, bsr.firstName accountOwnerFirstName, bsr.lastName accountOwnerLastName, bsr.title accountOwnerTitle , bsr.firstName + ' ' + bsr.lastName as accountOwnerFullName , opr.crmActive , cho.tastewiseId from tbl_OPR_CLientOperators opr with (nolock) left outer join tbl_OPR_ClientOperators popr with (nolock) on popr.operatorId = opr.oprParentCo and popr.ownerId = opr.ownerId and popr.fsltablecode = opr.fsltablecode inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode= opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_OPR_ClientSegments seg with (nolock) on seg.ownerId = opr.ownerId and seg.fsl_tablecode = opr.fsltablecode and seg.clientSegId = opr.oprSegment left outer join tbl_ORG_Attributes cuisine with (nolock) on cuisine.ownerId = opr.ownerId and cuisine.fsl_tablecode = opr.fsltablecode and cuisine.attrId = opr.cuisineId left outer join tbl_Fspro_members bsr with (nolock) on bsr.fspro_userId = opr.mfr_bsr_id and bsr.ownerId > 0 left outer join tbl_Fspro_members tm with (nolock) on tm.fspro_userId = ter.primaryManager and tm.ownerId > 0 left outer join tbl_CHO_Operators cho with (nolock) on cho.cho_id = opr.fsl_choId left outer join partnerURLs pu with (nolock) on 1 = 1 left outer join unitForecast uf with (nolock) on uf.ownerId = opr.ownerId and uf.ownertype = opr.fsltablecode and uf.operatorId = opr.operatorId left outer join affiliations a with (nolock) on a.operatorId = opr.operatorId left outer join distributionDetails dd1 with (nolock) on dd1.cdr_recordId = opr.distrib1 left outer join distributionDetails dd2 with (nolock) on dd2.cdr_recordId = opr.distrib2 where opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = @operatorId ) , data_mappedData as ( select * from data ) , data_preData as ( select * from data_mappedData with (nolock) where ( 1 = 1 ) ) , data_data as ( select * from data_preData ) , data_return as ( select * from data_data where 1 = 1 order by operatorId offset 0 rows fetch next 10000000 rows only ) , data_result as ( select ( select count(*) from data_data ) as totalCount, ( select * from data_return for json auto, include_null_values ) as data ) select * from data_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER