select fiscalYearStartMonth, fiscalYearEndMonth from tblManufacturers mfr where mfr.mfr_id = select distinct ter.territoryId from tbl_TER_Territories ter inner join tbl_TER_TerritoryLevels lvl on lvl.levelId = ter.levelId and lvl.ownerId = ter.ownerId and lvl.fsl_tablecode = ter.fsl_tablecode where lvl.businessPlanReviewCycle <> 'N' and lvl.ownerId = and lvl.fsl_tablecode = select max( convert( datetime, tpf.forecast_year + '-' + tpf.forecast_month + '-1' )) lastActualsDate 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 = 'MFR' inner join ( select max( forecast_year ) maxYear 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 = 'MFR' where tpf.mfr_id = and cdr.cdr_territoryId in ( ) and tpf.tpartnerType = 'CDR' and tpf.forecast_status = 'A' and tpf.forecast_qty <> 0) tpf2 on tpf2.maxYear = tpf.forecast_year where tpf.mfr_id = and cdr.cdr_territoryId in ( ) and tpf.tpartnerType = 'CDR' and tpf.forecast_status = 'A' and tpf.forecast_qty <> 0 group by tpf.forecast_year qmd_fiscalYear = QueryNew( "fiscalYear,startDate,endDate,totalWeeks,actualDate,actualWeeks","integer,date,date,integer,date,integer" ); QueryAddRow( qmd_fiscalYear, 1 ); QuerySetCell( qmd_fiscalYear, "fiscalYear", _fiscalYear ); QuerySetCell( qmd_fiscalYear, "startDate", _fiscalYearStart ); QuerySetCell( qmd_fiscalyear, "endDate", _fiscalYearEnd ); QuerySetCell( qmd_fiscalyear, "totalWeeks", _totalWeeks ); if( isDate( _lastActualsDate )) { QuerySetCell( qmd_fiscalyear, "actualDate", _lastActualsDate ); } QuerySetCell( qmd_fiscalYear, "actualWeeks", _actualWeeks );