最大日期间隔的分析功能范围窗口

时间:2015-01-08 14:00:12

标签: sql oracle analytic-functions

我试图从每个组或分区的最新日期获得10分钟间隔数据。

伪代码SQL:

Select
count(1) Over( partition by col1, col2, col3 
               Order by Col_Date Desc 
               Range Max(Col_Date) Between  Max(Col_Date) - 10(24*60) ) col_upd
From 
Table_1;

超出此特定范围的值将需要指定编号以设置删除。

2014-01-05 01:20:00  -- Max date 
2014-01-05 01:15:13
2014-01-05 01:12:13
2014-01-05 01:07:13  -- 1) these last two rows should be set for
2014-01-05 01:06:13  -- 2) delete or assign same id

有没有任何分析功能方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

你没有给表结构,但如果我组成一个虚拟表,如:

create table t42 (id number, grp_id number, dt date);

insert into t42 values (1, 1, timestamp '2014-01-05 01:20:00'); 
insert into t42 values (2, 1, timestamp '2014-01-05 01:15:13');
insert into t42 values (3, 1, timestamp '2014-01-05 01:12:13');
insert into t42 values (4, 1, timestamp '2014-01-05 01:07:13');
insert into t42 values (5, 1, timestamp '2014-01-05 01:06:13');

然后,这将给出组中每行的年龄与其(分析)max:

的比较
select grp_id, id, dt, max(dt) over (partition by grp_id) - dt as age
from t42
order by id;

    GRP_ID         ID DT                           AGE
---------- ---------- ------------------- ------------
         1          1 2014-01-05 01:20:00            0 
         1          2 2014-01-05 01:15:13 .00332175926 
         1          3 2014-01-05 01:12:13 .00540509259 
         1          4 2014-01-05 01:07:13 .00887731481 
         1          5 2014-01-05 01:06:13 .00957175926 

您可以将其用作内部查询并过滤最长10分钟的记录:

select grp_id, id, dt
from (
  select grp_id, id, dt, max(dt) over (partition by grp_id) - dt as age
  from t42
)
where age > (10*60)/(24*60*60)
order by id;

   GRP_ID         ID DT                
---------- ---------- -------------------
         1          4 2014-01-05 01:07:13 
         1          5 2014-01-05 01:06:13 

然后您可以根据需要使用这些删除/更新。从您的问题中不清楚您的组/分区是否已经从内部查询计算出来;如果是这样,你可以使用它而不是我的t42表。 (当然,改变列名等)。