select a.field0047,
case when a.处理时长<30 then '<30天'
when a.处理时长>30 AND a.处理时长<=60 then '30~60天'
when a.处理时长>60 AND a.处理时长<=90 then '60~90天'
when a.处理时长>90 AND a.处理时长<=180 then '90~180天'
when a.处理时长>180 then '>180天'
else null end as 时长段,
count(*) as num
from (
SELECT
field0047,
DATEDIFF(day, field0012, GETDATE()) AS 处理时长
FROM formmain_1141
WHERE finishedflag = '0'
) a
group by a.field0047,
case when a.处理时长<30 then '<30天'
when a.处理时长>30 AND a.处理时长<=60 then '30~60天'
when a.处理时长>60 AND a.处理时长<=90 then '60~90天'
when a.处理时长>90 AND a.处理时长<=180 then '90~180天'
when a.处理时长>180 then '>180天'
else null end