nHibernate QueryOver:计算两个日期之间的平均时间长度?

时间:2012-09-18 13:09:20

标签: sql nhibernate average queryover

我在查询如何在QueryOver中写这个问题。我有一个想法,我将加入一些合理的别名,并在我的选择中使用AVG投影,但不能想到如何使这项工作。

我们的想法是,我们查询的表中的每一行都是一个事件。会话以start事件开始,以end事件结束。我需要考虑每个会话的开始时间和结束时间,将它们加在一起然后除以会话数量(或平均值)。我在这里有SQL实现了同样的事情,并希望得到一些关于如何在查询中使用它的指导。

SELECT AVG(subQuery.SessionLengths)
FROM
(SELECT DATEDIFF (minute ,appEvent1.[Time],(SELECT TOP (1) appEvent2.[Time] as y0_
        FROM   [AppEvent] appEvent2
        WHERE  appEvent1.[Time] < appEvent2.[Time]
               and appEvent2.[Time] < (SELECT TOP (1) appEvent3.[Time] as y0_
                                     FROM   [AppEvent] appEvent3
                                     WHERE  appEvent3.[Name] = 'startEvent' 
                                            and appEvent3.[ClientId] = appEvent1.[ClientId]
                                            and appEvent3.[Time] > appEvent1.[Time]
                                     ORDER  BY appEvent3.[Time] asc)
               and appEvent2.[Name] = 'endEvent' 
               and appEvent2.[ClientId] = appEvent1.[ClientId]
        ORDER  BY appEvent2.[Time] asc) ) As SessionLengths
FROM   [AppEvent] appEvent1
       inner join [App] appAlias
         on appEvent1.[AppId] = appAlias.[Id]
WHERE  appEvent1.[Name] = 'startEvent' 
       and appEvent1.[Time] <= '09/09/2012 13:11:51 +01:00'
) subQuery

0 个答案:

没有答案