0

我有一个包含事务时间的表,1 个批次将触发多个作业。我想为批处理添加每个作业中经过的时间。事务表的格式如下,

START_TIME          JOB END_TIME
1/7/2012 12:10:24   AA  1/7/2012 12:18:49
1/7/2012 12:10:23   AA  1/7/2012 12:10:23
1/7/2012 12:10:24   AA  1/7/2012 12:10:24
7/7/2012 16:10:33   AA  7/7/2012 16:15:23
7/7/2012 16:10:32   AA  7/7/2012 16:10:32
7/7/2012 16:10:33   AA  7/7/2012 16:10:33
7/7/2012 17:58:21   BB  7/7/2012 19:05:41
7/7/2012 16:40:25   BB  7/7/2012 16:40:25
7/7/2012 17:56:58   BB  7/7/2012 17:56:58
1/7/2012 14:00:11   BB  1/7/2012 14:47:09
1/7/2012 12:40:19   BB  1/7/2012 12:40:19
1/7/2012 13:58:33   BB  1/7/2012 13:58:33

我想要达到的是,

DATE       JOB    TIME SPENT
1/7/2012   AA     ADDING THE TIME SPENT for AA on 1/7/2012
7/7/2012   AA     ADDING THE TIME SPENT for AA on 7/7/2012
1/7/2012   BB 
7/7/2012   BB

我可以知道什么 SQL 可以达到预期的效果吗?

谢谢!!

4

1 回答 1

0

假设START_TIMEandEND_TIME都是DATE数据类型(而不是TIMESTAMPor VARCHAR2)并假设START_TIMEandEND_TIME总是在同一天(即它不能在 1/7 晚开始并在 1/8 早点结束)

SELECT trunc(start_time), 
       job, 
       sum( end_time - start_time ) total_time_in_days
  FROM your_table
 GROUP BY trunc(start_time), job
 ORDER BY job, trunc(start_time)

总数将以天数表示。如果你想得到一个INTERVAL

SELECT trunc(start_time), 
       job, 
       sum( numtodsinterval( end_time - start_time, 'day' ) ) elapsed_interval
  FROM your_table
 GROUP BY trunc(start_time), job
 ORDER BY job, trunc(start_time)
于 2012-10-10T16:41:59.670 回答