我正在使用以下脚本来计算一个生产订单完成和下一个生产订单开始之间的持续时间。
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_namecase函数将大于'00:35:00‘的’Duration‘替换为'00:35:00’。当脚本运行时,我会得到以下16行的结果。

我想做的是从group by函数中删除'a.time_stamp_on‘,只留下持续时间的总和(07:26:55),但是当我从group by函数中删除'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)发布于 2017-06-26 00:15:58
我将使用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 0https://stackoverflow.com/questions/44747933
复制相似问题