declare @index int
set @index = 11
while @index >= 0
begin
insert into #tmpygzl
select cast(month(DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month,-@index,getdate())), 0)) as varchar(50)) +'月' as 月份,'全市' 县区,count(distinct(工单编号)) as "故障量",(13-@index) as 序号
from #tmpgj
where 1=1
and 字段 >=DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month,-@index,getdate())), 0)
and 字段< DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month,-@index+1,getdate())), 0)
set @index = @index -1
end
这个是月趋势图的代码