select distinct forecast_year, forecast_month from tbl_CDR_InvoiceHistory with (nolock) where ownerId = and ownerType = and forecast_year >= 2019 and forecast_month = 12---> order by forecast_year, forecast_month select cdrRDC.cdr_recordId from tbl_CDR_Distributors cdrRDC with (nolock) inner join tbl_DST_Distributors dstRDC with (nolock) on dstRDC.dstId = cdrRDC.fsl_dstId and dstRDC.dstCompanyType = 'RDC' where cdrRDC.ownerId = and cdrRDC.fsl_tablecode = select cdr.cdr_recordId, cdr.rdcVolumeShare, cdr.cdr_territoryId, cdr.cdr_dstCode, cdr.cdr_dstName from tbl_CDR_Distributors cdr with (nolock) inner join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId inner join tbl_CDR_Distributors cdrRDC with (nolock) on cdrRDC.fsl_dstId = dst.dstRDCId and cdrRDC.ownerId = cdr.ownerId and cdrRDC.fsl_tablecode = cdr.fsl_tablecode inner join tbl_TER_Territories ter with (nolock) on ter.territoryId = cdr.cdr_territoryId and ter.fsl_tablecode = cdr.fsl_tablecode and ter.ownerId = cdr.ownerId where cdrRDC.ownerId = and cdrRDC.fsl_tablecode = and cdrRDC.cdr_recordId = and cdr.rdcVolumeShare > 0.0 select ih.cdr_recordId, ih.skuId, 0.0 redistCases, 0.0 redistWeight, 0.0 redistDollars, 0.0 redistAlt, sum( caseAmt ) caseAmt, sum( weightAmt ) weightAmt, sum( dollarAmt ) dollarAmt, sum( altAmt ) altAmt from tbl_CDR_InvoiceHistory ih inner join tbl_PRD_Skus s on s.skuId = ih.skuId and s.ownerId = ih.ownerId and s.fsl_tablecode = ih.ownerType where ih.ownerId = and ih.ownerType = and ih.forecast_year = and ih.forecast_month = and ih.cdr_recordId not in ( ) and coalesce( ih.reverseInvoice, 'N' ) <>'Y' group by ih.cdr_recordId, ih.skuId select s.skuId, s.sku, p.prodId, case when s.bgtCatOverride = 'Y' and s.bgtCatId is not null then s.bgtCatId else p.bgtCatId end bgtCatId, coalesce( s.equivUnitsPerCase, 1 ) equivUnitsPerCase from tbl_PRD_Skus s inner join tbl_PRD_Products p on p.prodId = s.prodId and p.ownerId = s.ownerId and p.fsl_tablecode = p.fsl_tablecode where s.skuId = and s.ownerId = and s.fsl_tablecode = select ter.territoryId, coalesce( cdr.bgtCatId, coalesce( pcdr.bgtCatId, 0 )) bgtCatId from tbl_CDR_Distributors cdr inner join tbl_TER_TErritories ter on ter.territoryId = cdr.cdr_territoryId and ter.ownerId = cdr.ownerId and ter.fsl_tablecode = cdr.fsl_tablecode left outer join tbl_DST_Distributors dst on dst.dstId = cdr.fsl_dstId left outer join tbl_CDR_Distributors pcdr on pcdr.fsl_dstId = dst.dstParentCo and pcdr.ownerId = cdr.ownerId and pcdr.fsl_tablecode = cdr.fsl_tablecode where cdr.ownerId = and cdr.fsl_tablecode = and cdr.cdr_recordId = select forecast_qty,forecast_amt,forecast_alt,prodId,bgtCategoryId,productItemCode,territoryId,lastUpdateSource,lastUpdate from tbl_MFR_TPF_Detail#_tableSuffix# with (nolock) where mfr_id = and tpartnerType = 'CDR' and tpartnerId = and skuId = and productLevel = 'SKU' and forecast_status = 'A' and forecast_year = and forecast_month = update tbl_MFR_TPF_Detail#_tableSuffix# set forecast_qty = , forecast_amt = , forecast_alt = , prodId = , bgtCategoryId = , lastUpdateSource = , lastUpdate = , productItemCode = , territoryId = where mfr_id = and tpartnerType = 'CDR' and tpartnerId = and skuId = and productLevel = 'SKU' and forecast_status = 'A' and forecast_year = and forecast_month = insert into tbl_MFR_TPF_Detail#_tableSuffix# ( mfr_id, tpartnerType, tpartnerId, skuId, productLevel, forecast_status, forecast_year, forecast_month, forecast_qty, forecast_amt, forecast_alt, prodId, bgtCategoryId, lastUpdateSource, lastUpdate, productItemCode, territoryId ) values ( , 'CDR', , , 'SKU', 'A', , , , , , , , , , , ) select count(*) orphanCount from tbl_MFR_TPF_Detail#_tableSuffix# tpf with (nolock) where not exists ( select * from tbl_CDR_InvoiceHistory ih with (nolock) where ih.ownerType = 'MFR' and ih.ownerId = tpf.mfr_Id and ih.cdr_recordId = tpf.tpartnerId and ih.skuId = tpf.skuId ) and tpf.mfr_id = and 'MFR' = and tpf.forecast_status = 'A' and tpf.tpartnerType = 'CDR' delete tpf from tbl_MFR_TPF_Detail#_tableSuffix# tpf where not exists ( select * from tbl_CDR_InvoiceHistory ih with (nolock) where ih.ownerType = 'MFR' and ih.ownerId = tpf.mfr_Id and ih.cdr_recordId = tpf.tpartnerId and ih.skuId = tpf.skuId ) and tpf.mfr_id = and 'MFR' = and tpf.forecast_status = 'A' and tpf.tpartnerType = 'CDR'