您的实际代码如下:
Select M.TicketID,M.CreatedMoment, Max(L.StatusChangeMoment)AS StatusChangeTime,
Elapsed_time_in_Hours_Minutes = CONVERT(NUMERIC(18,2),DATEDIFF(minute, M.createdmoment, MAX(L.statuschangemoment))/60+(DATEDIFF(minute, M.createdmoment, MAX(L.statuschangemoment)) % 60/100.0))
From XX_MASTER_TICKETS AS M Left Join XX_DETAIL_TICKET_STATUS_LOG AS L
On M.RowID = L.TicketRowID
Where M.CreatedMoment between '08-23-2014' And '08-26-2014'
Group by M.TicketID,M.CreatedMoment
Order by M.TicketID asc
部分结果如下:
TicketID CreatedMoment StatusChangeTime Elapsed_time_in_Hours_Minutes
201408231 8/23/14 8:05 AM 8/25/14 11:47 AM 51.42
2014082310 8/23/14 8:19 AM 8/23/14 12:43 PM 4.24
20140823100 8/23/14 8:38 AM 8/24/14 11:15 AM 26.37
20140823101 8/23/14 8:38 AM 8/23/14 11:58 AM 3.2
20140823102 8/23/14 8:38 AM 8/24/14 10:33 AM 25.55
基本上,statuschangetime来自聚合函数,最后一列是第2列和第3列的差异。
我想修改查询,以便结果如下所示:
Date below24Hrs above24hours
2014-8-23 2 3
我的问题是我在运行此代码时遇到错误:
Select
[below24hrs] = COUNT (Case WHEN (CONVERT(NUMERIC(18,2),DATEDIFF(minute, TM.createdmoment, MAX(LG.statuschangemoment))/60+(DATEDIFF(minute, TM.createdmoment, MAX(LG.statuschangemoment)) % 60/100.0))) < 24 THEN 1 END)
From XX_MASTER_TICKETS AS M Left Join XX_DETAIL_TICKET_STATUS_LOG AS L
On M.RowID = L.TicketRowID
Where M.CreatedMoment between '08-23-2014' And '08-26-2014'
Group by M.TicketID,M.CreatedMoment
Order by M.TicketID asc
它表示不能使用查询中的MAX聚合函数计算。
答案 0 :(得分:-1)
您需要使用子查询并对它们求和。
select cast(sqry.CreatedMoment as date) as CreatedMoment
, sum(case when Elapsed_time_in_Hours_Minutes < 24 then 1 else 0 end) as below24Hrs
, sum(case when Elapsed_time_in_Hours_Minutes > 24 then 1 else 0 end) as above24Hrs
, sum(case when Elapsed_time_in_Hours_Minutes = 24 then 1 else 0 end) as At24Hrs
from
(
Select M.TicketID,M.CreatedMoment, Max(L.StatusChangeMoment)AS StatusChangeTime,
Elapsed_time_in_Hours_Minutes = CONVERT(NUMERIC(18,2),DATEDIFF(minute, M.createdmoment, MAX(L.statuschangemoment))/60+(DATEDIFF(minute, M.createdmoment, MAX(L.statuschangemoment)) % 60/100.0))
From XX_MASTER_TICKETS AS M Left Join XX_DETAIL_TICKET_STATUS_LOG AS L
On M.RowID = L.TicketRowID
Where M.CreatedMoment between '08-23-2014' And '08-26-2014'
Group by M.TicketID,M.CreatedMoment
Order by M.TicketID asc
) sqry
group by cast(CreatedMoment as date)