oracle怎么查询连续出现多次的数据

avatar
作者
猴君
阅读量:0

要查询连续出现多次的数据,可以使用窗口函数和分析函数来实现。以下是一个示例查询来查找在一个表中连续出现超过3次的数据:

WITH consecutive_counts AS (     SELECT *,            ROW_NUMBER() OVER (PARTITION BY data, grp ORDER BY id) AS rnk,            ROW_NUMBER() OVER (PARTITION BY data ORDER BY id) - ROW_NUMBER() OVER (PARTITION BY data, grp ORDER BY id) AS diff     FROM (         SELECT *,                ROW_NUMBER() OVER (ORDER BY id) - ROW_NUMBER() OVER (PARTITION BY data ORDER BY id) AS grp         FROM your_table     ) t )  SELECT data, COUNT(*) AS consecutive_count FROM consecutive_counts GROUP BY data, diff HAVING COUNT(*) > 3 

在这个例子中,首先使用ROW_NUMBER()来给每行数据分配一个唯一的序号,然后根据数据的变化情况(连续出现的数据在同一个grp组中)计算出连续出现次数,并最终根据这个次数来筛选出连续出现超过3次的数据。

    广告一刻

    为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!