declare @index int set @index = 0 while @index <= 30 begin -- select * from #tmppinfa insert into #tmpdangyue select CONVERT(DATE,getdate()-@index,23) 日期,A.县区,AA.故障量 FROM #tmpall A LEFT JOIN ( select 县区,count(distinct(工单号)) as 故障量 from #tmpty where 1=1 AND CONVERT(VARCHAR(20),完成日期,23) = CONVERT(VARCHAR(20),'${日期+" 20:00:00"}'-@index,23) group by 县区 )AA ON A.县区=AA.县区 错误是:将 varchar 值 '${日期+" 20:00:00"}' 转换成数据类型 int 时失败。 想循环30天,天的取值是当天20:00至昨天20:00 为1天 |