Sql面试总结

1.数据表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:13
WangYi
8:46
create table tb ( [user] varchar(20), operate varchar(20), [time] datetime) 
insert into tb select 'LiMing', 'Login', '2010/10/24 8:03' 
union all 
select 'WangYi', 'Login', '2010/10/24 8:14' 
union all 
select 'WangYi', 'Logout', '2010/10/24 16:14' 
union all 
select 'LiMing', 'Logout', '2010/10/24 16:44' 
select a.[user] , cast(DATEdiff(MI, b.[time] ,a.[time] )/ 60 as varchar) + ':' + cast(DATEdiff(MI, b.[time] ,a.[time] )%60 as varchar) AS 在线时间 
from ( select * from tb where operate ='Logout') a, 
(select * from tb where operate ='Login') b 
where a.[user] =b.[user] 

 

posted on 2013-11-04 15:27  douqiumiao  阅读(238)  评论(0编辑  收藏  举报