这是这个题目:
我自己尝试了写了很多但无法实现。这是我的代码:你可以测试一下。
select ta.[user],(DATEDIFF(mi,ta.time,tb.time)) from (select * from T1 where T1.operate='Login') as ta inner join (select * from T1 where T1.operate='Logout') as tb
on ta.[user]=tb.[user]
---------------------------------
另外一种格式:
select ta.[user], cast(datediff(hour, ta.time, tb.time) as varchar) + ':' + cast(DATEDIFF(MINUTE, ta.time, tb.time) as varchar)
from
(select [user], [time] from T1 where [operate] = 'login') as ta
inner join
(select [user], [time] from T1 where [operate] = 'logout') as tb
on ta.[user] = tb.[user];
我无法得到像08:00或者08:43和07:35等,这种格式的用户在线时长。
datediff的结果是总在线的分钟或者秒数。
希望大家给个思路。很着急!
select convert(varchar, convert(datetime, datediff(SS, '2010-08-10 08:00:59', '2010-08-10 16:09:01')/convert(decimal,86400)), 8) --08:08:02
不好意思,我在msdn问道答案了,谢谢你。
我很奇怪,为什么要用SQL给写出来,难不成代码中不能写么,代码中写应该比SQL执行速度快吧
这是面试题,不能改要求。
DECLARE @st DATETIME=GETDATE(),@et DATETIME='2013-10-13 23:55:12' SELECT CONVERT(VARCHAR(5),@et-@st,114)
你看看吧~~~
需求甚是奇怪
这是面试题,不能改要求。
@荆棘人: 用datepart函数可以取到时间的时分
试试这样
DECLARE @data table ([user] varchar(200), operate varchar(200), [time] datetime) INSERT into @data VALUES ('LiMing', 'Login', '2010-10-24 8:03'), ('WangYi', 'Login', '2010-10-24 8:14'), ('LiMing', 'Logout', '2010-10-24 16:14'), ('WangYi', 'Logout', '2010-10-24 16:44') select t1.[user], CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, (select max([time]) from @data t where operate = 'Login' And t.[user] = t1.[user]), (select max([time]) from @data t where operate = 'Logout' And t.[user] = t1.[user])), 0), 114) from @data t1 group by t1.[user]
不好意思,我在msdn问道答案了,谢谢你。