表 a
时间 用户名 动作
-------------------- ----- -----
2009-01-15 18:00:00 李 登陆
2009-01-15 22:00:00 李 退出
2009-01-14 12:00:00 赵 登陆
2009-01-14 13:00:00 李 登陆
2009-01-14 12:00:00 赵 退出
-------------------------------------------------
如何在这个表中查出他们在'2009-01-15 18:00:00'到'2009-01-14 12:00:00 '各自的登陆时长 如何统计??????????
时间 用户名 动作
-------------------- ----- -----
2009-01-15 18:00:00 李 登陆
2009-01-15 22:00:00 李 退出
2009-01-14 12:00:00 赵 登陆
2009-01-14 13:00:00 李 登陆
2009-01-14 12:00:00 赵 退出
-------------------------------------------------
如何在这个表中查出他们在'2009-01-15 18:00:00'到'2009-01-14 12:00:00 '各自的登陆时长 如何统计??????????
insert into tb values('2009-01-15 18:00:00','李', '登陆')
insert into tb values('2009-01-15 22:00:00','李', '退出')
insert into tb values('2009-01-14 12:00:00','赵', '登陆')
insert into tb values('2009-01-14 13:00:00','李', '登陆')
insert into tb values('2009-01-14 12:00:00','赵', '退出')
go
select m.用户名 , sum(datediff(mi,m.时间,n.时间)) 分钟 from
(select * , (select count(*) from tb where 用户名 = t.用户名 and 动作 = '登陆' and 时间 < t.时间) + 1 px from tb t) m ,
(select * , (select count(*) from tb where 用户名 = t.用户名 and 动作 = '退出' and 时间 < t.时间) + 1 px from tb t) n
where m.用户名 = n.用户名 and m.px = n.px
group by m.用户名drop table tb/*
用户名 分钟
---------- -----------
李 3720
赵 0(所影响的行数为 2 行)
*/
insert into tb values('2009-01-15 18:00:00','李', '登陆')
insert into tb values('2009-01-15 22:00:00','李', '退出')
insert into tb values('2009-01-14 12:00:00','赵', '登陆')
insert into tb values('2009-01-14 13:00:00','李', '登陆')
insert into tb values('2009-01-14 12:00:00','赵', '退出')
go
select m.用户名 , sum(datediff(mi,m.时间,n.时间)) 分钟 from
(select * , (select count(*) from tb where 用户名 = t.用户名 and 动作 = '登陆' and 时间 between '2009-01-14 12:00:00 ' and '2009-01-15 18:00:00' and 时间 < t.时间) + 1 px from tb t) m ,
(select * , (select count(*) from tb where 用户名 = t.用户名 and 动作 = '退出' and 时间 between '2009-01-14 12:00:00 ' and '2009-01-15 18:00:00' and 时间 < t.时间) + 1 px from tb t) n
where m.用户名 = n.用户名 and m.px = n.px
group by m.用户名drop table tb/*
用户名 分钟
---------- -----------
李 3720
赵 0(所影响的行数为 2 行)
*/