简体   繁体   English

SQL查询按天分组并计算经过的时间(汇总行)

[英]SQL query to group by day and calculate elapsed time (aggregating rows)

I'm using SQL Server 2008 and looking how to create such a query: 我正在使用SQL Server 2008,并寻找如何创建这样的查询:

I have a table: 我有一张桌子:

Username nvarchar(50), LogDate datetime

Test1, 2012.01.01 00:00:00
Test2, 2012.01.01 00:00:02
Test1, 2012.01.01 00:00:05
Test3, 2012.01.01 00:00:06
Test1, 2012.01.02 00:01:01
Test2, 2012.01.02 00:02:50
Test1, 2012.01.02 00:01:01

Every few seconds users send updates to insert current datetime with his username in to the log table. 用户每隔几秒钟发送一次更新,以将当前日期时间及其用户名插入日志表。 I need to calculate how much hours users were "online" every day. 我需要计算每天用户有多少小时处于“在线”状态。

I assume query should sum DateDiff between two rows with the same username grouped by day. 我假设查询应将按日期分组的具有相同用户名的两行之间的DateDiff相加。

I tried using rank, but haven't got what I want. 我尝试使用等级,但还没有得到我想要的东西。

Thank you for your help in advance. 提前谢谢你的帮助。

You can extract the date part of the DateTime and group by the UserName to use DateDiff on the MIN / MAX Values: 您可以提取DateTime的日期部分,并按UserName分组,以在MIN / MAX值上使用DateDiff:

Table and Data Setup: 表和数据设置:

create table UserLog (Username nvarchar(50), LogDate DateTime);

insert into UserLog Values('Test1', '2012-01-01 00:00:00');
insert into UserLog Values('Test2', '2012-01-01 00:00:02');
insert into UserLog Values('Test1', '2012-01-01 00:00:05');
insert into UserLog Values('Test3', '2012-01-01 00:00:06');
insert into UserLog Values('Test3', '2012-01-01 00:01:26');
insert into UserLog Values('Test3', '2012-01-01 00:03:22');
insert into UserLog Values('Test3', '2012-01-01 00:05:42');
insert into UserLog Values('Test3', '2012-01-01 00:00:06');
insert into UserLog Values('Test1', '2012-01-02 00:01:01');
insert into UserLog Values('Test2', '2012-01-02 00:02:50');
insert into UserLog Values('Test1', '2012-01-02 00:01:01');

Then you can SELECT as follows: 然后您可以按以下方式进行选择:

select UserName, CAST(LogDate AS DATE) as BusinessDay,
MIN(LogDate) as FirstLogEntry, MAX(LogDate) as LastLogEntry,
DATEDIFF(second,MIN(LogDate), MAX(LogDate)) as ElapsedSeconds
FROM UserLog
GROUP BY Username, CAST(LogDate AS DATE)

This will yield the following results and you can calculate hours from the seconds. 这将产生以下结果,您可以从秒计算小时。 I showed seconds based on your sample data with test3 user expanded: 我显示了基于您的示例数据的秒数,并扩展了test3用户:

UserName BusinessDay FirstLogEntry           LastLogEntry            ElapsedSeconds
-------- ----------- ----------------------- ----------------------- --------------
Test1    2012-01-01  2012-01-01 00:00:00.000 2012-01-01 00:00:05.000 5
Test2    2012-01-01  2012-01-01 00:00:02.000 2012-01-01 00:00:02.000 0
Test3    2012-01-01  2012-01-01 00:00:06.000 2012-01-01 00:05:42.000 336
Test1    2012-01-02  2012-01-02 00:01:01.000 2012-01-02 00:01:01.000 0
Test2    2012-01-02  2012-01-02 00:02:50.000 2012-01-02 00:02:50.000 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM