Mysql中timeStamp的总和

时间:2018-02-01 11:52:26

标签: mysql

  

我正在尝试获得完成时间的平均值,所以首先,我得到了单个processOrders的完成时间。

下面的Query将获得单个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      |
+--------------------+----------------+

当我试图得到总时间,即总和(完成时间)时,输出错误。

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     |
+---------------------------+----------------+

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

1 个答案:

答案 0 :(得分:1)

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

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;