日期:2014-05-18 浏览次数:20424 次
declare @T table ([user] varchar(6),operate varchar(6),time datetime) insert into @T select 'LiMing','Login','2010-10-24 08:03:00' union all select 'WangYi','Login','2010-10-24 08:14:00' union all select 'WangYi','Logout','2010-10-24 16:14:00' union all select 'LiMing','Logout','2010-10-24 16:44:00' ;with maco as( select [user], datediff(mi, max(case operate when 'Login' then [time] else '' end), max(case operate when 'Logout' then [time] else '' end) ) as mi from @T group by [user]) select [user],ltrim(mi/60)+'小时'+ltrim(mi%60)+'分' as 时长 from maco /* user 时长 ------ ------------------------------ LiMing 8小时41分 WangYi 8小时0分 */
------解决方案--------------------
DROP TABLE tb0 CREATE TABLE tb0 ( user1 VARCHAR(10), operate VARCHAR(10), time DATETIME ) GO INSERT INTO tb0 SELECT 'LiMing', 'Login', '2010-10-24 08:03:00' UNION SELECT 'WangYi', 'Login', '2010-10-24 08:14:00' UNION SELECT 'WangYi', 'Logout', '2010-10-24 16:14:00' UNION SELECT 'LiMing', 'Logout', '2010-10-24 16:44:00' SELECT User1,RIGHT(CONVERT(VARCHAR(100),(MAX(time) - MIN(time)),120),8) FROM tb0 GROUP BY user1,LEFT(time,10) User1 (No column name) LiMing 08:41:00 WangYi 08:00:00