Divide time period into partitions and calculate averages

时间:2015-08-06 13:55:33

标签: oracle olap

I have to divide elapsed time of a process into slots of equal sizes and calculate number of rows inserted during each time slot.

Here is DDL of the table

  ID                  number
  insert_date_time    date

Sample Data

    101          Aug 1 2015 4:43:00 PM
    931          Aug 1 2015 4:43:01 PM

The output I am looking for is as follows

 Time Slot          Rows Inserted
4:00 pm  - 5:00 pm       103
5:00 pm  - 6:00 pm       95
6:00 pm  - 7:00 pm       643

( I have left out date portion for brevity )

Similarly, I have to find out how much time each 100 rows took

    0 - 100 rows        4:00pm - 4:43pm
  101 - 200 rows        4:43pm - 5:58pm

I know Oracle OLAP functions can be used for this, but not sure how ?

2 个答案:

答案 0 :(得分:1)

希望我理解你的要求。

First Req#:

SELECT TO_CHAR(INSERT_TIME,'HH24'),COUNT(*) FROM TABLE
GROUP BY TO_CHAR(INSERT_TIME,'HH24');

Second Req#:

SELECT ROUND((ROW_NUMBER-1)/100,0),
MIN(INSERT_TIME)      +'-'+ MAX(INSERT_TIME)
FROM TABLE
GROUP BY ROUND((ROW_NUMBER-1)/100,0);

答案 1 :(得分:0)

如果您正在寻找1小时的分区,您只需使用分组子句中的截断值,将您正在查看的日期列截断为小时trunc(your_date_col, 'hour')

对于第二个要求,您可以使用如下查询对行进行分区:

with data as (
select a.your_date_col
     , row_number() over (order by Your_Date_Col) rn
     , row_number() over (order by Your_Date_Col) -
       mod(row_number() over (order by Your_Date_Col)-1,50) grp
  from your_table a
)
select min(rn) ||' - '
     ||max(rn) ||' rows' rows
     , min(your_date_col)||' - '
     ||max(your_date_col) time_range
  from data
 group by grp