oracle

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

小亿
436
2024-06-05 18:44:19
栏目: 云计算

要查询连续出现多次的数据,可以使用窗口函数和分析函数来实现。以下是一个示例查询来查找在一个表中连续出现超过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次的数据。

0
看了该问题的人还看了