![](/img/trans.png)
[英]Is there a way to take the output of a (HH24:MI:SS - HH24:MI:SS) and convert it to only minutes in oracle sql?
[英]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.