gpt4 book ai didi

mysql - 我想在 SQL 查询中显示总工作时间和分钟

转载 作者:行者123 更新时间:2023-11-29 15:38:19 25 4
gpt4 key购买 nike

我想在 SQL 查询中显示总工作时间和分钟

select 
mom.machinery_operation_master_id,
mom.assigned_code,
mom.record_type,
(case when mom.record_type=1 then time(mom.entered_time) end) power_on_time,
(case when mom.record_type=2 then time(mom.entered_time) end) power_off_time,
(case when mom.record_type=3 then time(mom.entered_time) end) breakdown_time,
(case when mom.record_type=4 then time(mom.entered_time) end) repaired_time
from machinery_operation_master mom
where mom.location_id = '9' and
mom.created_at between '2019-09-17 08:00:00' and '2019-09-18 07:59:00'

假设第一行的开机时间:13:00:00,第二行的关机时间:14:30:00,那么我的预期输出应该是这样的。总小时数和分钟数:1:30:00

最佳答案

使用 mariadb:结果为 HH:mm

select concat((TIMESTAMPDIFF(second, t1.power_on_time, t1.power_off_time) 
- TIMESTAMPDIFF(second, t1.breakdown_time, t1.repaired_time))/3600, ':',
((TIMESTAMPDIFF(MINUTE, t1.power_on_time, t1.power_off_time)
- TIMESTAMPDIFF(MINUTE, t1.breakdown_time, t1.repaired_time)) % 60), ':00') as working_time
from
(select
mom.machinery_operation_master_id,
mom.assigned_code,
mom.record_type,
(case when mom.record_type=1 then time(mom.entered_time) end) power_on_time,
(case when mom.record_type=2 then time(mom.entered_time) end) power_off_time,
(case when mom.record_type=3 then time(mom.entered_time) end) breakdown_time,
(case when mom.record_type=4 then time(mom.entered_time) end) repaired_time,
from machinery_operation_master mom
where mom.location_id = '9' and
mom.created_at between '2019-09-17 08:00:00' and '2019-09-18 07:59:00') as t1

关于mysql - 我想在 SQL 查询中显示总工作时间和分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57986668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com