sql problem more difficult to appear in the forum: 45 (users online landing time in hours, minutes calculation)

Original: SQL problem more difficult to appear in the forum: 45 (users online landing time in hours, minutes calculation)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.



How to solve users online landing time - the hour and minute calculation.

http://bbs.csdn.net/topics/390613823

I want users online time, the format is: 08: 00 and when 8:43 this online format length results.



I try to check the documentation sql, also Baidu a lot. But no application in this regard.
I have also tried to write a lot but I can not.
I can get the total number of minutes online, or the total number of seconds. Not inflicted desired format.
This is my code:
SELECT TA [User], (the DATEDIFF (mi The, ta.time, tb.time)) from. 
(SELECT * WHERE T1.operate = Tl from 'the Login') AS TA
Inner the Join 
(SELECT * from WHERE T1.operate = Tl 'Zimbabwe Logout') AS TB
ON TA. [User] = TB. [User]

-------------------------- ----------
or write:
SELECT TA [User], Cast (DATEDIFF (hour, ta.time, tb.time) AS VARCHAR) +. ':' + Cast (the DATEDIFF (MINUTE, TA .time, tb.time) AS VARCHAR)
from
(SELECT [User], [Time] from Tl WHERE [Operate] = 'Login') AS TA
Inner the Join
(SELECT [User], [Time] from Tl WHERE [Operate] = 'Zimbabwe Logout') AS TB
. ON TA [User] = TB [User].;

Both versions can not achieve the desired results. At the request of the god of guidance, help give a solution.
I finished testing, hair renderings.

My solution:

method 1:


   
   
  1. drop table t1
  2. create table T1
  3. (
  4. [ user] varchar( 30),
  5. operate varchar( 10),
  6. time datetime
  7. )
  8. insert into T1
  9. select 'LiMing', 'Login', '2010/10/24 8:03' union all
  10. select 'WangYi', 'Login', '2010/10/24 8:14' union all
  11. select 'WangYi', 'Logout', '2010/10/24 16:14' union all
  12. select 'LiMing', 'Logout', '2010/10/24 16:14'
  13. select [ user],
  14. cast( cast( round( interval * 1.0 / 60, 0, 1) as int) as varchar) + ':' +
  15. case when interval * 1.0 % 60 <> 0
  16. then cast( cast( round( interval * 1.0 % 60, 0, 1) as int) as varchar)
  17. else '00'
  18. end
  19. from
  20. (
  21. select T1.[ user],
  22. DATEDIFF( MINUTE,t1.time,t2.time) as interval
  23. from T1
  24. inner join T1 t2
  25. on t1.[ user] = t2.[ user]
  26. and t1.operate = 'login'
  27. and t2.operate = 'logout'
  28. )a
  29. /*
  30. user (无列名)
  31. LiMing 8:11
  32. WangYi 8:00
  33. */

Method 2:


   
   
  1. --方法2.
  2. select [ user],
  3. convert( varchar( 5), DATEADD( MINUTE, interval, time), 114)
  4. from
  5. (
  6. select T1.[ user],
  7. convert( varchar( 10),t1.time, 120) as time,
  8. DATEDIFF( MINUTE,t1.time,t2.time) as interval
  9. from T1
  10. inner join T1 t2
  11. on t1.[ user] = t2.[ user]
  12. and t1.operate = 'login'
  13. and t2.operate = 'logout'
  14. )a
  15. /*
  16. user (无列名)
  17. LiMing 08:11
  18. WangYi 08:00
  19. */

 

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.

Published 416 original articles · won praise 135 · views 950 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020079.html