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

时间:2011-10-11 18:37:42

标签: oracle excel plsql

您好我想计算特定时间片的批量运行实例的数量。例如,我有一张桌子:

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 / Pivot Table /等)中实现,你的建议是什么?

2 个答案:

答案 0 :(得分:2)

在特定时间列出所有内容:

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是一个查询参数。参数将替换为您在查询旁边提供的值,您无需担心格式化和转义。 (某些数据提供者使用?作为参数。)

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


以下是VBA中日期参数的一些示例用法:
Bytes.com: How to pass date parameter into Oracle SQL string in VBA

答案 1 :(得分:1)

创建样本表:

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.

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

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转换为实际日期(建议就是将它们存储起来,顺便说一下)。第二个查询显示报表的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.

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

的问候,
罗布。