我也在尝试按日期对它进行分组。我怎样才能做到这一点?我希望一组 0-30 岁,另一组 31-60 岁,最后一组 60 岁以上。我正在使用 SSRS 制作图表,并且需要水平轴为 0-31、31-60 和 60+。我尝试将“THEN P_NUMBER END) as Active_age”添加到 datediff 的末尾,但这不起作用。
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(4)
AND datediff(day,Open_Time,GETDATE()) between 0 and 30
group by Assignment, Severity
UNION
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(5)
AND datediff(day,Open_Time,GETDATE()) between 0 and 30 THEN P_NUMBER END) as Active_age
group by Assignment, Severity
UNION
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(4)
AND datediff(day,Open_Time,GETDATE()) between 30.01 and 60
group by Assignment, Severity
UNION
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(5)
AND datediff(day,Open_Time,GETDATE()) between 30.01 and 60
group by Assignment, Severity
UNION
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(4)
AND datediff(day,Open_Time,GETDATE()) > 60
group by Assignment, Severity
UNION
SELECT COUNT(*) Assignment, Severity
FROM dashboard.dbo.SmTickets
WHERE Assignment IN('Crosby')
AND Severity IN(5)
AND datediff(day,Open_Time,GETDATE()) > 60
group by Assignment, Severity