繁体   English   中英

Oracle SQL:总结HH:MI:SS

[英]Oracle SQL: Summing HH:MI:SS

我使用以下脚本来总结一个生产订单的完成和下一个生产订单的开始之间的持续时间......

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'的'Durations'。 当脚本运行时,我得到以下16行结果。

在此输入图像描述

我想要做的是从组中删除'a.time_stamp_on',只留下持续时间的总和(07:26:55),但是当我按功能I从组中删除'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)

我会用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM