select t.internalSalesId, count(*) dupCount from tbl_TER_Territories t where t.ownerId = and t.fsl_tablecode = and t.internalSalesId is not null and t.internalSalesId <> '' group by t.internalSalesId having count(*) > 1 update i set i.fseTerritoryId = t.territoryId from tbl_IMPORT_TerritoryBudgets i inner join tbl_TER_Territories t on t.ownerId = i.ownerId and t.fsl_tablecode = i.ownerType and t.internalSalesId = i.internalSalesId where i.eai_batchId = and i.ownerId = and i.ownerType = select min( budgetYear ) minYear, max( budgetYear ) maxYear, min( convert( datetime, convert( varchar, budgetYear ) + '-' + convert( varchar, budgetMonth ) + '-1' )) startDate, max( dateadd( day, -1, dateadd( month, 1, convert( datetime, convert( varchar, budgetYear ) + '-' + convert( varchar, budgetMonth ) + '-1' )))) endDate from tbl_IMPORT_TerritoryBudgets where ownerId = and ownerType = and eai_batchId = update i set i.lastYearActualQty = a.actualQty, i.lastYearActualAmt = a.actualAmt from tbl_IMPORT_TerritoryBudgets i inner join ( select ter.territoryId, tpf.productItemCode, tpf.forecast_year, tpf.forecast_month, sum( forecast_qty ) actualQty, sum( forecast_amt ) actualAmt from tbl_MFR_TPF_Detail#_tpfSuffix# tpf inner join tbl_CDR_Distributors cdr on cdr.cdr_recordId = tpf.tpartnerId and cdr.ownerId = tpf.mfr_id and cdr.fsl_tablecode = inner join tbl_TER_Territories ter on ter.territoryId = cdr.cdr_territoryId and ter.ownerId = cdr.ownerId and ter.fsl_tablecode = cdr.fsl_tablecode inner join tbl_PRD_Skus s on s.sku = tpf.productItemCode and s.ownerId = tpf.mfr_Id and s.fsl_tablecode = where tpf.mfr_id = and tpf.forecast_status = 'A' and tpf.tpartnerType = 'CDR' and tpf.productLevel = 'SKU' and convert( int, tpf.forecast_year ) < group by ter.territoryId, tpf.productItemCode, tpf.forecast_year, tpf.forecast_month ) a on a.territoryId = i.fseTerritoryId and a.productItemCode = i.sku and a.forecast_month = right( '00' + convert( varchar, i.budgetMonth ), 2 ) and a.forecast_year = convert( varchar, i.budgetYear - 1 ) where i.eai_batchId = and i.ownerId = and i.ownerType = delete from tbl_MFR_TPF_Detail#_tpfSuffix# where mfr_id = and tpartnerType = 'TER' and forecast_status = 'Q' and ( or ( forecast_month = and forecast_year = ) ) insert into tbl_MFR_TPF_Detail#_tpfSuffix# ( mfr_id, tpartnerType, tPartnerId, forecast_status, forecast_year, forecast_month, forecast_qty, forecast_amt, productLevel, productItemCode, skuId, prodId, mktCategoryId, mktSubCategoryId, sourceOfUpdate ) select ownerId mfr_id, 'TER' tpartnerType, fseTerritoryId tPartnerId, 'Q' forecast_status, budgetYear, right( '00' + convert( varchar, budgetMonth ), 2 ), budgetQty forecast_qty, budgetAmt forecast_amt, 'SKU' productLevel, sku productItemCode, fseSkuId skuId, fseProdId prodId, fseMktCategoryId mktCategoryId, fseMktSubCategoryId mktSubCategoryId, 'EAI' sourceOfUpdate from tbl_IMPORT_TerritoryBudgets where eai_batchId = and ownerId = and ownerType = and fseTerritoryId is not null update tpf set tpf.bgtCategoryId = sku.bgtCatId from tbl_MFR_TPF_Detail#_tpfSuffix# tpf inner join tbl_PRD_SKUs sku on sku.skuId = tpf.skuId where tpartnerType = 'TER' and forecast_status = 'Q' and MFR_ID = and ( convert( int, forecast_year ) between and ) select bp.ownerId, bp.ownerType, bp.territoryId, bp.businessPlanId from tbl_TER_BusinessPlans bp inner join tbl_TER_BusinessPlanGoals bpg on bpg.businessPlanId = bp.businessPlanId and bpg.goalType = 'G' and bpg.ownerId = bp.ownerId and bpg.ownerType = bp.ownerType where bp.ownerId = and bp.ownerType = and ( ( bp.startDate <= and bp.endDate >= ) or ( bp.startDate <= and bp.endDate >= ) )

Territory Budgets Processed