[英]Sum of time difference - Oracle SQL
I have a table: 我有一张桌子:
table1 表格1
col1 start_date_time end_date_time
Test1 01-06-19 11:43:35.927422000 AM 01-06-19 11:44:20.127907000 AM
Test2 01-06-19 11:44:28.703518000 AM 01-06-19 11:45:06.538883000 AM
Test3 01-06-19 11:42:18.784477000 AM 01-06-19 11:42:27.635102000 AM
I wrote a query which gives me the time difference: 我写了一个查询,给了我时差:
select a.*, end_date_time - start_date_time exec_time from table1 a
order by exec_time desc;
Output Table 输出表
col1 start_date_time end_date_time exec_time
Test1 01-06-19 11:43:35.927422000 AM 01-06-19 11:44:20.127907000 AM +00 00:00:44.200485
Test2 01-06-19 11:44:28.703518000 AM 01-06-19 11:45:06.538883000 AM +00 00:00:37.835365
Test3 01-06-19 11:42:18.784477000 AM 01-06-19 11:42:27.635102000 AM +00 00:00:08.850625
I need the sum like 00:01:29. 我需要像00:01:29这样的总和。
How can I sum the exec_time in SQL? 如何在SQL中汇总exec_time? I need to find the total time based. 我需要找到总时间。
Oracle does not support the aggregation of interval datatypes, which is a crying shame because that would be the easiest way to solve problems like this. Oracle不支持间隔数据类型的聚合,这令人遗憾,因为这将是解决此类问题的最简单方法。 However, the fiendish mind of Stew Ashton came up with a solution of converting intervals into numbers and back again. 但是, Stew Ashton的顽强思想提出了一种将间隔转换为数字然后再次返回的解决方案 。
select numtodsinterval(
sum(
(sysdate+(end_date_time - start_date_time) - sysdate) * 24 * 60 * 60
+ extract(second from (end_date_time - start_date_time))
- trunc(extract(second from (end_date_time - start_date_time))) -- (*)
)
, 'second'
)
from table1;
Here is a demo on db<>fiddle . 这是db <> fiddle上的演示 。
(*) This line makes the answer accurate to microseconds. (*)该行使答案精确到微秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.