select r.requestId, sum( tpf.forecast_qty * case when tpf.forecast_year = '#_prorateYear#' and tpf.forecast_month = '#_prorateMonth#' then #_prorate# else 1 end ) forecast_qty, sum( tpf.forecast_amt * case when tpf.forecast_year = '#_prorateYear#' and forecast_month = '#_prorateMonth#' then #_prorate# else 1 end) forecast_amt from tbl_MFR_TPF_Detail#_tpfSuffix# tpf inner join tbl_PRD_Products p on p.prodId = tpf.prodId and p.ownerId = tpf.mfr_id and p.fsl_tablecode = left outer join tbl_TPM_Requests r on r.requestId = tpf.rebateContractId where tpf.tpartnerType = 'CDR' and ( tpf.tpartnerId = 1=2 or exists ( select * from tbl_CDR_Distributors pcdr inner join tbl_DST_Distributors dst on dst.dstParentCo = pcdr.fsl_dstId inner join tbl_CDR_Distributors cdr on cdr.fsl_dstId = dst.dstId where cdr.cdr_recordId = tpf.tpartnerId and pcdr.cdr_recordId = ) ) and tpf.mfr_id = and 'MFR' = and tpf.forecast_status in ( 'A', 'C' ) and ( 1 = 2 or ( tpf.forecast_year = and tpf.forecast_month = ) ) and tpf.skuId in ( ) and 1 = case when tpf.forecast_status = 'A' then 1 else case when r.requestId = tpf.rebateContractId then 1 else 0 end end group by r.requestId select cdr_dstName from tbl_CDR_Distributors where cdr_recordId = where cdr_recordId = select max( requestId ) requestId, sum( forecast_amt ) forecast_amt, sum( forecast_qty ) forecast_qty, salesType, partnerName, assignedContractNbr, '' contractDescription, partnerId, partnerType, '' distributorName from qmd_data where deleteRow = 'N' group by salesType, partnerName, assignedContractNbr, partnerId, partnerType order by forecast_amt desc select * from qmd_data where deleteRow = 'N' order by forecast_amt desc