我有一个充满用户会话的表,我希望按会话类型(MessageBodyClassName
)获得平均会话时间。我已经接近了,但我有一个小问题:没有记录的消息类型显示1
MessageCount
。
这是我当前的查询:
SELECT mh.MessageBodyClassName as MessageType,
count(*) as MessageCount,
CAST(AVG(ResponseTime)AS DECIMAL(5,2)) as AvgResponse
FROM
(
SELECT DISTINCT(MessageBodyClassName) FROM ens.messageheader
) mh FULL OUTER JOIN
(
SELECT
li.SessionId,
li.MessageBodyClassName,
datediff(s,min(li.TimeCreated),max(lo.TimeCreated)) as ResponseTime
FROM
(Select SessionId, TimeCreated, MessageBodyClassName FROM ens.messageheader) li
LEFT JOIN
(Select SessionId, TimeCreated, MessageBodyClassName FROM ens.messageheader) lo
ON li.SessionId = lo.SessionId
WHERE li.TimeCreated > DATEADD(hh, -1, GETDATE())
GROUP BY li.SessionId
) l ON mh.MessageBodyClassName = l.MessageBodyClassName
GROUP BY mh.MessageBodyClassName
以下是结果示例:
MessageType MessageCount AvgResponse -------------------------------------- ------------ ----------- HS.MESSAGE.ADDPATIENTREQUEST 1 (null) HS.MESSAGE.ADDUPDATEHUBREQUEST 525 0.44 HS.MESSAGE.GATEWAYREGISTRATIONREQUEST 1 (null) HS.MESSAGE.IDUPDATENOTIFICATIONREQUEST 10 0.51
上面的1
都应该是0
。
我尝试将count(*)
更改为count(l.*)
,但这不起作用。如何在没有黑客攻击的情况下让它返回零(即count(*) -1 as MessageCount
)?