不同日期范围的单独结果

时间:2016-09-16 15:39:01

标签: sql-server

我一直在尝试根据不同的日期范围生成报告。但我写的查询是将所有结果合二为一。期望获得4个日期范围的4个结果。

PS:我是初学者。任何帮助将不胜感激。

select
    sum(case when IsBlank = 1 then 1 else 0 end) as BlankChats,
    sum(case when LeadID > 0 then 1 else 0 end) as ConvertedChats,
    sum(case when PickedUpBy != AssignedToAgent and AssignedToAgent > 0 and PickedUpBy > 0 then 1 else 0 end) as MissedChats,
    sum(case when AssignedToAgent > 0 then 1 else 0 end) as AssignedChats,
    sum(case when LeadType=1 then 1 else 0 end) as SalesLeads
from
    chats with (nolock)
    left join leads on leads.id=chats.leadid
where
    (chats.createdon >= DATEADD(d,0,DATEDIFF(d,0,GETDATE())) and chats.createdOn <= DATEADD(MI, -1*30, GETDATE()))
OR
    (chats.CreatedOn >= DATEADD(d, -1, DATEDIFF(d, 0, GETDATE())) and chats.createdOn <= DATEADD(MI, -1*30, DATEADD(d, -1, GETDATE())))
OR
    (chats.CreatedOn >= DATEADD(d, -7, DATEDIFF(d, 0, GETDATE())) and chats.createdOn <= DATEADD(MI, -1*30, DATEADD(d, -7, GETDATE())))
OR
    (chats.CreatedOn >= DATEADD(d, -8, DATEDIFF(d, 0, GETDATE())) and chats.createdOn <= DATEADD(MI, -1*30, DATEADD(d, -8, GETDATE())))

1 个答案:

答案 0 :(得分:0)

如果您想要查询4行,则需要将日期范围作为case语句添加到select,然后按case case语句进行分组。然后,您应该根据案例创建的每个值获得一行 - 在这种情况下,应该是您希望统计数据分割的日期范围。