update ss set ss.fseTerritoryId = ter.territoryId, ss.fseBgtCatId = COALESCE(bgtCatLookup.bgtCatId,a.objectId), ss.tpfMonth = right( '00' + convert( varchar, salesMonth ), 2 ), ss.tpfYear = convert( varchar, salesYear ), ss.fseInclude = case when ter.territoryId is null or ( bgtCatLookup.bgtCatId is null and a.objectId is null ) then 0 else 1 end from tbl_IMPORT_TerritorySalesDataSummary ss left outer join tbl_IMPORT_Aliases a on a.alias = ss.bgtCategory and a.objectType = 'BGTCategories' and a.ownerId = and a.ownerType = left outer join tbl_TER_Territories ter on ter.OwnerID = ss.ownerId and ter.FSL_TableCode = ss.ownerType and ter.InternalSalesID = ss.territoryId left outer join ( select a.attrID bgtCatId, a.attrDescription bgtCategory from tbl_CMM_FilterTypes ft inner join tbl_CMM_Filters f on f.filterTypeId = ft.filterTypeId inner join tbl_ORG_AttributeLinks oal on oal.mstr_attrID = f.filterId and oal.mstr_attrTypeID = 28 and oal.child_attrTypeID = 29 inner join tbl_ORG_Attributes a on a.attrID = oal.child_attrId and a.attrTypeID = oal.child_attrTypeId and a.OwnerID = f.ownerId and a.FSL_TableCode = f.fsl_tablecode where ft.filterTypeName = 'BudgetCategories' and a.ownerId = and a.fsl_tablecode = ) bgtCatLookup on bgtCatLookup.bgtCategory = ss.bgtCategory where ss.eai_batchId = select distinct salesYear, salesMonth from tbl_IMPORT_TerritorySalesDataSummary where fseInclude = 1 and eai_batchId = order by salesYear, salesMonth update tbl_IMPORT_TerritorySalesDataSummary set fiscalYear = , weeksElapsed = where eai_batchId = and salesYear = and salesMonth = and fseInclude = 1 select fiscalYear, max( weeksElapsed ) weeksElapsed from tbl_IMPORT_TerritorySalesDataSummary where eai_batchId = and fseInclude = 1 group by fiscalYear order by fiscalYear select distinct fseTerritoryId from tbl_IMPORT_TerritorySalesDataSummary where eai_batchId = and fseInclude = 1 and fiscalYear = select distinct summaryType, fseBgtCatId, tpfMonth, tpfYear from tbl_IMPORT_TerritorySalesDataSummary where eai_batchId = and fseInclude = 1 and fiscalYear = and fseTerritoryId = delete tbl_MFR_TPF_Detail#_tpfSuffix# where mfr_id = and forecast_year = and forecast_month = and tpartnerType = 'TER' and tpartnerId = and bgtCategoryId = and productLevel = 'UOM' and productItemCode = and forecast_status = insert into tbl_MFR_TPF_Detail#_tpfSuffix# ( MFR_ID, tpartnerID, tpartnerType, productItemCode, productLevel, forecast_year, forecast_month, forecast_qty, forecast_status, forecast_amt, bgtCategoryId ) select ownerId mfr_id, fseTerritoryId tpartnerId, 'TER' tpartnerType, 'BGT_' + CONVERT( varchar, fseBgtCatId ) productItemCode, 'UOM' productLevel, tpfYear forecast_year, tpfMonth forecast_month, sum( coalesce( qty, 0.0 )) forecast_qty, summaryType forecast_status, sum( coalesce( salesAmt, 0.0 )) forecast_amt, fseBgtCatId bgtCategoryId from tbl_IMPORT_TerritorySalesDataSummary where fseTerritoryId = and fseBgtCatId = and tpfMonth = and tpfYear = and summaryType = and eai_batchId = and fseInclude = 1 group by ownerId, fseTerritoryId, fseBgtCatId, tpfYear, tpfMonth, summaryType update tbl_CRM_FiscalCalendar set weeksElapsed = where ownerId = and ownerType = and fiscalYear = and coalesce( weeksElapsed, 0 ) < #_salesDataSharingHTML#