执行查询时计算时差

时间:2015-10-28 06:23:02

标签: mysql

查询:

SELECT products.product_name ,
employee.emp_name,employee.emp_code,process.process_name,timer_completed.worked_qty , 
FROM_UNIXTIME(timer_completed.start/1000,'%d-%m-%Y %H:%i:%s') as start,
FROM_UNIXTIME(timer_completed.end/1000,'%d-%m-%Y %H:%i:%s') as end , 
FROM timer_completed
INNER JOIN com_jobcard ON com_jobcard.id = timer_completed.job_card_id
INNER JOIN products ON products.id = com_jobcard.product_id    
INNER JOIN employee ON employee.id = timer_completed.employee_id
INNER JOIN process ON process.id = timer_completed.process_id
INNER JOIN carat_percentage ON carat_percentage.id = com_jobcard.carat_id
INNER JOIN groups ON groups.id = timer_completed.group_id
INNER JOIN departments ON departments.id = groups.dept_id
AND departments.id = '40';

输出:

JCID-1151521    PRNG    ARAVINTHAN.N        A4942   Brushing           0    07-07-2015 14:35:10     07-07-2015 14:43:45     
JCID-1149968    SRNL    Sathya              A4535   Correction          29  07-07-2015 18:06:28     07-07-2015 18:32:33     
JCID-1149968    SRNL    Sathya              A4535   Inside_Correction   29  07-07-2015 18:32:45     07-07-2015 18:42:44     
JCID-1149968    SRNL    Pani Paul Prakash   A3100   Lapping          30     08-07-2015 09:16:20     08-07-2015 09:16:46     
JCID-1149968    SRNL    Pani Paul Prakash   A3100   Nice             30     08-07-2015 13:14:36     08-07-2015 13:37:50     

亲爱的以下查询的朋友我将得到如下所示的结果,但我的预期结果是我将SUM(timer_completed.worked_qty)的值相加,以便开始时间和结束时间必须减去,值必须是平行的。 我将按employee.emp_code使用group,因此工作的数量必须是sum,并且对于每次迭代,必须减去开始时间和结束时间 ((07-07-2015 18:32:45) - (07-07-2015 18:42:44))最后必须给出工作数量和时间的总和

预期结果是

JCID-1151521    PRNG    ARAVINTHAN.N        A4942   Brushing           0   0:0:35
JCID-1149968    SRNL    Sathya              A4535   Correction          58  0:23:47
JCID-1149968    SRNL    Pani Paul Prakash   A3100   Lapping          60     0:3:50

2 个答案:

答案 0 :(得分:0)

使用以下代码

替换以下代码

TIMESTAMPDIFF(SECOND, FROM_UNIXTIME(timer_completed.start/1000,'%d-%m-%Y %H:%i:%s'), FROM_UNIXTIME(timer_completed.end/1000,'%d-%m-%Y %H:%i:%s')) as res

使用

{{1}}

随意发表评论

答案 1 :(得分:0)

使用TIMESTAMPDIFF和GROUP BY以及SUM MySQL函数。

我编辑了整个查询(假设A4942,A4535是process.ids)

SELECT products.product_name, employee.emp_name,employee.emp_code,
process.process_name, 
SUM(timer_completed.worked_qty),SUM(timer_completed.start -timer_completed.end) as res, 
FROM timer_completed
INNER JOIN com_jobcard ON com_jobcard.id = timer_completed.job_card_id
INNER JOIN products ON products.id = com_jobcard.product_id    
INNER JOIN employee ON employee.id = timer_completed.employee_id
INNER JOIN process ON process.id = timer_completed.process_id
INNER JOIN carat_percentage ON carat_percentage.id = com_jobcard.carat_id
INNER JOIN groups ON groups.id = timer_completed.group_id
INNER JOIN departments ON departments.id = groups.dept_id
AND departments.id = '40'
GROUP BY process.id;