如何计算MySQL中的日用时间?

时间:2016-03-02 05:02:06

标签: mysql database

我有一个名为userActivity的表,其中记录了每个活动时段。

这是表结构:

表: userActivity

ID       user_id      start_time   end_time 

当用户上线时,会记录start time,每当在线状态发生变化时,end time都会记录在相应的行中。

现在我要生成一个报告,显示用户的日用时间。

示例输入:

ID     user_id         start_time                end_time 

'1'     '1'       '2016-02-28 10:00:00'    '2016-02-28 19:00:00'   
'2'     '1'       '2016-02-28 22:00:00'    '2016-02-29 10:00:00'
'3'     '1'       '2016-03-02 10:00:00'    '2016-03-02 19:00:00'
'4'     '1'       '2016-03-02 22:00:00'    '2016-03-06 19:00:00'

预期输出:

Date           AvailableTime(Hours)
2016-02-28         11 
2016-02-29         10
2016-03-02         11
2016-03-03         24
2016-03-04         24
2016-03-05         24
2016-03-06         19

到目前为止,我尝试过:

SELECT 
DATE_FORMAT(start_time,"%Y-%m-%d") `date`,
TIMESTAMPDIFF(HOUR,start_time,end_time) availableTime
FROM useractivity
GROUP BY `date`

得到输出:

Date         availableTime(Hours)

2016-02-28       9
2016-03-02       9

这是SQL FIDDLE

注意: 请暂时忽略user_id。我可以在应用程序级别解决它,但我想在MySQL处理它。

时间间隔可以从一天开始,一天以后结束

总之, 可用时间只是日轴的投影(从开始时间和结束时间开始) 。如果开始时间没有投放到结束时间的同一天,那么开始时间将被视为结束时间投射到的特定日期的start_time。

图示视图:

enter image description here

所以可用时间将从此屏幕截图中计算如下:

28 Feb = (t2-t1) + (t4- t3)

29 Feb = (t5 - t4)

02 Mar = (t7 - t6)

3 个答案:

答案 0 :(得分:1)

您可以使用日期表进行交叉连接,以获得您希望从日志时间中拆分的实际开始和结束时间。

CREATE TABLE `dates` (
  `date` date ,
  `start_time` timestamp ,
  `end_time` timestamp 
);

INSERT INTO `dates` VALUES('20160228','2016-02-28 00:00:00', '2016-02-29 00:00:00');
INSERT INTO `dates` VALUES('20160229','2016-02-29 00:00:00', '2016-03-01 00:00:00');
INSERT INTO `dates` VALUES('20160301','2016-03-01 00:00:00', '2016-03-02 00:00:00');
INSERT INTO `dates` VALUES('20160302','2016-03-02 00:00:00', '2016-03-03 00:00:00');
INSERT INTO `dates` VALUES('20160303','2016-03-03 00:00:00', '2016-03-04 00:00:00');
INSERT INTO `dates` VALUES('20160304','2016-03-04 00:00:00', '2016-03-05 00:00:00');
INSERT INTO `dates` VALUES('20160305','2016-03-05 00:00:00', '2016-03-06 00:00:00');
INSERT INTO `dates` VALUES('20160306','2016-03-06 00:00:00', '2016-03-07 00:00:00');

SELECT 
    u.*,
    d.date,
    case when u.start_time<= d.start_time then d.start_time
      else u.start_time end as `start_time_in_the_day`,
    case when u.end_time> d.end_time then d.end_time
      else u.end_time end as `end_time_in_the_day`
FROM useractivity u
INNER JOIN dates d
ON u.start_time< d.end_time
   and u.end_time>= d.start_time

然后,您只需要将end_time_in_the_daystart_time_in_the_day之间的小时数相加。

SELECT 
    user_id,
    date,
    sum(TIMESTAMPDIFF(HOUR,start_time_in_the_day,end_time_in_the_day)) as `availableTime`
FROM(
    SELECT 
        u.*,
        d.date,
        case when u.start_time<= d.start_time then d.start_time
          else u.start_time end as `start_time_in_the_day`,
        case when u.end_time> d.end_time then d.end_time
          else u.end_time end as `end_time_in_the_day`
    FROM useractivity u
    INNER JOIN dates d
    ON u.start_time< d.end_time
       and u.end_time>= d.start_time) as t
group by user_id,date

我的SqlFiddle here.

我认为使用TIMESTAMPDIFF(SECOND...代替TIMESTAMPDIFF(HOUR...会更好。

答案 1 :(得分:0)

我创建了UNION ALL

的查询帮助
SELECT sub_query.`date`, SUM(sub_query.available_time) FROM (
    SELECT 
        DATE_FORMAT(start_time,"%Y-%m-%d") `date`,
        IF(TIMESTAMPDIFF(day,date(start_time),date(end_time))= 0, 
           TIMESTAMPDIFF(HOUR,start_time,end_time),0) AS available_time
    FROM useractivity

    UNION ALL

    SELECT 
        DATE_FORMAT(start_time,"%Y-%m-%d") `date`,
        IF(TIMESTAMPDIFF(day,date(start_time),date(end_time)) > 0, 
           TIMESTAMPDIFF(HOUR,start_time, date_add(date(start_time),interval 24 hour)),0) AS available_time
    FROM useractivity

    UNION ALL

    SELECT 
        DATE_FORMAT(end_time,"%Y-%m-%d") `date`,
        IF(TIMESTAMPDIFF(day,date(start_time),date(end_time)) > 0,
           TIMESTAMPDIFF(HOUR,date(end_time), end_time) , 0) AS available_time
    FROM useractivity 
) AS sub_query
GROUP BY sub_query.`date`

UNION

SELECT SELECTed_date `date`, 24 FROM 
(SELECT adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) SELECTed_date FROM
 (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
 (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
 (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
 (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
 (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) v
WHERE SELECTed_date between (SELECT min(date(start_time)) FROM useractivity) and (SELECT max(date(end_time)) FROM useractivity) 
AND SELECTed_date NOT IN(
SELECT miss_date FROM (
    SELECT date(start_time) AS miss_date FROM useractivity
    UNION 
    SELECT date(end_time) AS miss_date FROM useractivity
) AS miss
) ORDER BY `date`;

SQLFiddle

答案 2 :(得分:0)

我修改了@Vipin Jain的查询以满足要求:

BasketsController