计算在特定时间范围内运行的实例的数量

嗨,我想计算一个特定的时间片批处理运行实例的数量。 例如,我有一个表格:

BatchID startTime endTime 12957 10:15 10:25 13032 10:16 10:20 13080 10:16 10:22 13090 10:16 10:20 13214 10:19 10:30 13232 10:19 10:22 13276 10:19 10:29 13279 10:19 10:30 13315 10:20 10:23 13341 10:20 10:24 13430 10:22 10:33 13566 10:27 10:30 13580 10:27 10:31 13585 10:28 10:31 13596 10:28 10:32 13626 10:30 10:42 13637 10:32 10:35 13699 10:40 10:44 13702 10:41 10:45 

在10:41运行的实例数量为3,运行的批次为:BatchID 13626,13699和13702。

为了可视化这个问题,我从10点15分到10点41分以1分钟作为x轴,以及在那个时间分段运行的实例的数量作为y轴。 我想在ORACLE(SQL / PLSQL)或EXCEL(函数/ VBA /数据透视表/等)实现,你有什么build议?

创build您的示例表:

 SQL> create table mytable (batchid,starttime,endtime) 2 as 3 select 12957, '10:15', '10:25' from dual union all 4 select 13032, '10:16', '10:20' from dual union all 5 select 13080, '10:16', '10:22' from dual union all 6 select 13090, '10:16', '10:20' from dual union all 7 select 13214, '10:19', '10:30' from dual union all 8 select 13232, '10:19', '10:22' from dual union all 9 select 13276, '10:19', '10:29' from dual union all 10 select 13279, '10:19', '10:30' from dual union all 11 select 13315, '10:20', '10:23' from dual union all 12 select 13341, '10:20', '10:24' from dual union all 13 select 13430, '10:22', '10:33' from dual union all 14 select 13566, '10:27', '10:30' from dual union all 15 select 13580, '10:27', '10:31' from dual union all 16 select 13585, '10:28', '10:31' from dual union all 17 select 13596, '10:28', '10:32' from dual union all 18 select 13626, '10:30', '10:42' from dual union all 19 select 13637, '10:32', '10:35' from dual union all 20 select 13699, '10:40', '10:44' from dual union all 21 select 13702, '10:41', '10:45' from dual 22 / Table created. 

作为绑定variables引入要报告的时间间隔的开始和结束时间。 您可以通过它们的冒号识别SQL和PL / SQL中绑定variables的使用。

 SQL> var START_X_AXIS varchar2(5) SQL> var END_X_AXIS varchar2(5) SQL> begin 2 :START_X_AXIS := '10:15'; 3 :END_X_AXIS := '10:41'; 4 end; 5 / PL/SQL procedure successfully completed. 

为了清晰起见,分三个阶段执行查询。 首先将你的varchar2时间转换成实际的date(顺便说一句,build议也是这样保存它们)。 第二个查询显示您的报告在X轴上的所有分钟。 第三个是计数。

 SQL> with mytable_with_real_dates as 2 ( select batchid 3 , to_date(starttime,'hh24:mi') starttime 4 , to_date(endtime,'hh24:mi') endtime 5 from mytable 6 ) 7 , all_minutes as 8 ( select to_date(:START_X_AXIS,'hh24:mi') + numtodsinterval(level-1,'minute') minute 9 from dual 10 connect by level <= 24 * 60 * (to_date(:END_X_AXIS,'hh24:mi') - to_date(:START_X_AXIS,'hh24:mi')) + 1 11 ) 12 select to_char(m.minute,'hh24:mi') 13 , count(t.batchid) 14 from all_minutes m 15 left outer join mytable_with_real_dates t on (m.minute between t.starttime and t.endtime) 16 group by m.minute 17 order by m.minute 18 / TO_CH COUNT(T.BATCHID) ----- ---------------- 10:15 1 10:16 4 10:17 4 10:18 4 10:19 8 10:20 10 10:21 8 10:22 9 10:23 7 10:24 6 10:25 5 10:26 4 10:27 6 10:28 8 10:29 8 10:30 8 10:31 5 10:32 4 10:33 3 10:34 2 10:35 2 10:36 1 10:37 1 10:38 1 10:39 1 10:40 2 10:41 3 27 rows selected. 

编辑:我刚刚看到您的评论,您的列存储为date。 这是好消息,所以你可以跳过第一部分,从第7行开始,用逗号“WITH”replace逗号。

问候,
抢。

要列出所有在特定的时间:

 SELECT BatchID, startTime, endTime FROM Batch WHERE :instanceTime BETWEEN startTime AND endTime 

或者只是数一数:

 SELECT COUNT(*) AS numConcurrent FROM Batch WHERE :instanceTime BETWEEN startTime AND endTime 

但是,如果您需要在某个时间间隔内每次查询它,则可以更快地查询您的时间间隔内的所有批次,并用一些编程逻辑对其进行计数。

 SELECT startTime, endTime FROM Batch WHERE endTime > :intervalStart AND startTime < :intervalEnd ORDER BY startTime 

:instanceTime是一个查询参数。 参数被replace为您在查询旁边提供的值,您不必担心格式和转义。 (一些数据提供者使用?作为参数。)

如果您不能使用参数,您可以用实际值replace它们。 在这种情况下,不要忘记使用to_date()


以下是VBA中date参数的一些示例用法:
Bytes.com:如何将dateparameter passing到VBA中的Oracle SQLstring