选择查询以获取在24小时内发生五次或更多次的数据

时间:2018-04-06 16:45:29

标签: sql oracle

我很抱歉,我试过研究它,但似乎找不到合适的东西。

以下我已经找到了,但不能用于我的问题:

Filtering MySQL query result set to yield multiple occurences within a specific period of time

SQL Query To Obtain Value that Occurs more than once

我无法修改这些陈述来解决我的问题。

我想在24小时内选择在表格中出现五次或更多次数据的所有情况。 该表包含过去几年的数据。

表格未按日期排序(时间戳dd.mm.yy hh:mm:ss,sssssssss)

这显示整体出现> = 5,但如果这发生在24小时内则不行。

SELECT Column, COUNT(*) FROM Table
GROUP BY Column
HAVING COUNT(*) >= 5;

欢迎任何有关搜索关键字的建议。

先谢谢你。 最良好的问候 电友

编辑: 谢谢大家的帮助,对不起的问题我很抱歉。 我试着回答你所有的问题。

@mathguy 数据库版本是12c。

它有一列用于"数据"和另一列日期(时间戳)。我想在"数据"中找到相同值的出现。从第一次出现此值开始的< = 24小时窗口内的列。

@Matthew McPeak 我仍然试图理解你的SQL语句。我非常喜欢SQL。我问我的问题的方式很明显:(。

@Aurelian 我的问题不是很好。所以你的回答是不合适的,对不起。

@all我对自己的问题感到不满,浪费你的时间。

我希望你接受我的道歉。

祝你好运 电友

EDIT2:我现在正在尝试这个

begin
for i in (select Table.DATE(Timestamp), Table.IP, Table.Value from Table) 
loop
  dbms_output.put_line(i.Value); (instead of this output, i would like to add another loop and compare the Date(Timestamp) with i.DATE and count if matches
end loop;
end;

4 个答案:

答案 0 :(得分:1)

  

我想在第一次出现此值的< = 24小时窗口内的“数据”列中找到相同值的出现。

每个值第一次出现:

select value, min(timestmp) from mytable group by value;

现在我们必须检查一次值出现后是否至少再发生四次:

select value, min(timestmp)
from mytable m
group by value
having
(
  select count(*)
  from mytable following24hours
  where following24hours.value = m.value
    and following24hours.timestmp > min(m.timestmp) 
    and following24hours.timestmp < min(m.timestmp) + interval '24' hour
) >= 4;

答案 1 :(得分:1)

在Oracle 12中,您可以使用MATCH_RECOGNIZE子句获得非常干净和高效的解决方案。 (您可以根据接受的答案对其进行测试,以验证它是否正确,并查看哪些对您的实际数据更有效。)

select value, first_timestmp
from   mytable
match_recognize(
  partition by value
  order by     timestmp
  measures     a.timestmp as first_timestmp
  one row per match
  pattern      ( a b{4,} )
  define       b as b.timestmp <= a.timestmp + interval '24' hour
)
;

答案 2 :(得分:0)

您可以使用以下代码:

SELECT DISTINCT Column, COUNT(*) 
FROM TABLE
WHERE date >= SYSDATE - 1
GROUP BY Column
HAVING COUNT(*) >= 5;   

答案 3 :(得分:0)

您可以使用此表达式计算过去24小时内每个值的出现次数:

 COUNT(*) OVER (PARTITION BY column
                ORDER BY date_column 
                RANGE NUMTODSINTERVAL(24,'HOUR') PRECEDING

然后,获取表达式至少为5的行。

我没有您的数据,但这是一个使用DBA_OBJECTS的示例。此版本将显示对象OWNER在前五天内更新了5个或更多对象的对象更新。

with five_day_count as 
( select o.owner, 
  last_ddl_time,
  count(*) over ( partition by owner 
                  order by last_ddl_time 
                  range numtodsinterval(5, 'DAY') PRECEDING ) cnt
  from dba_objects o)
SELECT owner, last_ddl_time, cnt 
FROM five_day_count
WHERE cnt >= 5

...而且,只是为了好玩,这个查询将为每个OWNER提供一行 - 为该用户提供最新对象的五天间隔。此版本的变体可能更适合您的需要。

with five_day_count as 
( select o.owner, 
         last_ddl_time range_end,
         min(last_ddl_time) 
            over ( partition by owner 
                   order by last_ddl_time 
                   range numtodsinterval(5, 'DAY') PRECEDING ) range_start,
         count(*) 
            over ( partition by owner 
                   order by last_ddl_time range 
                   numtodsinterval(5, 'DAY') PRECEDING ) cnt
  from dba_objects o),
max_counts as 
( SELECT owner, 
         range_start, 
         range_end, 
         cnt, 
         row_number() 
             over ( partition by owner 
                    order by cnt desc, 
                             range_end desc) rn 
  FROM five_day_count
  WHERE cnt > = 5)
select owner, range_start, range_end, cnt
from max_counts
where rn = 1;