with tandl as ( select top 1 startDate, fiscalYear, periodType, periodIndex from tbl_CRM_FiscalPeriods with (nolock) where ownerId = and ownerType = and periodType = 'T' and cast( getDate() as date ) < startDate order by startDate ), n as ( select top 2 startDate, fiscalYear, periodType, periodIndex from tbl_CRM_FiscalPeriods with (nolock) where ownerId = and ownerType = and periodType = 'T' and cast( getDate() as date ) >= startDate order by startDate desc ) select * from n union all select * from tandl order by startDate desc select '#_planningTimeframe#' as planningTimeframe, coalesce( sum( targetCount ), 0 ) targetCount, coalesce( sum( targetVolume ), 0.0 ) targetVolume, coalesce( sum( actualCount ), 0 ) actualCount, coalesce( sum( actualVolume ), 0.0 ) actualVolume from tbl_BCRM_Planning with (nolock) where ownerId = and ownerType = and planningTimeframe = and territoryId in ( ) and salesRepId in ( ) and salesRepId in ( ) and ( 1 = 2 or territoryPath like ) and mfr_id in ( ) for json auto, without_array_wrapper