无法找出准确的查询来获取我的报告的时间戳

Unable to figure out accurate query to get timestamps for my report

我正在尝试根据登录 session 的用户数据编写报告。

我需要能够获得从第一个人加入会议到最后一个人离开的完整 session 时间。

我希望它看起来像下面这样:

这是我的查询:

select vl.originalChatSessionID, 
       CONVERT(DATE, min(vl.ReceivedDateTime)) as VideoDate, 
       --CONVERT(TIME, min(vl.ReceivedDateTime)) as StartTime,
       min(vl.ReceivedDateTime) as StartTime2,
       --CONVERT(TIME, max(vl.ReceivedDateTime)) as EndTime,
       max(vl.ReceivedDateTime) as EndTime2,
       DATEDIFF(MINUTE, min(vl.ReceivedDateTime), max(vl.ReceivedDateTime)) as SessionLength
from iclickphrDxvideolog vl
    --inner join iclickphrDxVideoHistory vh
    --  on vl.originalChatSessionID = vh.meetingid
    --  and vl.applicationUserID = vh.applicationUserID
where originalChatSessionID = @MeetingSessionID
    --and (vl.logType = 'Initialize-Load Video chat Window' or vl.logType = 'Video Chat-End Chat')
group by originalChatSessionID

问题是我正在抓取 logType 列中第一个记录的元素和最后一个记录的元素,我知道这一点。如果我取消注释 where 子句中它说的部分; --and (vl.logType = 'Initialize-Load Video chat Window' or vl.logType = 'Video Chat-End Chat'), 那么我对 session 的开始时间没有问题... .但是 session.

的结束时间有一个大问题

下面是原始数据的图片:

我假设 originalChatSessionID 定义了一个唯一的会话。如果不是,那么您将不得不更改 'PARTITION BY' 子句以镜像使其唯一的一个或多个列。

这还假设 ReceivedDateTimedatetime 数据类型

SELECT DISTINCT 
       vl.originalChatSessionID, 
       VideoDate = MIN(vl.ReceivedDateTime) OVER(PARTITION BY originalChatSessionID), 
       StartTime = MIN(ISNULL(sc.StartChat, vl.ReceivedDateTime)) OVER(PARTITION BY originalChatSessionID),
       EndTime = MAX(ISNULL(ec.EndChat, vl.ReceivedDateTime)) OVER(PARTITION BY originalChatSessionID),
       SessionLength = DATEDIFF(
                minute,
                MIN(ISNULL(sc.StartChat, vl.ReceivedDateTime)) OVER(PARTITION BY originalChatSessionID), 
                MAX(ISNULL(ec.EndChat, vl.ReceivedDateTime)) OVER(PARTITION BY originalChatSessionID)
                )
    FROM iclickphrDxvideolog vl
        LEFT JOIN (
                    SELECT originalChatSessionID, StartChat = MIN(ReceivedDateTime) 
                        WHERE logType = 'Initialize-Load Video chat Window'
                        GROUP BY originalChatSessionID
                    ) sc ON vl.originalChatSessionID = sc.originalChatSessionID
        LEFT JOIN (
                    SELECT originalChatSessionID, EndChat = MAX(ReceivedDateTime) 
                        WHERE logType = 'Video Chat-End Chat'
                        GROUP BY originalChatSessionID
                    ) ec ON vl.originalChatSessionID = ec.originalChatSessionID

这是未经测试的,因为我没有时间重新创建您的数据集。如果您需要更多帮助,我建议您 post 一个脚本来重新创建您的示例数据,以便人们可以使用它来进行测试。

上面使用了两个子查询,一个获取Initialize-Load Video chat Window的第一个实例,一个获取Video Chat-End Chat的最后一个实例。我已经 LEFT JOIN 编辑了这些,所以他们将 return NULL 值是没有找到。在查询的主要部分,我使用 ISNULL() 来测试是否未找到开始记录然后使用会话的最早记录,如果未找到结束记录则使用会话的最后记录.

Note that there is no grouping but I have used DISTINCT to get a similar result. This SQL statement does not have a WHERE clause so you could create a view from it then simply use that view with a WHERE clause for your report.