这是答案,如果有人可以复习/增强它,请告诉我…
declare @startDate datetime, @endDate datetime
set @startDate = '01/Nov/2009'
set @endDate = '04/Apr/2011'
declare @kpi table(kpiid int, interval int)
insert into @kpi
select 1, 3
union select 2, 5
--union select 3, 9
--union select 4, 12
;with mycte(i, d, interval, p, kpiid) as
(
select i = 1, d=@startDate, Interval, 0, kpiid from @kpi
union all
select
i = i + 1,
dateAdd(mm, i, @StartDate),
interval,
case when (datediff(mm, @StartDate, m.d)) % interval = (interval - 1) then 1 else 0 end,
m.kpiid
from
mycte m where m.d < @EndDate
)
select * from mycte where p = 1 and d <=@EndDate order by kpiid, d