Oracle SQL:求和 HH:MI:SS
Posted
技术标签:
【中文标题】Oracle SQL:求和 HH:MI:SS【英文标题】:Oracle SQL: Summing HH:MI:SS 【发布时间】:2017-06-25 15:41:52 【问题描述】:我正在使用以下脚本来计算一个生产订单完成与下一个订单开始之间的持续时间...
select mac.name, par.name name_1, ref.name Fehler, count(*) count,
case when
to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(log.time_stamp_to - log.time_stamp_on)),'HH24:MI:SS')
> to_char(to_date('01-JAN-2001 00:35:00','DD-MM-YYYY HH24:MI:SS'),'HH24:MI:SS')
then
to_char(to_date('01-JAN-2001 00:35:00','DD-MM-YYYY HH24:MI:SS'),'HH24:MI:SS')
else
to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(log.time_stamp_to - log.time_stamp_on)),'HH24:MI:SS')
end duration
from mde_logstate log
right outer join mde_refstate ref
on log.mach_typ = ref.mach_typ
and log.part_id = ref.part_id
and log.state_id = ref.state_id
and ref.name = 'ORDERCHANGE'
right outer join mde_machpart par
on log.mach_typ = par.mach_typ
and log.part_id = par.part_id
right outer join mde_mach mac
on log.mach_typ = mac.mach_typ
and log.mach_id = mac.mach_id
and mac.name = 'OFFSET PRINTER NO.3'
where log.mach_typ in ('80','82')
and log.time_stamp_on between to_date('01-05-2017 06:00:00','DD-MM-YYYY HH24:MI:SS') and to_date('01-06-2017 06:59:59','DD-MM-YYYY HH24:MI:SS')
and not
(select to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(a1.time_stamp_to-a1.time_stamp_on)),'HH24:MI:SS')
from mde_logstate a1, mde_refstate b, mde_machpart d, mde_mach e1
where a1.mach_typ = log.mach_typ and a1.mach_typ = b.mach_typ and a1.mach_typ = d.mach_typ and a1.mach_typ = mac.mach_typ and a1.mach_id = e1.mach_id
and a1.part_id = b.part_id and a1.part_id = d.part_id and a1.state_id = b.state_id and b.name = 'PRODUCTION'
and a1.time_stamp_on between to_date('01-05-2017 06:00:00','DD-MM-YYYY HH24:MI:SS') and to_date('01-06-2017 06:59:59','DD-MM-YYYY HH24:MI:SS')
and trunc(log.time_stamp_on) = trunc(a1.time_stamp_on) and e1.short_name = mac.short_name) is null
group by log.time_stamp_on, mac.name, par.name, ref.name, mac.mach_typ, log.mach_typ, mac.short_name
case 函数将大于“00:35:00”的“持续时间”替换为“00:35:00”。当脚本运行时,我得到以下 16 行结果。
我想做的是从 group by 函数中删除“a.time_stamp_on”,只留下持续时间的总和(07:26:55),但是当我从组中删除“a.time_stamp_on”时通过函数我得到以下结果;
如何获得“持续时间”的总和?我尝试了以下 'sum over partition by',但我得到一个 oracle 错误(ORA-01722:无效数字):
sum(case when
to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(a.time_stamp_to - a.time_stamp_on)),'HH24:MI:SS')
> to_char(to_date('01-JAN-2001 00:35:00','DD-MM-YYYY HH24:MI:SS'),'HH24:MI:SS')
then
to_char(to_date('01-JAN-2001 00:35:00','DD-MM-YYYY HH24:MI:SS'),'HH24:MI:SS')
else
to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(a.time_stamp_to - a.time_stamp_on)),'HH24:MI:SS')
end) over (partition by e.name)
【问题讨论】:
我认为这个答案可以帮助你很多。 Calculate the Sum of duration in sql query 使用DATE '2001-01-01'
而不是 to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')
- 它更短
我认为比较更简单sum(log.time_stamp_to - log.time_stamp_on) > 35/24/60
而不是to_char(to_date('01-JAN-2001 00:00:00','DD-MM-YYYY HH24:MI:SS')+(sum(log.time_stamp_to - log.time_stamp_on)),'HH24:MI:SS') > to_char(to_date('01-JAN-2001 00:35:00','DD-MM-YYYY HH24:MI:SS'),'HH24:MI:SS')
【参考方案1】:
我会使用lead
来计算持续时间
请参阅下面的示例
with orders as
(select 1 as order_num, sysdate-9 as startdate from dual union all
select 2 as order_num, sysdate-8 as startdate from dual union all
select 3 as order_num, sysdate-7 as startdate from dual union all
select 4 as order_num, sysdate-4 as startdate from dual union all
select 5 as order_num, sysdate-1 as startdate from dual
)
select order_num, startdate, lead(startdate, 1) over (order by startdate) next_order_start
,nvl(lead(startdate, 1) over (order by startdate), startdate) - startdate as duration
from orders
ORDER_NUM STARTDATE NEXT_ORDER_START DURATION
1 1 16/06/2017 17:13:53 17/06/2017 17:13:53 1
2 2 17/06/2017 17:13:53 18/06/2017 17:13:53 1
3 3 18/06/2017 17:13:53 21/06/2017 17:13:53 3
4 4 21/06/2017 17:13:53 24/06/2017 17:13:53 3
5 5 24/06/2017 17:13:53 0
【讨论】:
以上是关于Oracle SQL:求和 HH:MI:SS的主要内容,如果未能解决你的问题,请参考以下文章