经过一些搜索,我能够使用此查询获得结果。
SELECT
DATEDIFF(HOUR, LOGINTIME, LOGOUTTIME)
FROM
[SESSION]
GROUP BY
DATEPART(YEAR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(MONTH, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(DAY, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(HOUR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
(DATEPART(MINUTE, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)) / 10),
LoginTime, LogoutTime
这是一个包含用户登录和注销时间会话的表。我想要的是计算有多少用户有一个小时两个或更多。我想得到点数。
此查询的示例结果是。
0
0
0
1
1
1
2
4
17
20
答案 0 :(得分:1)
尝试将其添加到子查询中并对结果进行分组和过滤,如下所示:
SELECT Hours, COUNT(T1.Hours) HourCount
FROM
(SELECT DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME) as Hours
FROM [SESSION]
GROUP BY
DATEPART(YEAR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(MONTH, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(DAY, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(HOUR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
(DATEPART(MINUTE, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)) / 10),
LoginTime,LogoutTime) T1
GROUP BY T1.Hours
HAVING T1.Hours > 1
这将按Hours
分割和计数,大于1.
答案 1 :(得分:0)
试试这个:
SELECT MAX(Total) AS Total
FROM
(SELECT
ROW_MUMBER() OVER (ORDER BY DATEDIFF(HOUR, LOGINTIME, LOGOUTTIME)) AS Total
FROM
[SESSION]
WHERE
DATEDIFF(HOUR, LOGINTIME, LOGOUTTIME) > 1
GROUP BY
DATEPART(YEAR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(MONTH, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(DAY, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
DATEPART(HOUR, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)),
(DATEPART(MINUTE, DATEDIFF(HOUR,LOGINTIME,LOGOUTTIME)) / 10),
LoginTime, LogoutTime
) AS T
答案 2 :(得分:0)
在添加" SessionDuration"后,我必须使用此查询。作为计算列,使用" LogoutTime-LoginTime"。
SELECT COUNT(DATEPART(HOUR,SessionDuration)) AS [Count],
DATEPART(HOUR,SessionDuration) AS SessionDuration
FROM [SESSION]
GROUP BY DATEPART(HOUR,SessionDuration)