mysql - Mysql中时间戳的总和

标签 mysql

I'm trying to get the average of Completed time, so at first, I'm getting the completed time of individual processOrders.

下面的查询将获取单个 processOrders 的完成时间。

select processorder_prod_number,ifnull(time(round( timediff(max(completed_time), 
min(start_time)))) ,'00:00:00') as `completedtime`
from processorder_prod where week(start_time)=week(curdate())
group by processorder_prod_number;

输出是这样的

+--------------------+----------------+
| processorder_number| completedtime  | 
+--------------------+----------------+
|   1002135897       |  01:42:15      |
|   1002135898       |  01:39:43      |
|   1002135900       |  05:31:52      |
|   1002135901       |  02:31:52      |
+--------------------+----------------+

当我尝试获取总时间,即 sum(completedtime) 时,它得到了错误的输出。

select count(distinct processorder_prod_number),time(round(sum(completedtime))) as totaltime from
(select processorder_prod_number,ifnull(time(round( timediff(max(completed_time), 
min(start_time)))) ,'00:00:00') as `completedtime`
from processorder_prod where week(start_time)=week(curdate())
group by processorder_prod_number) as t;

上述查询的输出

+---------------------------+----------------+
| count(processorder_number)| completedtime  | 
+---------------------------+----------------+
|   4                       |   01:42:15     |
+---------------------------+----------------+

请在我做错的地方帮助我。欢迎任何建议和更正,在此先感谢。

最佳答案

在这里,您将获得已完成时间的总和。

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `time` ) ) ) AS `completedtime` FROM dual;

更新的答案

 SELECT COUNT(DISTINCT processorder_prod_number), TIME(ROUND(SUM(completedtime))) AS  totaltime FROM (SELECT processorder_prod_number, IFNULL(SEC_TO_TIME( SUM( TIME_TO_SEC( `completed_time` ) ) ),'00:00:00') AS  `completedtime`  FROM processorder_prod WHERE WEEK(start_time)=WEEK(CURDATE())  GROUP BY processorder_prod_number) AS t;

关于mysql - Mysql中时间戳的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562122/

相关文章:

c++ - 另一个使用相同 InnoDB 数据或日志文件的 mysqld 进程

mysql - sql中的最大函数

php - 尝试在 mac os mavericks 上使用 mysql、php 和 apache 连接到本地主机

DataGrip 升级后 MySQL 数据库连接中断 (2021.3.x)

mysql - 如何在 SQL 中使用 NULL 存储缺失的 FLOAT 值

php - 像 "v.*"这样的语法在 MySQL 查询中意味着什么?

C# LINQ 查询 (MYSQL EF) - 不同的最新记录

mysql - 这个 phpbb 查询可以优化吗?

mysql - 如何使用带有值 OR IS NULL 的 IF 语句构建 WHERE 子句

mysql - 对于空值,计数无法正常运行