MySQL - SEC_TO_TIME给出相同的结果,无论组

时间:2015-10-23 13:13:34

标签: mysql sql database datetime time

使用以下查询以获得每月的时差之和。基本上我是一个开始时间,停止时间。我采取的差异等于运行时间,然后我喜欢将所有运行时间加起来一个月,说1月我们有x小时y分钟和z秒运行时间。

但是,在对值进行求和时,我会遇到奇怪的结果。总结1月和2月的秒数。我得到了不同的结果。然后,当将秒转换为时间戳时,它们将解析为相同的时间值。

查询1

select sum(TIME_TO_SEC(diff)) as "total runtime" from
(SELECT
    CASE WHEN RIGHT(stp_time,3)="DUR"
        THEN
            TIMEDIFF(LEFT(stp_time,8), '00:00:00')
        ELSE
            TIMEDIFF(
                STR_TO_DATE(CONCAT(stp_date," ",LEFT(stp_time,8)), '%d/%b/%Y %H:%i:%s'),
                STR_TO_DATE(CONCAT(str_date," ",LEFT(str_time,8)), '%d/%b/%Y %H:%i:%s')
                )
    END AS diff, ext_umid, str_date
FROM vms1_asrun
where str_date like "%JAN%") as a

"%JAN%" --->的结果enter image description here

"%FEB%" --->的结果enter image description here

查询2

select monthname(STR_TO_DATE(STR_DATE, "%d/%b/%Y")) as month,
SEC_TO_TIME(sum(TIME_TO_SEC(diff)))
                 from 
(SELECT
    CASE WHEN RIGHT(stp_time,3)="DUR"
        THEN
            TIMEDIFF(LEFT(stp_time,8), '00:00:00')
        ELSE
            TIMEDIFF(
                STR_TO_DATE(CONCAT(stp_date," ",LEFT(stp_time,8)), '%d/%b/%Y %H:%i:%s'),
                STR_TO_DATE(CONCAT(str_date," ",LEFT(str_time,8)), '%d/%b/%Y %H:%i:%s')
                )
    END AS diff, ext_umid, str_date
FROM vms1_asrun) as a
group by month

每个月的结果相同(12月除外,数据量显着减少)

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是我正在使用的当前查询。由于最大TIME对象值的限制,无法访问长时间运行的时间码。 我选择的解决方法是简单地显示所花费的时间,选择时间戳

 select monthname(STR_TO_DATE(STR_DATE, "%d/%b/%Y")) as month,
    sum(TIME_TO_SEC(diff))/3600 as hours
                     from 
    (SELECT
        CASE WHEN RIGHT(stp_time,3)="DUR"
            THEN
                TIMEDIFF(LEFT(stp_time,8), '00:00:00')
            ELSE
                TIMEDIFF(
                    STR_TO_DATE(CONCAT(stp_date," ",LEFT(stp_time,8)), '%d/%b/%Y %H:%i:%s'),
                    STR_TO_DATE(CONCAT(str_date," ",LEFT(str_time,8)), '%d/%b/%Y %H:%i:%s')
                    )
        END AS diff, ext_umid, str_date
    FROM vms1_asrun) as a
    group by month