日期:2014-05-18  浏览次数:20333 次

求sql ???
以下是测试数据,求同时在线的最大用户数,也就是登陆时间和退出时间有交集的记录,sql脚本该如何写,求指点。

用户ID 登陆时间 退出时间
UserID CreatedServer LoginClient LogoutClient
23 2011-03-16 19:29:31.700 2011-03-16 19:29:33.980 2011-03-16 19:41:27.000
24 2011-03-16 19:30:37.103 2011-03-16 19:30:39.347 2011-03-16 20:39:53.000
23 2011-03-18 10:52:05.667 2011-03-18 10:52:09.227 2011-03-18 11:05:59.000
23 2011-03-18 11:28:01.030 2011-03-18 11:28:04.167 2011-03-18 13:57:02.000
24 2011-03-19 12:56:50.193 2011-03-19 12:56:54.273 2011-03-19 12:57:44.000
23 2011-03-21 09:49:51.297 2011-03-21 09:49:54.340 2011-03-21 09:53:51.000
24 2011-03-21 09:50:18.717 2011-03-21 09:50:21.727 2011-03-21 10:23:47.000
26 2011-03-21 09:52:48.660 2011-03-21 09:52:49.007 2011-03-21 13:21:53.000
27 2011-03-21 14:10:19.233 2011-03-21 14:10:22.303 2011-03-21 23:31:29.000
21 2011-03-21 15:12:03.433 2011-03-21 15:12:06.407 2011-03-21 16:59:33.000
23 2011-03-21 16:54:51.227 2011-03-21 16:54:48.370 2011-03-21 17:12:59.000

------解决方案--------------------
以十五分钟为单位统计:
SQL code
create table tb(UserID int,CreatedServer datetime,LoginClient datetime,LogoutClient datetime)
insert into tb select 23,'2011-03-16 19:29:31.700','2011-03-16 19:29:33.980','2011-03-16 19:41:27.000'
insert into tb select 24,'2011-03-16 19:30:37.103','2011-03-16 19:30:39.347','2011-03-16 20:39:53.000'
insert into tb select 23,'2011-03-18 10:52:05.667','2011-03-18 10:52:09.227','2011-03-18 11:05:59.000'
insert into tb select 23,'2011-03-18 11:28:01.030','2011-03-18 11:28:04.167','2011-03-18 13:57:02.000'
insert into tb select 24,'2011-03-19 12:56:50.193','2011-03-19 12:56:54.273','2011-03-19 12:57:44.000'
insert into tb select 23,'2011-03-21 09:49:51.297','2011-03-21 09:49:54.340','2011-03-21 09:53:51.000'
insert into tb select 24,'2011-03-21 09:50:18.717','2011-03-21 09:50:21.727','2011-03-21 10:23:47.000'
insert into tb select 26,'2011-03-21 09:52:48.660','2011-03-21 09:52:49.007','2011-03-21 13:21:53.000'
insert into tb select 27,'2011-03-21 14:10:19.233','2011-03-21 14:10:22.303','2011-03-21 23:31:29.000'
insert into tb select 21,'2011-03-21 15:12:03.433','2011-03-21 15:12:06.407','2011-03-21 16:59:33.000'
insert into tb select 23,'2011-03-21 16:54:51.227','2011-03-21 16:54:48.370','2011-03-21 17:12:59.000'
go
select top 1 dt,COUNT(*)ct from(
select dateadd(mi,a.number*15,(select MIN(LoginClient) from tb))dt,1 as flg
from master..spt_values a inner join tb b on
dateadd(mi,a.number*15,(select MIN(loginclient) from tb)) between b.LoginClient and b.LogoutClient
where a.type='p' and dateadd(mi,a.number*15,(select MIN(loginclient) from tb))<=(select MAX(logoutclient) from tb)
)t group by dt
order by 2 desc
/*
dt                      ct
----------------------- -----------
2011-03-21 09:59:33.980 2

(1 行受影响)

*/
go
drop table tb

------解决方案--------------------
SQL code
create table tb(UserID int,CreatedServer datetime,LoginClient datetime,LogoutClient datetime)
insert into tb select 23,'2011-03-16 19:29:31.700','2011-03-16 19:29:33.980','2011-03-16 19:41:27.000'
insert into tb select 24,'2011-03-16 19:30:37.103','2011-03-16 19:30:39.347','2011-03-16 20:39:53.000'
insert into tb select 23,'2011-03-18 10:52:05.667','2011-03-18 10:52:09.227','2011-03-18 11:05:59.000'
insert into tb select 23,'2011-03-18 11:28:01.030','2011-03-18 11:28:04.167','2011-03-18 13:57:02.000'
insert into tb select 24,'2011-03-19 12:56:50.193','2011-03-19 12:56:54.273','2011-03-19 12:57:44.000'
insert into tb select 23,'2011-03-21 09:49:51.297','2011-03-21 09:49:54.340','2011-03-21 09:53:51.000'
insert into tb select 24,'2011-03-21 09:50:18.717','2011-03-21 09:50:21.727','2011-03-21 10:23:47.000'
insert into tb select 26,'2011-03-21 09:52:48.660','2011-03-21 09:52:49.007','2011-03-21 13:21:53.000'
insert into tb select 27,'2011-