首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oracle SQL: Summing HH:MI:SS

Oracle SQL: Summing HH:MI:SS
EN

Stack Overflow用户
提问于 2017-06-25 23:41:53
回答 1查看 190关注 0票数 1

我正在使用以下脚本来计算一个生产订单完成和下一个生产订单开始之间的持续时间。

代码语言:javascript
复制
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‘的’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:无效数字):

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-06-26 00:15:58

我将使用lead来计算持续时间,请参阅下面的示例

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44747933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档