MySql SUM函数在数据不同的情况下为我的查询得到类似的答案



我在mysql数据库中有更多的数据,我想通过TIMEDIFF(actual_end_time,actual_start_time)每月计算持续时间。但它给了我同样的产出数字,这是不可能的。我的查询是:

SELECT
  TIME_FORMAT(
    SEC_TO_TIME( SUM( TIME_TO_SEC(
      TIMEDIFF( eo.actual_end_time, eo.actual_start_time )))), '%Hh %im %ss') AS timeDiff
  ,MONTH(date_created) AS MONTH 
  FROM event_operations AS eo 
  GROUP BY MONTH(date_created);

输出:

timeDiff        month
150h 26m 42s     NULL
102h 53m 59s      5
838h 59m 59s      6
838h 59m 59s      7
838h 59m 59s      8
838h 59m 59s      9
838h 59m 59s     10
838h 59m 59s     11
838h 59m 59s     12

我从第6个月到第12个月都有类似的情况,这是不可能的。任何帮助都是可观的。

问题在于TIME_TO_SEC函数,请参阅此处有关时间类型的文档。

TIME值的范围可能从"-838:59:59"到"838:59:59"。

作为一种变通方法,我会尝试使用这样的查询:

select
  month,
  concat_ws(' ',
    concat((sec DIV 60*60), 'h'),
    concat((sec DIV 60) % 60, 'm'),
    concat(sec % 60, 's')
  )
from (
select
  SUM(
    to_seconds(
      TIMEDIFF(eo.actual_end_time,eo.actual_start_time)
    )
  ) sec,
  month(date_created) as month
from
  event_operations as eo
) s;

最新更新