计算间隔之间的数据,间隔取决于记录的年龄
我有一个名为complaint
的表格,其中我有许多列ComplaintDate
,我想显示记录数取决于间隔。
以下间隔将用于分类记录:
我尝试过这样的事情
select SUM(CASE WHEN ComplaintDate BETWEEN DATEADD (dd,-10,getdate())AND GETDATE() THEN 1 ELSE 0 END) As '0-10',
SUM(CASE WHEN ComplaintDate BETWEEN DATEADD (dd,-20,getdate()) AND DATEADD (dd,-10,getdate()) THEN 1 ELSE 0 END) AS '10-20',
SUM(CASE WHEN ComplaintDate BETWEEN DATEADD (dd,-30,getdate()) AND DATEADD (dd,-20,getdate()) THEN 1 ELSE 0 END) AS '20-30'
FROM Complaint
我也希望0-10天间隔内包含的那一天不应计入10-20或20-30天间隔,同样也应该适用于其他其他间隔。
答案 0 :(得分:0)
select
SUM(CASE WHEN ComplaintDate > DATEADD (dd,-10,getdate()) AND ComplaintDate <= GETDATE() THEN 1 ELSE 0 END) As '0-10',
SUM(CASE WHEN ComplaintDate > DATEADD (dd,-20,getdate()) AND ComplaintDate<=DATEADD (dd,-10,getdate()) THEN 1 ELSE 0 END) AS '10-20',
SUM(CASE WHEN ComplaintDate > DATEADD (dd,-30,getdate()) AND ComplaintDate <=DATEADD (dd,-20,getdate()) THEN 1 ELSE 0 END) AS '20-30'
FROM Complaint