如何基于duration-mysql将datetime 1行拆分为2行或更多行

时间:2017-12-11 12:26:18

标签: mysql sql

如何通过基于每小时的日期时间吐出持续时间,将1行日期时间记录分成多行

输入:

tutor_id | login_time           | logout_time           | duration
12       |'2017-02-20 11:20:20' | '2017-02-20 12:10:00' | 00:50:20
13       |'2017-02-20 12:30:00' | '2017-02-20 14:10:00' | 01:40:00

希望按潜水时间按照确切的时间每小时减少一次 输出:

tutor_id | login_time           | logout_time           | duration
12       |'2017-02-20 11:20:20' | '2017-02-20 12:00:00' | 00:40:20
12       |'2017-02-20 12:00:00' | '2017-02-20 12:10:00' | 00:10:00
13       |'2017-02-20 12:30:00' | '2017-02-20 13:00:00' | 00:30:00
13       |'2017-02-20 13:00:00' | '2017-02-20 14:00:00' | 01:00:00
13       |'2017-02-20 14:00:00' | '2017-02-20 14:10:00' | 00:10:00

请用这个帮助我

1 个答案:

答案 0 :(得分:0)

您应该尝试此查询

Select top 10  login_time, logout_time,  
CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,login_time,logout_time),'1990-01-01 00:00:00.000') as time) 
from Table

这里我假设登录和注销时间具有相同的日期。 它会在几分钟内产生差异。

select * from [login-info]

login-time  logout-time
2017-12-13 11:09:38.557 2017-12-13 11:19:38.557
2017-12-13 11:15:45.490 2017-12-13 11:19:45.490
2017-12-13 11:06:49.700 2017-12-13 11:19:49.700
2017-12-13 11:11:07.580 2017-12-13 11:20:07.580


Select [login-time] ,[logout-time], 
CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,[login-time],[logout-time]),'1990-01-01 00:00:00.000') as time) MINUTEDIFF
from [login-info]

login-time  logout-time MINUTEDIFF
2017-12-13 11:09:38.557 2017-12-13 11:19:38.557 00:10:00.0000000
2017-12-13 11:15:45.490 2017-12-13 11:19:45.490 00:04:00.0000000
2017-12-13 11:06:49.700 2017-12-13 11:19:49.700 00:13:00.0000000
2017-12-13 11:11:07.580 2017-12-13 11:20:07.580 00:09:00.0000000