declare @year int
set @year = 2009
while(year <= 2020)
begin
SELECT A.dsformfieldvalue as Instrumento ,
AVG(DATEDIFF(day, B.DTSTARTDATE , C.DTENDDATE)) as TempoMedio ,
'Jul/2009 a Dez/2009' as Periodo
from wfflow_form_field_log A
right join wfflow_execute_task B on A.codflowexecute = B.codflowexecute
right join wfflow_execute_task C on B.codflowexecute = C.codflowexecute
where A.codflow in (326, 439)
and A.codfield = 2498
and B.codtask = 7064
and C.codtask = 7095
and CONVERT(CHAR(4), B.DTSTARTDATE, 120) = @year
and CONVERT(CHAR(4), B.DTSTARTDATE, 100) in ('Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
set @year = @year + 1
group by A.dsformfieldvalue
union all
end
基本上我想要做的是大幅减少重复代码的数量,因为我必须合并一大堆选择。我正在尝试使用 while 循环,但它不起作用。有输入吗?