日期:2014-05-17  浏览次数:20450 次

求2个时间段的差怎么写
A. 数据表T用来记录用户登录、注销信息,其中只有4个记录:
user(用户名) operate(操作) time(时间)
LiMing Login 2010/10/24 8:03
WangYi Login 2010/10/24 8:14
WangYi Logout 2010/10/24 16:14
LiMing Logout 2010/10/24 16:44
求一个SQL查询语句,返回用户在线时间情况的结果集:
LiMing 8:31
WangYi 8:00
Sql

------解决方案--------------------
用 datediff 函数.
例:
create table tb(users nvarchar(20),operate nvarchar(20),dtime datetime)
insert into tb select 'LiMing','Login','2010/10/24 8:03'
insert into tb select 'WangYi','Login','2010/10/24 8:14'
insert into tb select 'WangYi','Logout','2010/10/24 16:14'
insert into tb select 'LiMing','Logout','2010/10/24 16:44'
go
select a.users,datediff(mi,b.dtime,a.dtime)
from tb a inner join tb b on a.users=b.users
where a.operate='Logout' and b.operate='Login'
/*
users                
-------------------- -----------
WangYi               480
LiMing               521

(2 行受影响)

*/
go
drop table tb